0

I'm dealing with following code

private void tbMemberID_TextChanged(object sender, EventArgs e)
{
        try
        {

            //int RowsAffected = 0;
            DataAccess oDataAccess = new DataAccess();
            con.Open();
            oDataAccess.cmd.CommandText = "SELECT MemberName FROM AccountInfo where MemberID='" + tbMemberID.Text + "'";
            oDataAccess.cmd.Connection = con;

            tbMemberName.Text = ((string)cmd.ExecuteScalar());
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

When I insert memberID, it shows error

object reference is not set to an instance of object

Is something wrong with my code?

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Niteen4u
  • 31
  • 6

1 Answers1

0

You might need to get it from the sender parameter, like:

 private void tbMemberID_TextChanged(object sender, EventArgs e)
    {
        try
        {
            TextBox txtBox= sender as Textbox;
            //int RowsAffected = 0;
            DataAccess oDataAccess = new DataAccess();
            con.Open();
            oDataAccess.cmd.CommandText = "SELECT MemberName FROM AccountInfo where MemberID='" + txtBox.Text + "'";
            oDataAccess.cmd.Connection = con;

            tbMemberName.Text = ((string)cmd.ExecuteScalar());
            con.Close();
        }

        catch (Exception ex)
        {

            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Also, please be aware of sql injection

Zippy
  • 1,764
  • 5
  • 24
  • 31