我的Windows窗体上有2个ComboBox控件,名为comboBox1和comboBox2。 comboBox1中的项目是从表单的加载事件中的数据库加载的,当用户从comboBox1中选择一个项目并收到此错误时,我试图将项目添加到comboBox2。
程序或功能' SubjectCodeQuery'期望参数' @ StudentId',这是未提供的。
这是我的代码:
using System.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StudentAttendanceSystem
{
public partial class AttendanceEntry : Form
{
private string sasdbConnectionString = @"Data Source=(LocalDB)\v11.0;Initial Catalog=sasdb;Integrated Security=True;Pooling=False";
public AttendanceEntry()
{
InitializeComponent();
}
private void AttendanceEntry_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(sasdbConnectionString);
SqlCommand cmd = new SqlCommand("SELECT StudentId FROM Students", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.comboBox1.Items.Add(dr.GetInt32(0).ToString());
}
dr.Close();
conn.Close();
toolStripStatusLabel1.Text = "";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(sasdbConnectionString);
SqlCommand cmd = new SqlCommand("SubjectCodeQuery", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StudentId", comboBox1.SelectedValue);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.comboBox2.Items.Add(dr.GetInt32(0).ToString());
}
conn.Close();
}
}
}