Reputation: 607
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
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
Reputation: 7438
This link shows accepted answer on how to update binarydata to table using .Write
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
Upvotes: 0