Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Must declare the scalar variable "@data"

Tags:

asp.net

I am getting an error when execution reaches cmd.ExecuteNonQuery() which says Must declare the scalar variable:

OleDbCommand cmd = new OleDbCommand();
cmd.Connection = Connection;
cmd.CommandTimeout = 0;

string commandText = "update groups set subjectline ='" +    txtSubjectLine.Text + "',data= @data where groupid = " + ddlGroup.SelectedItem.Value + " ";
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;

cmd.Parameters.Add("@Data",OleDbType.VarBinary);
cmd.Parameters["@Data"].Value = binarydata;               
cmd.ExecuteNonQuery();
like image 1000
Priscilla Jobin Avatar asked Oct 07 '22 17:10

Priscilla Jobin


1 Answers

Replace

string commandText = "update groups set subjectline ='" +    txtSubjectLine.Text + "',data= @data where groupid = " + ddlGroup.SelectedItem.Value + " ";

with

string commandText = "update groups set subjectline ='" +    txtSubjectLine.Text + "',data= ? where groupid = " + ddlGroup.SelectedItem.Value + " ";

that is, replace "@data" with "?" in the command text. This is how you specify parameter placeholders with OleDbCommand.


Here's the edited original:

OleDbCommand cmd = new OleDbCommand();
cmd.Connection = Connection;
cmd.CommandTimeout = 0;

cmd.CommandText = "update groups set subjectline ='" + txtSubjectLine.Text + "', data = ? where groupid = " + ddlGroupSelectedItem.Value;
cmd.CommandType = CommandType.Text;

cmd.Parameters.Add("p1", OleDbType.VarBinary);
cmd.Parameters["p1"].Value = binarydata;

cmd.ExecuteNonQuery();
like image 170
m1kael Avatar answered Oct 10 '22 22:10

m1kael