I have used this code to store data of selected checkboxes and data of text box as you have shown but error is coming in InsertRecords(string id) method : Syntax Error in from clause.
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;
for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
{
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 += ",";
}
}
}
id = id.Substring(0, id.Length - 1);
InsertRecords(id); // call method for insert and pass the StringCollection
foreach (GridViewRow row in GridView1.Rows)
{ // Access the Textbox
TextBox gridText = (TextBox)row.FindControl("TextBox1");
if (gridText != null)
{
string textValue = gridText.Text;
InsertingRecords(textValue); // call method for insert
}
}
}
// logic to insert in access data
public void InsertingRecords(string value)
{
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Esmail\WebSite1\Allot.mdb");
string sql = "insert into Table2 (Quantity) values (" + value + ");";
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
}
public void InsertRecords(string id)
{
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Esmail\WebSite1\Allot.mdb");
string sql = "insert into Table2 select * from Table1, where ID in(" + id + ");";
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
}