ASP.NET - GridView - Asked By Esmail on 20-Jul-10 01:07 AM

I want to take textboxes in grid view and iterate through it.

I want that data entered by user should be dumped in a access database column name Quantity.

How to do this????????????????

 

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);
      Session["abc"] = id;
      InsertRecords(id); // call method for insert and pass the StringCollection
      //TextBox txt;
      //foreach (GridViewRow rowItem in GridView1.Rows)
      //{

      //    txt = (TextBox)(rowItem.Cells[ColumnIndex].FindControl("TextBox1"));//Columnindex is in which
      //    // column ur textbos is
      //    //then get value of textbox by using textbox1.Text property then insert in db
      //}
   }
    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();
    }
}
Mash B replied to Esmail on 20-Jul-10 01:44 AM
// loop throgh all records and insert the value one by one
foreach (GridViewRow row in Gridview1.Rows)
{ // Access the Textbox
  TextBoxe gridText = (TextBox)row.FindControl("TextBox1");
  if (cb != null)
  {
    string textValue = gridText.Text;
    InsertRecords(textValue); // call method for insert
  }
}
 
 
// logic to insert in access data
   public void InsertRecords(string value)
  {
    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Esmail\WebSite1\Allot.mdb");
    string sql = "insert into Table1("Quantity") values (" + value + ");";
    conn.Open();
    OleDbCommand cmd = new OleDbCommand(sql, conn);
    cmd.ExecuteNonQuery();
  }
Esmail replied to Mash B on 20-Jul-10 02:04 AM

cb does not exist in current context and when I commented it it gives error in my previous code as

cmd,ExecuteNonQuery ................... errror near from clause

Mash B replied to Esmail on 20-Jul-10 02:07 AM

ohh sorry make below changes to my code (changes in red)


TextBoxe gridText = (TextBox)row.FindControl("TextBox1");

  if (gridText != null)
  {
  string textValue = gridText.Text;
  InsertRecords(textValue); // call method for insert
  }

Esmail replied to Mash B on 20-Jul-10 02:20 AM

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();
    }
Mash B replied to Esmail on 20-Jul-10 02:26 AM

There is syntax error in your query in INsertRecord methos , REMOVE SEMOCOLON FROM QUERY


below is your extra semocolon in red

"insert into Table2 select * from Table1, where ID in(" + id + ");";



Use below query

"insert into Table2 select * from Table1 where ID in(" + id + ");";

Esmail replied to Mash B on 20-Jul-10 02:58 AM
I have removed that the same error
Mash B replied to Esmail on 20-Jul-10 03:07 AM

I think there is table column mismatch , be sure Table1 and Table2 both have the same columns else it will give error.

If you want selected columns to be importes to other table , replace * with your columns to export .