Yamini
Yamini

Reputation: 1

store selected grid view value into database

I've a gridview in which a radiobutton is used to select a particular row of data. Now I need to save radio button selected row into database. How do I achieve this? I'm not using JQuery or Ajax, my entire coding is done on ASP.NET

Upvotes: 0

Views: 968

Answers (2)

Vivek Raj
Vivek Raj

Reputation: 456

Easy

  1. Search through GridRows
  2. FindControl radio button on each row.
  3. Check that button is Checked or not.

and now try it.

Upvotes: 0

Shreyas Achar
Shreyas Achar

Reputation: 1435

I didn't got your problem correctly,But the ideal methodology is to use Check box instead of Radio button will be far better for your coding purpose.

The below code is a demo for inserting the values into the database from a grid view where in the particular rows will be inserted which has been checked.

   protected void Button1_Click(object sender, EventArgs e)
{
     SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionstring"].ToString());

    SqlCommand cmd = new SqlCommand();
    string active = "N";
    for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
    {
        string A = GridView1.Rows[i].Cells[0].Text;
         string B = GridView1.Rows[i].Cells[1].Text;
        GridViewRow row = GridView1.Rows[i];
        CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
        if (Ckbox.Checked == true)
        {
            cn.Open();
            cmd.CommandText = "Insert into table_name(A,B) values (@A,@B)";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cn;

            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@A", A);
            cmd.Parameters.AddWithValue("@B", B);
            cmd.ExecuteNonQuery();
            cn.Close();



    }


}

Hope this will be helpful for you.

Upvotes: 1

Related Questions