Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Must declare the scalar variable when updating my database

my code:

            SqlConnection con = new SqlConnection(@"DATA;");
            SqlDataAdapter cmd = new SqlDataAdapter();
            cmd.InsertCommand = new SqlCommand(" UPDATE TIME set TimeOut = @TimeOut Where TimeOut = @textBox1.Text", con);
            cmd.InsertCommand.Parameters.Add("@timeOut", DateTime.Now);
            con.Open();
            cmd.InsertCommand.ExecuteNonQuery();
            con.Close();

error:
Must declare the scalar variable "@textBox1"

I tried declaring a variable with textBox1.Text but it didn't work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

SqlConnection con = new SqlConnection(@"DATA;");
SqlDataAdapter cmd = new SqlDataAdapter();
cmd.InsertCommand = new SqlCommand(@"UPDATE `TIME` 
                                     SET TimeOut = @NewTimeOut 
                                     WHERE TimeOut = @OldTimeOut", con);
cmd.InsertCommand.Parameters.Add("@NewTimeOut", DateTime.Now);
cmd.InsertCommand.Parameters.Add("@OldTimeOut", DateTime.Parse(textBox1.Text));
con.Open();
cmd.InsertCommand.ExecuteNonQuery();
con.Close();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading