Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i tried to figure out. but failed.if i changed sqldatareader into comments mode it will work.please help me on this.
Giving error like
Must declare the scalar variable "@label2".


What I have tried:

con = new SqlConnection(@"Data Source=M2\SQL2016;Initial Catalog=inventoryDB;Integrated Security=True");// Database connetion path
                con.Open();
                cmd = new SqlCommand("INSERT INTO companyDB(compname) VALUES (@label2)", con);// inserting into database
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Close();

                cmd.Parameters.Add("@label2", cmpname.Text);
                MultipleActiveResultSets = true;
               //duplicatecheck();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Saved Successfully");
                cmpname.Text = "";
                cmpname.Focus();
                con.Close();
Posted
Updated 3-Mar-17 23:48pm
v3

1 solution

Of course it will work. Look at the code, you are calling ExecuteReader on a INSERT statement. Also you have not declared the parameter value.

And please, never do the following
C#
MessageBox.Show("Saved Successfully");

when you have not checked that the command succeeded. You could run for weeks, months or even years without knowing that your database is garbage. System commands return status information for a good reason, make use of it.
 
Share this answer
 
v2
Comments
Member 13037527 4-Mar-17 6:18am    
@Richard Sql datareader I used to check for duplicates.so that prevent to enter duplicates.
Richard MacCutchan 4-Mar-17 6:19am    
You are not checking anything, and your SQL statement is still incorrect.
Member 13037527 4-Mar-17 6:24am    
@richard for duplicate check i wrote this but error is coming "
There is already an open DataReader associated with this Command which must be closed first.

So iclosed there but still error is coming so i converted that two lines as comments
public bool duplicatecheck()
            {
            
            string constring = @"Data Source=M2\SQL2016;Initial Catalog=inventoryDB;Integrated Security=True"; 
            SqlConnection con = new SqlConnection(constring);// data connection
            SqlCommand cmd = new SqlCommand("Select count(*) from companyDB where compname= @alias", con);
            cmd.Parameters.AddWithValue("@alias", this.cmpname.Text);
            con.Open();
            
            
            SqlDataReader dr = cmd.ExecuteReader();
            
            
            

            int TotalRows = 0;
            TotalRows = Convert.ToInt32(cmd.ExecuteScalar());
            //dr.Close();
            if (TotalRows > 0)
            {
                MessageBox.Show("Alias " + dr[1].ToString() + " Already exist");
                
                return true;
                

            }
            else
            {
                return false;
            }
Richard MacCutchan 4-Mar-17 6:33am    
You are calling ExecuteReader immediately followed by ExecuteScalar. You need to stop and look closely at the workflow of your code, rather than randomly adding or modifying commands.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900