Error: it is necessary to declare the scalar variable "@data"

I get an error when execution reaches cmd.ExecuteNonQuery()that says it should declare a 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();
+5
source share
2 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 text of the team. This way you specify parameter labels using OleDbCommand.


Here is 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();
+4
source

This link shows the accepted answer on how to update binary data in a table using .Write

http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/dc1b053d-f0d5-48f8-ad82-fb6d96d27f80

If this does not solve the problem, read below:

@data, @Data, .

TSQL, , MS Sql , , , , .

. .

SQL Server

SQL ?

0

All Articles