Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am New to C#, In my Project I have a Page that displays the Registered Systems for Approval in the Grid View I have a Check box Template Field in the Grid View for Selection and Button outside the Gridview for the selected row to be Updated I had tried with the Following code for Updation but it excecutes only the Else part that "Select atleast one Row" even if I select a Check Box My Code is,

C#
protected void Button1_Click(object sender, EventArgs e)
       {

           for (int i = 0; i < GridView1.Rows.Count; i++)
           {

               CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");

               if (cb != null && cb.Checked)
               {
                   using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EvalCon"].ConnectionString))
                   {
                       string sqlqry = "Update Sysinfo Set Appflag='W',PApprovedate=getdate() where Mac_id=' " + GridView1.Rows[i].Cells[0].ToString()+ "'";
                       try
                       {
                           using (SqlCommand cmd = new SqlCommand(sqlqry, conn))
                           {
                               cmd.CommandType = CommandType.StoredProcedure;
                               conn.Open();
                               cmd.ExecuteNonQuery();
                               lblMessage.Visible = true;
                               lblMessage.Text = "Selected System(s) has been Approved Successfully...";
                           }
                       }
                       catch (SqlException ex)
                       {
                           if (ex.Message.Contains("SQL"))
                           {
                               lblMessage.Visible = true;
                               lblMessage.ForeColor = System.Drawing.Color.Red;
                               lblMessage.Text = "Error Occured Continue Kindly Contact Administartor!!!";
                           }
                       }
                       finally
                       {
                           conn.Close();
                           conn.Dispose();
                       }
                   }
               }
               else
               {
                   lblMessage.Visible = true;
                   lblMessage.ForeColor = System.Drawing.Color.Red;
                   lblMessage.Text = "Select Atleast One Row!!!";

               }

           }

       }


Can Anybody help me out with Example Coding....
Posted
Updated 13-May-13 22:21pm
v3
Comments
vijay__p 14-May-13 4:14am    
Are you getting checkbox object in your code?
Rajesh Kumar Sowntararajan 14-May-13 4:18am    
Yes I am getting the Check box object for Updation of the Table
Vani Kulkarni 14-May-13 4:15am    
Did you try and debug your code using breakpoints? What is the value coming in CheckBox cb? Is it giving proper value?
Rajesh Kumar Sowntararajan 14-May-13 4:20am    
@ Vani Kulkarni Sorry I am new to C# I dont know to debug with breakpoints

1 solution

Try below code to get checkbox value

C#
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("chkSelect");


Now to get value of id use DataKeys from GridView.
Set DataKeyNames property of grid to PropertyName of Id and you can get value of it ineach row as below

C#
GridView1.DataKeys[i].Value
 
Share this answer
 

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