cmp cmp - 1 year ago 68
ASP.NET (C#) Question

Error: must declare the scalar variable @newsSummary

I have declared

as far as I am aware, but I still get this error:

System.Data.SqlClient.SqlException: Must declare the scalar variable "@newsSummmary".

I am trying to setup a news system, with images. I have most definitely also declared it in my HTML too.

Here is my 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 {
finally {

Answer Source

When you run two sql commands using the same SqlCommand.CommandText you need to separate the two commands with a semicolon

string strQuery = @"insert into tblFiles (FileName, FilePath) 
                    values(@FileName, @FilePath);
                    insert into tblNews (newsTitle, newsDate, newsSummmary, newsContent) 
                    values(@newsTitle, @newsDate, @newsSummmary, @newsContent)"; 

Thanks to Juharr below because he noted the problem that triggers the exact error message being the extra (or missing) m in the name @newSummmary.
It is not clear if you have really a field named Summmary with 3 m but you could fix the problem removing the extra m from your query or adding the extra m to your parameter.

This should be closed as a simple typo, however the error about the missing semicolon between the two command texts is still there and will bite you at the next step.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download