ASP.NETデータベースエラー
-
21-12-2019 - |
質問
チュートリアルオンラインに従うことを試みていて、私はデータベースに書き込もうとしたときにエラーが発生し続けています。
これは簡単な形式のコードです
@{
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から返されたエラーテキストであり、Webページに表示されます。
"The column cannot contain null values. [ Column name = ID,Table name = ContactLog ]"
.
私は完全に
の形を記入しました解決
問題のあなたのエラーは、問題はあなたがそれに添付されていないNOT NULL属性を持つ列(ID)を持つテーブル(ContactLog)を持っているということです。この列に値なしで行を挿入しようとしていて、このエラーが発生しています。
これを修正するかを[ID]列に挿入するか、列のFALSEの代わりにID属性をtrueに設定し、それを自動初期化/自動インクリメントに設定します。
私の仮定は、挿入ステートメントの構文を修正した場合でも挿入を正しく処理できなかった場合でも、
他のヒント
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);
. 所属していません StackOverflow