我100%确定我的listview有数据。我的程序将从我的列表视图的第0列获取ID,连接到数据库,并使用ID作为我的参考将从我的数据库获取数据并将其显示到我的文本框/组合框。因此,当我从列表视图中单击某个项目时,会出现一个异常,并且我的所有文本框以及我的组合框都是空的。我是c#和编程的新手,非常感谢任何帮助。
using System;
using System.Data.OleDb;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
private string button = null;
private string carID;
public Form1()
{
InitializeComponent();
textDateReg.Text = DateTime.Now.ToString();
lvRefresh();
}
private void lvRefresh()
{
listView1.Items.Clear();
listView1.View = View.Details;
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\genesis\\Documents\\Database2.accdb";
con.Open();
OleDbCommand cmd = new OleDbCommand("Select ID, PlateNo from Cars", con);
OleDbDataReader cmdrdr = cmd.ExecuteReader();
if (cmdrdr.HasRows)
{
while (cmdrdr.Read())
{
ListViewItem list = new ListViewItem(cmdrdr["ID"].ToString());
list.SubItems.Add(cmdrdr["PlateNo"].ToString());
listView1.Items.Add(list);
}
}
con.Close();
cmdrdr.Close();
cmd.Dispose();
}
private void buttonNew_Click(object sender, EventArgs e)
{
button = "new";
buttonSub.Enabled = true;
panel1.Enabled = true;
}
private void textBox7_MouseClick(object sender, MouseEventArgs e)
{
textBox7.Text = null;
}
private void buttonSub_Click(object sender, EventArgs e)
{
switch (button)
{
case "new":
DialogResult dialogResult = MessageBox.Show("Are you sure you want to Register?", "Confirm", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
string Adrs = this.textADRS.Text;
string Fname = this.textFN.Text;
string Mname = this.textMN.Text;
string Lname = this.textLN.Text;
string Age = this.textAGE.Text;
string RegDate = this.textDateReg.Text;
string Gender = comboGender.SelectedItem.ToString();
string Phone = this.textPHONE.Text;
string Color = this.textColor.Text;
string Type = this.comboType.SelectedItem.ToString();
string Brand = this.textBrand.Text;
string Model = this.textModel.Text;
string PlateNo = this.textPlateNo.Text;
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\genesis\\Documents\\Database2.accdb";
OleDbCommand cmd = new OleDbCommand("Insert into Drivers (FirstName,MiddleName,LastName,Address,Age,PhoneNo,Gender,RegDate) Values (@FirstName,@MidName,@LastName,@Address,@Age,@Phone,@Gender,@RegDate)", con);
con.Open();
cmd.Parameters.Add(new OleDbParameter("@FirstName", Fname));
cmd.Parameters.Add(new OleDbParameter("@MidName", Mname));
cmd.Parameters.Add(new OleDbParameter("@LastName", Lname));
cmd.Parameters.Add(new OleDbParameter("@Address", Adrs));
cmd.Parameters.Add(new OleDbParameter("@Age", Age));
cmd.Parameters.Add(new OleDbParameter("@Phone", Phone));
cmd.Parameters.Add(new OleDbParameter("@Gender", Gender));
cmd.Parameters.Add(new OleDbParameter("@RegDate", RegDate));
cmd.ExecuteNonQuery();
cmd.Dispose();
cmd = new OleDbCommand("Insert into Cars(Color, Brand, Model, Type, PlateNo) Values(@Color, @Brand, @Model, @Type, @PlateNo)", con);
cmd.Parameters.Add(new OleDbParameter("@Color", Color));
cmd.Parameters.Add(new OleDbParameter("@Brand", Brand));
cmd.Parameters.Add(new OleDbParameter("@Model", Model));
cmd.Parameters.Add(new OleDbParameter("@Type", Type));
cmd.Parameters.Add(new OleDbParameter("@PlateNo", PlateNo));
cmd.ExecuteNonQuery();
con.Close();
cmd.Dispose();
MessageBox.Show("Record Submitted", "Nice!");
lvRefresh(); //refresh listview
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
break;
case "del":
dialogResult = MessageBox.Show("Are you sure you want to Register?", "Confirm", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
string Adrs = this.textADRS.Text;
string Fname = this.textFN.Text;
string Mname = this.textMN.Text;
string Lname = this.textLN.Text;
string Age = this.textAGE.Text;
string RegDate = this.textDateReg.Text;
string Gender = comboGender.SelectedItem.ToString();
string Phone = this.textPHONE.Text;
string Color = this.textColor.Text;
string Type = this.comboType.SelectedItem.ToString();
string Brand = this.textBrand.Text;
string Model = this.textModel.Text;
string PlateNo = this.textPlateNo.Text;
try
{
}
catch
{
}
}
break;
case "edit":
break;
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
panel1.Enabled = true;//panel that contains my textboxes/comboboxes
buttonDel.Enabled = true;
try
{
ListViewItem item = listView1.SelectedItems[0];
carID = item.Text;
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\genesis\\Documents\\Database2.accdb";
OleDbCommand cmd = new OleDbCommand("Select * from Drivers where ID = @ID", con);
con.Open();
cmd.Parameters.AddWithValue("@ID", carID);
OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.HasRows)
{
textFN.Text = rdr["FirstName"].ToString();
textMN.Text = rdr["MiddleName"].ToString();
textLN.Text = rdr["LastName"].ToString();
textADRS.Text = rdr["Address"].ToString();
textPHONE.Text = rdr["PhoneNo"].ToString();
textDateReg.Text = rdr["RegDate"].ToString();
textAGE.Text = rdr["Age"].ToString();
comboGender.Text = rdr["Gender"].ToString();
if (!rdr.HasRows)
{
rdr.Close();
cmd.Dispose();
OleDbCommand cmdc = new OleDbCommand("Select * from Cars where ID = @ID", con);
cmdc.Parameters.AddWithValue("@ID", carID);
OleDbDataReader rdrc = cmdc.ExecuteReader();
while (rdrc.HasRows)
{
textColor.Text = rdr["Color"].ToString();
comboType.Text = rdr["Type"].ToString();
textBrand.Text = rdr["Brand"].ToString();
textModel.Text = rdr["Model"].ToString();
textPlateNo.Text = rdr["PlateNo"].ToString();
}
rdrc.Close();
cmdc.Dispose();
con.Close();
}
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
}
}