locked
object reference error RRS feed

  • Question

  • User-674342078 posted
    this code gives error of object reference not set to an instance of an object. how can i solve this?
    private void details(int id) { string query="select * from Employee where emp_id="+id; SqlConnection con = new SqlConnection(@"Data Source=JAHANGEER\JAHANGEER;Initial Catalog=TimeInformationSystem;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand(query, con); try { SqlDataReader read = cmd.ExecuteReader(); while (read.Read()) { txt_firstName.Text = obj_emp.obj_sqlDataReader["emp_fname"].ToString(); txt_lastName.Text = obj_emp.obj_sqlDataReader["emp_lname"].ToString(); datepicker_joinDate.SelectedDate = (DateTime)obj_emp.obj_sqlDataReader["emp_join_date"]; datepicker_dateOfBirth.SelectedDate = (DateTime)obj_emp.obj_sqlDataReader["emp_dob"]; txt_address.Text = obj_emp.obj_sqlDataReader["emp_address"].ToString(); txt_salary.Text = obj_emp.obj_sqlDataReader["emp_salary"].ToString(); cmbx_department.SelectedValue = obj_emp.obj_sqlDataReader["depart_id"]; cmbx_city.SelectedValue = obj_emp.obj_sqlDataReader["city_id"]; txt_cnic.Text = obj_emp.obj_sqlDataReader["emp_cnic_no"].ToString(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }

    Sunday, June 28, 2015 5:59 PM

Answers

  • User760463175 posted

    Hi,

    Are you using the correct object (read vs obj_emp.obj_sqlDataReader?

    You can try replace this code:

    while (read.Read())
    {
        txt_firstName.Text = obj_emp.obj_sqlDataReader["emp_fname"].ToString();
        txt_lastName.Text = obj_emp.obj_sqlDataReader["emp_lname"].ToString();
        datepicker_joinDate.SelectedDate = (DateTime)obj_emp.obj_sqlDataReader["emp_join_date"];
        datepicker_dateOfBirth.SelectedDate = (DateTime)obj_emp.obj_sqlDataReader["emp_dob"];
        txt_address.Text = obj_emp.obj_sqlDataReader["emp_address"].ToString();
        txt_salary.Text = obj_emp.obj_sqlDataReader["emp_salary"].ToString();
        cmbx_department.SelectedValue = obj_emp.obj_sqlDataReader["depart_id"];
        cmbx_city.SelectedValue = obj_emp.obj_sqlDataReader["city_id"];
        txt_cnic.Text = obj_emp.obj_sqlDataReader["emp_cnic_no"].ToString();
    }

    with this:

    while (read.Read())
    {
        txt_firstName.Text = read["emp_fname"].ToString();
        txt_lastName.Text = read["emp_lname"].ToString();
        datepicker_joinDate.SelectedDate = (DateTime)read["emp_join_date"];
        datepicker_dateOfBirth.SelectedDate = (DateTime)read["emp_dob"];
        txt_address.Text = read["emp_address"].ToString();
        txt_salary.Text = read["emp_salary"].ToString();
        cmbx_department.SelectedValue = read["depart_id"];
        cmbx_city.SelectedValue = read["city_id"];
        txt_cnic.Text = read["emp_cnic_no"].ToString();
    }
    

    Does you query returns any NULL data from database?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 28, 2015 6:10 PM
  • User753101303 posted

    Hi,

    More likely the issue is that you are opening the "read" SqlDataReader and then you try to read column values from another obj_emp.obj_sqlDataReader which was likely never initiliazed.

    More generally, it means you are trying to use an object member while the object is null. When it happens check all objects found on the line where it happens to find which one is null....

    BTW : MessageBox.Show shows an error message on the server. It may appear to work on your dev machine as the client and the server machine are the same but it won't work once deployed to a real web server. Plus catching all exceptions and showing the error to the user is perhaps not what you want (for example here you are showing a programming error to users and they can't do anything about it).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 29, 2015 6:46 AM

All replies

  • User760463175 posted

    Hi,

    Are you using the correct object (read vs obj_emp.obj_sqlDataReader?

    You can try replace this code:

    while (read.Read())
    {
        txt_firstName.Text = obj_emp.obj_sqlDataReader["emp_fname"].ToString();
        txt_lastName.Text = obj_emp.obj_sqlDataReader["emp_lname"].ToString();
        datepicker_joinDate.SelectedDate = (DateTime)obj_emp.obj_sqlDataReader["emp_join_date"];
        datepicker_dateOfBirth.SelectedDate = (DateTime)obj_emp.obj_sqlDataReader["emp_dob"];
        txt_address.Text = obj_emp.obj_sqlDataReader["emp_address"].ToString();
        txt_salary.Text = obj_emp.obj_sqlDataReader["emp_salary"].ToString();
        cmbx_department.SelectedValue = obj_emp.obj_sqlDataReader["depart_id"];
        cmbx_city.SelectedValue = obj_emp.obj_sqlDataReader["city_id"];
        txt_cnic.Text = obj_emp.obj_sqlDataReader["emp_cnic_no"].ToString();
    }

    with this:

    while (read.Read())
    {
        txt_firstName.Text = read["emp_fname"].ToString();
        txt_lastName.Text = read["emp_lname"].ToString();
        datepicker_joinDate.SelectedDate = (DateTime)read["emp_join_date"];
        datepicker_dateOfBirth.SelectedDate = (DateTime)read["emp_dob"];
        txt_address.Text = read["emp_address"].ToString();
        txt_salary.Text = read["emp_salary"].ToString();
        cmbx_department.SelectedValue = read["depart_id"];
        cmbx_city.SelectedValue = read["city_id"];
        txt_cnic.Text = read["emp_cnic_no"].ToString();
    }
    

    Does you query returns any NULL data from database?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 28, 2015 6:10 PM
  • User-824199218 posted

    Which line of code gives the error?  Which object reference on that line is null?

    (Don't guess, use the debugger)

    Sunday, June 28, 2015 7:07 PM
  • User753101303 posted

    Hi,

    More likely the issue is that you are opening the "read" SqlDataReader and then you try to read column values from another obj_emp.obj_sqlDataReader which was likely never initiliazed.

    More generally, it means you are trying to use an object member while the object is null. When it happens check all objects found on the line where it happens to find which one is null....

    BTW : MessageBox.Show shows an error message on the server. It may appear to work on your dev machine as the client and the server machine are the same but it won't work once deployed to a real web server. Plus catching all exceptions and showing the error to the user is perhaps not what you want (for example here you are showing a programming error to users and they can't do anything about it).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 29, 2015 6:46 AM