Priscilla Jobin
Priscilla Jobin

Reputation: 607

Error: Must declare the scalar variable "@data"

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();

Upvotes: 5

Views: 4062

Answers (2)

m1kael
m1kael

Reputation: 2851

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();

Upvotes: 4

Imran Rizvi
Imran Rizvi

Reputation: 7438

This link shows accepted answer on how to update binarydata to table using .Write

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

If that not solves your problem go ahead and read below:

You have used variable @data whereas you are declaring variable @Data, both are of different case.

In certain condition variable names can be case sensitive in TSQL, for instance if MS Sql server is installed using case sensitive collation, then table, column, variable names become case sensitive, even if database has case insensitive collation.

see the following links for more details.

Case sensitive variables in SQL Server

Is SQL syntax case sensitive?

Upvotes: 0

Related Questions