- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- using (SqlConnection conn = new SqlConnection(@"Data source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\DB.mdf;
- integrated Security=True;User Instance=True"))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = "select * from Province";
- using (SqlDataReader dataReader = cmd.ExecuteReader())
- {
- while (dataReader.Read())
- {
- Province item = new Province();
- item.Name = dataReader.GetString(dataReader.GetOrdinal("proName"));
- item.Id = dataReader.GetInt32(dataReader.GetOrdinal("proId"));
- cmbProvince.Items.Add(item.Name);
- }
- }
- }
- }
- }
- private void cmbProvince_SelectedIndexChanged(object sender, EventArgs e)
- {
- Province item = (Province)cmbProvince.SelectedItem; //此句报错
- int proid = item.Id;
- using (SqlConnection conn = new SqlConnection(@"Data source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\DB.mdf;
- integrated Security=True;User Instance=True"))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = "select * from City where proId=proid";
- cmd.Parameters.Add(new SqlParameter("proId", proid));
- using (SqlDataReader dataReader = cmd.ExecuteReader())
- {
- while (dataReader.Read())
- {
- string ctName = dataReader.GetString(dataReader.GetOrdinal("cityName"));
- cmbCity.Items.Add(ctName);
- }
- }
- }
- }
- }
- }
- class Province
- {
- public string Name { get; set; }
- public int Id { get; set; }
- }
复制代码 跟着视频一块做的,但到了这里却报错了。明明是跟着老师一步一步做的可为什么他的没事而我的却报错了呢?
报错如下:
在线坐等求解~~~~~~~~~~~~~~~~~
|
|