i am in problem to insert data from gridview by checking checkbox
i give my try below & source in zip file
| ID | namw | qty | amount | Select |
|---|---|---|---|---|
| 73 | ras | 4 | 4.0000 | |
| 1920 | ras | 4 | 4.0000 | |
| 1921 | ras | 4 | 4.0000 | |
| 1922 | ras | 4 | 4.0000 | |
| 1923 | ras | 4 | 4.0000 | |
| 1961 | ras | 4 | 4.0000 |
protected void Page_Load(object sender, EventArgs e)
{
var st = from s in db.ITEMMs select s;
gvw_employees.DataSource = st;
gvw_employees.DataBind();
}
DataClasses1DataContext db = new DataClasses1DataContext();
protected void Button1_Click(object sender, EventArgs e)
{
try
{
//foreach (GridViewRow dg in gvw_employees.Rows)
// {
// CheckBox ckbox;
// ckbox = ((CheckBox)dg.FindControl("checkBox"));
// if (ckbox.Checked == true)
// {
for (int i = 0; i < gvw_employees.Rows.Count; i++)
{
int Id = Convert.ToInt32(gvw_employees.Rows[i].Cells[0].Text);
string name = gvw_employees.Rows[i].Cells[1].Text;
int qty = Convert.ToInt32(gvw_employees.Rows[i].Cells[2].Text);
decimal amt = Convert.ToDecimal(gvw_employees.Rows[i].Cells[3].Text);
var st = new ITEMM
{
ID = Id,
namw = name.ToString(),
qty = qty,
amount = amt,
};
db.ITEMMs.InsertOnSubmit(st);
db.SubmitChanges();
Label1.Text = "success";
}
//}
//else
//{
// Label1.Text = "faiels";
//}
//}
}
catch (Exception ex)
{
Label1.Text = ex.Message.ToString();
}
}

Satyapriya NayakPosted Feb 20, 2013, 8:28 AM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Insert_Selectedata_from_Gridview
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
int select;
protected void btn_show_Click(object sender, EventArgs e)
{
DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath("student.xml"));
GridView1.DataSource = reportData;
GridView1.DataBind();
}
protected void btn_insert_Click(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Chbox = (CheckBox)row.FindControl("chb1");
if (Chbox.Checked == true)
{
select++;
}
}
if (select == 0)
{
Page.RegisterStartupScript("Alert Message", "");
return;
}
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string sid = GridView1.Rows[i].Cells[1].Text;
string sname = GridView1.Rows[i].Cells[2].Text;
string smarks = GridView1.Rows[i].Cells[3].Text;
string saddress = GridView1.Rows[i].Cells[4].Text;
GridViewRow row = GridView1.Rows[i];
CheckBox Chbox = (CheckBox)row.FindControl("chb1");
if (Chbox.Checked == true)
{
InsertData(sid, sname, smarks, saddress);
}
}
Response.Write("Record inserted successfully");
}
void InsertData(String sid, String sname, String smarks,string saddress)
{
SqlConnection con = new SqlConnection(connStr);
try
{
con.Open();
com = new SqlCommand("insert into student values('" + sid + "','" + sname + "','" + smarks + "','" + saddress + "')", con);
com.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
}
Thanks
If this post helps you mark it as answer