문제

나는 튜토리얼을 온라인으로 지키려고 노력하고 있으며, 데이터베이스에 쓸 때 오류가 계속 발생하는 단계를 정확히 수행했습니다.

여기에 간단한 형식의 코드가 있습니다

@{
var name = string.Empty;
var email = string.Empty;
var subject = string.Empty;
var message = string.Empty;

if (IsPost){
    name = Request["txtName"];
    email = Request["txtEmail"];
    subject = Request["txtSubject"];
    message = Request["txtMessage"];

    var db = Database.Open("FormAndDataDB");

    db.Execute("INSERT INTO ContactLog (Name, Email, Subject, Message, DateSent) " +
      "VALUES (@0, @1, @2, @3, @4)", name, email, subject, message, DateTime.Now);
}    

 }

 <doctype html>

 <html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>

<body>
    <form action="contact.cshtml" method="POST">
        <p>
            <label for="txtName">Name:</label><br />
            <input type="text" name="txtName" id="txtName" value="@name"/>
        </p>

        <p>
            <label for="txtEmail">Email:</label><br />
            <input type="text" name="txtEmail" id="txtEmail" value="@email" />
        </p>

        <p>
            <label for="txtSubject">Subject:</label><br />
            <input type="text" name="txtSubject" id="txtSubject" value="@subject" />
        </p>

        <p>
            <label for="txtMessage">Message:</label><br />
            <textarea name="txtMessage" id="txtMessage" cols="40" rows="10" >@message</textarea>
        </p>

        <p>
            <input type="submit" value="send" />
        </p>
    </form>
</body>
</html>
.

SQL INSERT 문을 실행하면 Visual Studio에서 얻은 오류가 발생합니다.

"An exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll but was not handled in user code"
.

SQL에서 반환 된 오류 텍스트이며 웹 페이지에 표시됩니다.

"The column cannot contain null values. [ Column name = ID,Table name = ContactLog ]"
.

나는 양식을 완전히 채웠다

도움이 되었습니까?

해결책

오류에도 불구하고 문제는 Not Null 속성이 부착 된 Not Null 속성이있는 열 (ID)이있는 테이블 (ContactLog)을 가지고있는 것으로 보입니다.상기 열에 값없이 행을 삽입 하고이 오류가 발생합니다.

이 문제를 해결하려면 [ID] 열에 값을 삽입하거나 컬럼에서 FALSE 대신 ID 속성을 true로 설정하고 Autogenerate / autoincrement를 허용합니다.

나의 가정은 insert 문의 구문을 수정 한 경우에도 삽입물을 올바르게 처리 할 수 없었습니다.

다른 팁

5 개의 열이 있지만 3 개의 값만 있습니다.

귀하의 진술은

입니다.
INSERT INTO ContactLog (Name, Email, Subject, Message, DateSent)
                VALUES (@0, @1, @3)
.

@ 2 및 @ 4에서 무슨 일이 일어 났습니까?

어쩌면

가 필요할 것입니다.
db.Execute("INSERT INTO ContactLog (Name, Email, Subject, Message, DateSent) "+
           "VALUES (@0, @1, @2, @3, @4)",
           name, email, subject, message, DateTime.Now);
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top