I have this code to enter data of checked boxes to be entered in other table of access database. I have taken template field and added a text box in Item Template but how to enter the text box data in a database on a button click.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Button1.Attributes.Add("onclick", "return confirm('Are you sure you want to select this units?');");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string id = string.Empty;
string tid = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
{
TextBox tb = (TextBox)GridView1.Rows[i].Cells[0].FindControl("TextBox1"); //find the TextBox
if (tb != null)
{
if (tb.Enabled)
{
tid += GridView1.Rows[i].Cells[1].Text;
tid += ",";
}
}
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox
if (cb != null)
{
if (cb.Checked)
{
id += GridView1.Rows[i].Cells[1].Text;
id += ",";
}
}
}
tid = tid.Substring(0, tid.Length - 1);
Label1.Text = tid.ToString();
id = id.Substring(0, id.Length - 1);
InsertRecords(id, tid); // call method for insert and pass the StringCollection
}
public void InsertRecords(string id, string tid)
{
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Esmail\WebSite2\Allot.mdb");
string sql = "insert into Table2 select * from Table1 where ID in(" + id + ");";
string sql1 = "insert into Table2 (Quantity) values where ID in(" + tid + ");";
conn.Open();
OleDbCommand cmd1 = new OleDbCommand(sql1, conn);
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
}
}