0

I have two problems:

  1. how can i use textbox in other way so it wont throw any error?
  2. i got an error :

    "object reference not set to an instance of an object.

and i dont know how to fix it. please can someone take a look?

protected void DataGridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("InsertFooter"))
        {


            try
            {

                System.Web.UI.WebControls.TextBox txtEmp_num = (System.Web.UI.WebControls.TextBox)DataGridView1.Controls[0].Controls[0].FindControl("txtEmp_num");
                System.Web.UI.WebControls.TextBox txtEmp_fn = (System.Web.UI.WebControls.TextBox)DataGridView1.Controls[0].Controls[0].FindControl("txtEmp_fn");
                System.Web.UI.WebControls.TextBox txtEmp_ln = (System.Web.UI.WebControls.TextBox)DataGridView1.Controls[0].Controls[0].FindControl("txtEmp_ln");
                System.Web.UI.WebControls.TextBox txtEmp_phone = (System.Web.UI.WebControls.TextBox)DataGridView1.Controls[0].Controls[0].FindControl("txtEmp_phone");
                System.Web.UI.WebControls.TextBox txtEmp_email = (System.Web.UI.WebControls.TextBox)DataGridView1.Controls[0].Controls[0].FindControl("txtEmp_email");

                MySqlConnection conn = new MySqlConnection(connection);
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "insert into employees(Emp_number,First_name,Last_name,Phone_num,Email) values(@1,@2,@3,@4,@5)";



                cmd.Parameters.AddWithValue("@1", txtEmp_num.Text);
                cmd.Parameters.AddWithValue("@2", txtEmp_fn.Text);
                cmd.Parameters.AddWithValue("@3", txtEmp_ln.Text);
                cmd.Parameters.AddWithValue("@4", txtEmp_phone.Text);
                cmd.Parameters.AddWithValue("@5", txtEmp_email.Text);
                conn.Open();
                cmd.ExecuteNonQuery();

                conn.Close();
                Bind();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90

2 Answers2

0

Object reference not set to an instance of an object means that you are trying to get a property / calling a method of something which is null.

It means that -at least- one of your variable is null: txtEmp_num, txtEmp_fn, txtEmp_ln, txtEmp_phone, txtEmp_email and it is throwing you an exception when you are trying to call the Text property inside AddWithValue method

Maybe, you have some issue with the values you defined in FindControl("txt<smth>")

Didier Aupest
  • 3,227
  • 2
  • 23
  • 35
0

Try it like below

System.Web.UI.WebControls.TextBox txtEmp_num=(System.Web.UI.WebControls.TextBoDataGridView1.Rows[0].Cells[0].FindControl("txtEmp_num");
System.Web.UI.WebControls.TextBox txtEmp_fn = (System.Web.UI.WebControls.TextBox)DataGridView1.Rows[0].Cells[1].FindControl("txtEmp_fn");
System.Web.UI.WebControls.TextBox txtEmp_ln = (System.Web.UI.WebControls.TextBox)DataGridView1.Rows[0].Cells[2].FindControl("txtEmp_ln");
System.Web.UI.WebControls.TextBox txtEmp_phone = (System.Web.UI.WebControls.TextBox)DataGridView1.Rows[0].Cells[3].FindControl("txtEmp_phone");
System.Web.UI.WebControls.TextBox txtEmp_email = (System.Web.UI.WebControls.TextBox)DataGridView1.Rows[0].Cells[4].FindControl("txtEmp_email");
umer
  • 1,196
  • 1
  • 14
  • 33
  • it gives me "specified argumenr wad out of the range of valid values. parameter name:index – Shani Ohayon Aug 10 '16 at 12:33
  • it returns you a reference to the TextBox you can further get the value inside the Textbox by txtEmp_num.Text property. – umer Aug 10 '16 at 13:03