0

I have a problem with using textbox in c# (Visual Studio). When I'm writting this:

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = gv.Rows[e.RowIndex];

    TextBox serial = (TextBox)row.Cells[1].Controls[0];
    TextBox name = (TextBox)(row.Cells[2].Controls[0]);
    TextBox cost = (TextBox)(row.Cells[3].Controls[0]);
    TextBox number = (TextBox)(row.Cells[4].Controls[0]);
    string query = String.Format("UPDATE games SET name='{1}', cost={2}, number={4} WHERE serial={0}",
                                serial.Text, name.Text, cost.Text, number.Text);

    SqlConnection connect = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(query, connect);
    connect.Open();
    cmd.ExecuteNonQuery();
    connect.Close();

    this.gv.EditIndex = -1;
    BindTheGridView();
}

i get 4 errors like this one:

Error 1 'TextBox' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'TextBox' could be found (are you missing a using directive or an assembly reference?)

Can someone tell me why is this thing happening? tnx...

  • 7
    What is the fully qualified path/name of `TextBox`? Also, refrain from constructing queries like that, you are open to SQL injection. – DGibbs Feb 06 '14 at 09:43
  • @user3278876 ca you explain in detail... – Muhammad Saad Feb 06 '14 at 09:45
  • Do you have `using System.Web.UI.WebControls;` in top of the your code file? it seems you have defined some other Type/Class named 'TextBox', select the TextBox and click F12 to find definition – Damith Feb 06 '14 at 09:50
  • see this answer: [http://stackoverflow.com/questions/2101203/help-with-error-object-does-not-contain-a-definition-for-text](http://stackoverflow.com/questions/2101203/help-with-error-object-does-not-contain-a-definition-for-text) – markpsmith Feb 06 '14 at 10:04

3 Answers3

0

Try doing something like this:

var serial = (sender as GridView).FindControl("serial") as TextBox;
var name = (sender as GridView).FindControl("name") as TextBox;
...
...
Alok
  • 1,290
  • 1
  • 11
  • 21
0

The reason your controls aren't being found is because you don't actually HAVE a serial textbox etc. Once your code has compiled and been executed you would have as many serial textboxes as you would rows. You can verify this by simply looking at the HTML of your rendered page; you'll find lots of text boxes with serial in the ID/Name

You are referencing the row that you're interested in by calling GridViewRow row = gv.Rows[e.RowIndex]; - This is the row where you will FindControl the textbox.

So try getting to them by using:

TextBox serial = (TextBox)row.FindControl("serial");

etc..

As a side-note, ALL rows in your gridview will go through your method aboce; including any headers and footers. To ensure you don't run into an issue where the textboxes are not found again, instruct your code to only process WHEN the row is actually a data row.

So wrap your code (the code after you get the GridViewRow, in an if like this - after you get your row

if (row == GridViewRow.RowType.DataType){
  // your code in here.
}
Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127
0
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = gv.Rows[e.RowIndex];

    string serial = row.Cells[1].Controls[0];
    string name = row.Cells[2].Controls[0]);
    string cost = row.Cells[3].Controls[0]);
    string number = row.Cells[4].Controls[0]);
    yourtextbox_Id.text = serial;
    nametextbox.text = name;
    costtextbox.text = cost ;
    numbertextbox.text = number ;
// do like this

    string query = String.Format("UPDATE games SET name='{1}', cost={2}, number={4} WHERE serial={0}",
                                serial.Text, name.Text, cost.Text, number.Text);

    SqlConnection connect = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(query, connect);
    connect.Open();
    cmd.ExecuteNonQuery();
    connect.Close();

    this.gv.EditIndex = -1;
    BindTheGridView();
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Nitin Ware
  • 109
  • 9