M
M
Maria Popova2016-05-04 21:50:55
PHP
Maria Popova, 2016-05-04 21:50:55

Can you help me with the error System.Data.SqlClient.SqlException (0x80131904): Must declare the scalar variable?

error in this code:

try {
                string str = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(str);
                SqlCommand com = new SqlCommand("UPDATE [Composition] SET [Mark] = @Mark WHERE [Id_composition] = @original_Id_composition AND (([Mark] = @original_Mark) OR ([Mark] IS NULL AND @original_Mark IS NULL))", con);
                con.Open();
                SqlDataReader reader = com.ExecuteReader();
                //reader.Open();
                Session.Add("Mark", (int)reader["Mark"]);
                Session.Add("Id", (int)reader["Id_composition"]);
                com.Parameters.AddWithValue("@original_Id_composition", (Session["Id"]));
                com.Parameters.Add("@Mark", SqlDbType.Int).Value =(int)(Session["Mark"])+1;
                reader.Close();
                con.Close();
                LErr.Text = "Спасибо!, нам очень важен ваш голос! ";
            }
            catch(Exception ex)
            {
                LErr.Text = "Что то пошло не так--- " + ex.ToString();
            }

Here in the table you need to change the Mark column, but an error occurs
System.Data.SqlClient.SqlException (0x80131904): Must declare the scalar variable "@Mark". в System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) в System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) в System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) в System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) в System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() в System.Data.SqlClient.SqlDataReader.get_MetaData() в System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) в System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) в System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) в System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) в System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) в System.Data.SqlClient.SqlCommand.ExecuteReader() в Diplom.MyKabinet.GV_SelectedIndexChanged(Object sender, EventArgs e) в D:\учеба\4 курс\Диплом\Diplom\Diplom\MyKabinet.aspx.cs:строка 120 ClientConnectionId:b4a9d6da-85b7-4747-bb42-41c9aa705e92 Error Number: 137, State: 2, Class: 15

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2016-05-04
@dmitryKovalskiy

Offhand - you have written some garbage. 1) I don’t know what Session is in this context, well, and God bless her. 2) You first execute the query and then give it the value of the @Mark variable.

com.Parameters.AddWithValue("@original_Id_composition", (Session["Id"]));
                com.Parameters.Add("@Mark", SqlDbType.Int).Value =(int)(Session["Mark"])+1;

Move these 2 lines of code before the con.Open() line

K
Konstantin Tsvetkov, 2016-05-05
@tsklab

Form a SQL string without parameters (substituting the necessary values ​​into it) and execute.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question