ここで少し問題があります。データベースの列から値を取得するコンボボックスがあり、それを使用してデータベースの別の場所にデータを入力します。
comboBox2.DataSource = ds1.Tables[0];
comboBox2.DisplayMember = "DoctorName";
comboBox2.ValueMember = "DoctorCode";
comboBox2.BindingContext = this.BindingContext;
これにより、コンボボックスに医師の名前が入力され、値は医師のコードになります。
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();
}
このコードは特定の行を選択するためのものであり、selectステートメントはSQLマネージャーで正しく機能していますが、実行中にエラーが発生します。
"System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。C:\ Users \ Administrator \ document \ visual studio 2010 \ Projects \ Clinic \ Clinic \ DoctorMoneyCallのClinic.DoctorMoneyCall.button1_Click(Object sender、EventArgs e)にあります.cs:line 45 "
何が悪いのかわからない