Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
foreach (GridViewRow rowItem in GridView1.Rows)
{
      CheckBox chk = (CheckBox)(rowItem.Cells[7].FindControl("ch1"));

         if (chk != null && chk.Checked)
        {
               
                SqlConnection myConnection = new SqlConnection("Data Source=PCIMS051;Initial Catalog=unblockingwebsite;Integrated Security=True");
                myConnection.Open();


                SqlCommand myCommand = new SqlCommand("INSERT INTO twebsite_unblocking (Requested_Status) " +
                                     "Values ('2')", myConnection);
  }
     }

The if block not working and not update to sql
Posted
Updated 26-Nov-12 18:46pm
v2

Try this,


foreach (GridViewRow rowItem in GridView1.Rows)
{
CheckBox chk = (CheckBox)(rowItem.Cells[7].FindControl("ch1"));
 
if (chk.Checked==true)
{

SqlConnection myConnection = new SqlConnection("Data Source=PCIMS051;Initial Catalog=unblockingwebsite;Integrated Security=True");
myConnection.Open();
 

SqlCommand myCommand = new SqlCommand("INSERT INTO twebsite_unblocking (Requested_Status) " +
"Values ('2')", myConnection);
}
}
 
Share this answer
 
Hi,

Update your code as below.

C#
foreach (GridViewRow rowItem in GridView1.Rows)
{
   CheckBox chk = (CheckBox)(rowItem.FindControl("ch1")); 
   if (chk != null && chk.Checked)
   {  
      try
      {             
         SqlConnection myConnection = new SqlConnection("Data Source=PCIMS051;Initial Catalog=unblockingwebsite;Integrated Security=True");
         myConnection.Open();
         SqlCommand myCommand = new SqlCommand("INSERT INTO twebsite_unblocking (Requested_Status) " + "Values ('2')", myConnection);
         myCommand.ExecuteNonQuery();
         con.Close();
      }
      catch
      {
      }
    }
}

Try to debug the code line by line and see the result on each line.
If still you are facing issue then copy the error here.
 
Share this answer
 
Comments
Member 9630032 27-Nov-12 1:57am    
insertion into sql failed
Mohd. Mukhtar 27-Nov-12 2:15am    
Is connection opening or not, copy-past your error stack here.

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