Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code. I can't get rid of this error please help.

C#
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       String strConnString = ConfigurationManager.ConnectionStrings["facultylogConnectionString"].ConnectionString;
       String strQuery = "Insert into selections (subname, selections) VALUES (@subname, @selections) where subname= @subname ";
       SqlConnection con = new SqlConnection(strConnString);
       SqlCommand cmd = new SqlCommand();
       cmd.CommandType = CommandType.Text;
       cmd.CommandText = strQuery;
       cmd.Connection = con;
       cmd.Parameters.AddWithValue("@subname", DropDownList5.SelectedValue);
       string var = string.Empty;
       for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
       {
           if (CheckBoxList1.Items[i].Selected)
           {
               var += CheckBoxList1.Items[i].Text.ToString() + ",";

           }
       }
       cmd.Parameters.AddWithValue("@selections", var);
       cmd.Parameters.Clear();
       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();
   }


What I have tried:

I don't know why I keep getting that error above. Even after I remove
where subname= @subname "
Posted
Updated 21-Jun-20 2:27am
v3
Comments
PIEBALDconsult 21-Jun-20 15:40pm    
Be sure that the value of the parameter is not null.
And, no, you can't have a WHERE clause on that type of INSERT statement.

You seem to be clearing the parameters after you've set them with:
cmd.Parameters.Clear();
 
Share this answer
 
Look ay comments in code.
C#
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       String strConnString = ConfigurationManager.ConnectionStrings["facultylogConnectionString"].ConnectionString;
       String strQuery = "Insert into selections (subname, selections) VALUES (@subname, @selections) where subname= @subname ";
       SqlConnection con = new SqlConnection(strConnString);
       SqlCommand cmd = new SqlCommand();
       cmd.CommandType = CommandType.Text;
       cmd.CommandText = strQuery;
       cmd.Connection = con;
       cmd.Parameters.AddWithValue("@subname", DropDownList5.SelectedValue); // Here you declare first parameter
       string var = string.Empty;
       for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
       {
           if (CheckBoxList1.Items[i].Selected)
           {
               var += CheckBoxList1.Items[i].Text.ToString() + ",";

           }
       }
       cmd.Parameters.AddWithValue("@selections", var); // Here you declare second parameter
       cmd.Parameters.Clear(); // Here you kill parameters 
       con.Open();
       cmd.ExecuteNonQuery(); // and here you execute the query 
       con.Close();
   }
 
Share this answer
 
v2
Comments
[no name] 25-Jun-20 9:19am    
I hate votes below 4 without comments, so have a 5 for balance.
Patrice T 25-Jun-20 9:29am    
Thank you very much.
On my side, I appreciate comment explaining what is wrong in a solution.
[no name] 25-Jun-20 9:45am    
On my side I will give always a comment in case the vote is below 4.
Ok, for spammers/trolls/abusive I allow me then and when also to give a 1 without comment, but that is a completely other field.
Patrice T 25-Jun-20 9:52am    
I agree with you.
Contrary to you, not so many downvoters have the courage to express the reason.
[no name] 25-Jun-20 10:04am    
What I forgot: In Q / A for serious answers, I don't go below 3, even if the answer is completely wrong. This is because I still trust that the respondent simply tried to help;)

Now let us go on to help and do not post too much templated answers :-)

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