2

I have a GridView in which i have placed textbox in item template of each column. I want to store the values which I will be entering in the textboxes to the database on button click.

protected void Button1_Click(object sender, EventArgs e)
{
    GridView1.Visible = true;
    DataTable dt = new DataTable();
    DataRow row = dt.NewRow();
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        string UserID = GridView1.Rows[i].Cells[1].Text;
        string q="insert into details (name) values('"+UserID+"')";

        SqlCommand cmd = new SqlCommand(q, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
    }
}

the problem is that the textboxes are not even visible when i run the page.

Devraj Gadhavi
  • 3,541
  • 3
  • 38
  • 67
Arbaaz
  • 121
  • 1
  • 5
  • 15

3 Answers3

1

You can not get reference of textbox from grid directly. So first you need to find the textbox from each row of grid. Code should be like this..

protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
DataTable dt = new DataTable();
DataRow row = dt.NewRow();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
   ## add below code ##
    TextBox txtUsrId=(TextBox)GridView1.Rows[i].FindControl("Your textbox id");
    string UserID = txtUsrId.Text;

    string q="insert into details (name) values('"+UserID+"')";

    SqlCommand cmd = new SqlCommand(q, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
}

}

let me know..

Anup
  • 80
  • 1
  • 1
  • 6
0

This statement is wrong-

 string UserID = GridView1.Rows[i].Cells[1].Text;

You cannot directly access a subcontrol's value. Do it like this-

TextBox tt = (TextBox)GridView1.Rows[i].FindControl("your text box");
string UserId= tt.Text;

And coming to your issue, you should be showing your Gridview source to get help.

Shiridish
  • 4,942
  • 5
  • 33
  • 64
0

It should be something like this.

protected void Button1_Click(object sender, EventArgs e)
{
    Int32 TotalRecords = 0;
    SqlConnection con = new SqlConnection("data source=.\; Integrated Security=SSPI; initial catalog=Testdb;User ID=sa;password=abc123;");            

    foreach (GridViewRow gvRow in GridView1.Rows)
    {            
        TextBox textBox1 = (TextBox)gvRow.FindControl("textBox1");

        String q = "INSERT INTO details ([name]) VALUES('" + textBox1.Text.Trim() + "')";
        SqlCommand cmd = new SqlCommand(q, con);

        con.Open();
        TotalRecords += cmd.ExecuteNonQuery();
        con.Close();
    }

    lblMessage.Text = "Total " + TotalRecords + " inserted successfully.";

}

About the textboxes not being displayed on the page, check if the Visible property of either GridView, TemplateField or the TextBox it self is not set to false. (As you are making the gridview visible in the button click event.)

Also I would not recommend inserting records this way. Rather I would create a strored procedure & pass parameters instead, to avoid SQL INJECTION.

Community
  • 1
  • 1
Devraj Gadhavi
  • 3,541
  • 3
  • 38
  • 67