1

ここで少し問題があります。データベースの列から値を取得するコンボボックスがあり、それを使用してデータベースの別の場所にデータを入力します。

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 "

何が悪いのかわからない

4

3 に答える 3

2

selectコマンドを実行しようとしているようですが、挿入コマンドにパラメーターを追加しています。

        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

そのドキュメントの45行目でnullの変数を渡しているようです。すべての値がnull以外であることを確認してください。

于 2012-11-26T14:31:26.623 に答える
0

エラーメッセージは、使用したオブジェクトの1つがnullであることを意味します。conオブジェクトは正しく初期化されていますか?

于 2012-11-26T14:30:54.743 に答える