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

4

3 回答 3

2

似乎您正在尝试运行选择命令,但您正在将参数添加到插入命令。

        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;
于 2012-11-26T15:17:34.690 回答
0

错误消息意味着您使用的对象之一为空。con 对象是否正确初始化?

于 2012-11-26T14:30:54.743 回答
0

您似乎在该文档的第 45 行传递了一个为 null 的变量。确保所有值都不为空。

于 2012-11-26T14:31:26.623 回答