我宣佈@newsSummary
據我所知標量變量,但我仍然得到這個錯誤:錯誤:必須聲明@newsSummary
System.Data.SqlClient.SqlException: Must declare the scalar variable "@newsSummmary".
我試圖建立一個新聞系統,在圖像上。我絕對也在我的HTML中聲明瞭它。
這裏是我的C#
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
// Save files to disk
FileUpload1.SaveAs(Server.MapPath("/images/admin/news/" + FileName));
// Add Entry to DataBase
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "insert into tblFiles (FileName, FilePath) values(@FileName, @FilePath)" + "insert into tblNews (newsTitle, newsDate, newsSummmary, newsContent) values(@newsTitle, @newsDate, @newsSummmary, @newsContent)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@FileName", FileName);
cmd.Parameters.AddWithValue("@FilePath", "/images/admin/news/" + FileName);
cmd.Parameters.AddWithValue("@newsTitle", txtnewstitle.Text);
cmd.Parameters.AddWithValue("@newsDate", txtnewsdate.Text);
cmd.Parameters.AddWithValue("@newsSummary", txtnewssummary.Text);
cmd.Parameters.AddWithValue("@newsContent", txtnewsmaincontent.Text);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try {
con.Open();
cmd.ExecuteNonQuery();
}
finally {
con.Close();
con.Dispose();
}
}
}
你在插入命令'@ newsSummmary'中有一個額外的m @ – juharr
@juharr oops! *隱藏* - 這是我需要休息的標誌。很好看,非常感謝! – cmp
你應該看看[我們可以停止使用AddWithValue()了嗎?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/)並停止使用'.AddWithValue()' - 它可能會導致意外的和令人驚訝的結果... –