Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Tabel as follows (table Name Student_details)

C#
Stud_id   Stud_name MobileNo

 1        Ram       99404542100
 2        Sam       97892112000
 3        Baskar    99454452121
 4        Suresh    97892512223
 5        Vikram    97905678242


I display the above Student_details into gridview.

My code as follows
C#
public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection("Server=(local);initial catalog=Master;Trusted_Connection=True");
    string str;
    SqlCommand cmd;

    protected void Page_Load(object sender, EventArgs e)
    {
        studentdetails();
    }

    private void studentdetails()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select Stud_id,Stud_Name,Mobileno from Student_details order by Stud_Name", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        con.Close();
        gvstudentdetails.DataSource = ds;
        gvstudentdetails.DataBind();
    }

  protected void btn_checked_Click(object sender, EventArgs e)
    {

           foreach(GridViewRow gvrow in gvstudentdetails.Rows)
           {
              CheckBox chk = (CheckBox)gvstudentdetails.FindControl("chbox");
            if (chk != null & chk.Checked)
          {
con.Open();
                str = "insert into Student_details values('" + txt_studentid.Text.ToString().Trim() + "','" + txt_studname.Text.ToString().Trim() + "','" + txt_mobile.Text.ToString().Trim() + "')";
                cmd = new SqlCommand(str, con);
                cmd.ExecuteNonQuery();
                Response.Write("Record inserted");
                con.Close();

               }
          }
    }

when i run , in gridview 5 rows details are displayed in the gridview.
When i run i checked the first two rows values in database, that selected checkbox values is not saving in the database.

please help me what is the problem in my code.

Regards,
Narasiman P
Posted
Updated 23-Feb-14 3:51am
v3
Comments
Kornfeld Eliyahu Peter 23-Feb-14 8:41am    
Please format your code...
Also elaborate on 'not saving'...
You get error? Something else saved in the DB not what you've expected?
Thanks7872 23-Feb-14 9:58am    
foreach(GridViewRow gvrow in gvstudentdetails.Rows)
{
CheckBox chk = (CheckBox)gvstudentdetails.FindControl("chbox");
}


When you have gridview row why you are using Gridview? Further,did you face any error? Question is not clear at all.

1 solution

Try with this,
C#
try
{
    // your insert query code and stuff...

    int n = cmd.ExecuteNonQuery();
    if(n > 0)
        Response.Write("Record inserted.");
}
catch(Exception ex)
{
    Response.Write(ex.ToString());
}

and on Page_Load()
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            studentdetails();
    }

-KR
 
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