1

i have a little problem here: i have a combobox that gets its values from column in database and i use it to enter data back to another palce in database like

comboBox2.DataSource = ds1.Tables[0];
comboBox2.DisplayMember = "DoctorName";
comboBox2.ValueMember = "DoctorCode";
comboBox2.BindingContext = this.BindingContext;

this fill the combobox with the name of doctors and the value will be the code of doctor then

 SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\administrator\documents\visual studio 2010\Projects\Clinic\Clinic\Clinc.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        con.Open();
        SqlCommand cmd1 = new SqlCommand("SELECT   Doctors.DoctorCode, Doctors.DoctorName, SessionReservations.SessionCode, SessionReservations.PatientCode, SessionReservations.ExaminationCode, SessionReservations.DoctorCode AS Expr1, SessionReservations.SessionMonth, SessionReservations.SessionYear   FROM  Doctors INNER JOIN   SessionReservations ON Doctors.DoctorCode = SessionReservations.DoctorCode    WHERE  (Doctors.DoctorCode = @DoctorCode) AND (SessionReservations.SessionMonth = @month) AND (SessionReservations.SessionYear = @year)", con);
        SqlDataAdapter da2 = new SqlDataAdapter(cmd1);
        DataSet ds2 = new DataSet();

        try
        {

            da2.InsertCommand.Parameters.Add("@DoctorCode", SqlDbType.Int).Value = Convert.ToInt32(comboBox2.SelectedValue);
            da2.InsertCommand.Parameters.Add("@month", SqlDbType.Int).Value = comboBox1.SelectedValue;
            da2.InsertCommand.Parameters.Add("@year", SqlDbType.Int).Value = textBox2.Text;

            da2.Fill(ds2);
            cmd1.ExecuteReader();
            con.Close();
}

this code is for selecting specific rows and the select statment is working right in sql manager but while running it gives error that

"System.NullReferenceException: Object reference not set to an instance of an object. at Clinic.DoctorMoneyCall.button1_Click(Object sender, EventArgs e) in C:\Users\Administrator\documents\visual studio 2010\Projects\Clinic\Clinic\DoctorMoneyCall.cs:line 45"

i just don't understand what's going wrong

3
  • @fenonoga - This is just a null reference problem. This isn't unique to the running a SQL query. Commented Nov 26, 2012 at 14:34
  • try debuging and see if your checkboxes and text box have any value to pass as a parameter Commented Nov 26, 2012 at 14:36
  • @MarcGravell line 45 is where it adds the doctor code da2.InsertCommand.Parameters.Add("@DoctorCode", SqlDbType.Int).Value = Convert.ToInt32(comboBox2.SelectedValue); Commented Nov 26, 2012 at 15:39

3 Answers 3

2

It seems like you are trying to run a select command butt you are adding your parameters to the insert command.

        da2.SelectCommand.Parameters.Add("@DoctorCode", SqlDbType.Int).Value = Convert.ToInt32(comboBox2.SelectedValue);
        da2.SelectCommand.Parameters.Add("@month", SqlDbType.Int).Value = comboBox1.SelectedValue;
        da2.SelectCommand.Parameters.Add("@year", SqlDbType.Int).Value = textBox2.Text;
Sign up to request clarification or add additional context in comments.

4 Comments

yeah i want select different thing each time, so i have to make variables in the query statment
but you added them to the Insert command, not the select command. SQL commands have 4 states: Select, Update, Insert, Delete. it comes from the SQL querys you can run. I changed your "insertcommand"'s to "selectCommand"'s
oh my god thank you very much, i feel stupid actually trying for days to know where the mistake, very stupid thing, i have to be more attention again thank you
no prob! its often something simple. happened to me recently too.
0

The error message means that one of the objects you used was null. Is the con object correctly initialized?

1 Comment

yes the connection is working right and i have displayed the values of my textboxes and comboboxes in labels to be sure they working right and they do
0

It seems like you're passing a variable that is null in line 45 of that document. Make sure all values are non-null.

1 Comment

This doesn't make a great deal of sense. What he is passing couldn't be null since its the contents of a control that exists on the form and both of those values have a default value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.