locked
Object reference not set to an instance of an object. Please Help to solve error

    Question

  • Hi! I am new to C#. Kindly tell me what`s wrong with this code. I am inserting data in data base using two input fields EndValueTextBox and StartValueTextBox .

    I am receiving following error. "Object reference not set to an instance of an object"

    private void buttonSave_Click(object sender, EventArgs e)
        {
            connection = new System.Data.SqlClient.SqlConnection();
             da = new SqlDataAdapter();
            try
            {
                connection.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='G:\\C#.Net\\Forms Practice\\WindowsFormsPractice1\\WindowsFormsPractice1\\WindowsFormsPractice1.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True";
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message,"Connection String");
            }
            try
            {
                connection.Open();
                string sql = "insert into TBLWORKERS (first_name , last_name )" + " values('" + StartValueTextBox.Text + "', '" + EndValueTextBox.Text + ")";
                //SqlDataAdapter da = new SqlDataAdapter(query, connString);
    
    
                da.InsertCommand.CommandText = sql;
    
                da.InsertCommand.ExecuteNonQuery();
    
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection open");
            } 
    
    
        } 


     

    Thursday, October 27, 2011 6:25 AM

Answers

  • da.InsertCommand is null. And you should use parameters: it helps not doing errors like forgetting a quote before the right parenthesis, and deals about names like O'Hara which would break your query.

    try
    {
                connection.Open();
                string sql = "insert into TBLWORKERS (first_name , last_name )" + " values(@firstname, @lastname)";
                da.InsertCommand = new SqlCommand(sql, connection);
                da.InsertCommand.Parameters.Add("@firstname", StartValueTextBox.Text);
                da.InsertCommand.Parameters.Add("@lastname", EndValueTextBox.Text);
    
                da.InsertCommand.ExecuteNonQuery();
    
    }
    catch (System.Exception ex)
    {
                MessageBox.Show(ex.Message, "Connection open");
    }
    

    Thursday, October 27, 2011 9:57 AM

All replies

  • da.InsertCommand is null. And you should use parameters: it helps not doing errors like forgetting a quote before the right parenthesis, and deals about names like O'Hara which would break your query.

    try
    {
                connection.Open();
                string sql = "insert into TBLWORKERS (first_name , last_name )" + " values(@firstname, @lastname)";
                da.InsertCommand = new SqlCommand(sql, connection);
                da.InsertCommand.Parameters.Add("@firstname", StartValueTextBox.Text);
                da.InsertCommand.Parameters.Add("@lastname", EndValueTextBox.Text);
    
                da.InsertCommand.ExecuteNonQuery();
    
    }
    catch (System.Exception ex)
    {
                MessageBox.Show(ex.Message, "Connection open");
    }
    

    Thursday, October 27, 2011 9:57 AM
  • You have asked the same question at StackOverlfow and it was answered there!? if you haven't checked there then try out what Louis has suggested.
    www.midnightprogramer.net
    Thursday, October 27, 2011 10:23 AM