如何在单击事件按钮上使用数据库记录填充ListView [英] Howto populate ListView with database records on click event button
问题描述
亲爱的人们
如何在单击事件按钮上使用数据库记录填充ListView.基本上,我有一个表单,在这个表单中,我有一个按钮(称为Starters)和一个listview.在我的数据库中,有一个名为Food的表.
该表的属性是:
Dear people
How to populate a ListView with database records on click event button. Basically I have a form, and in this form I have a button (called Starters), and a listview. On my database I have a table called Food.
The attributes of this table are:
=============================================
FoodID FoodName FoodType FoodPrice
=============================================
000001 Soup Starter £4.50
------- ------ -------- -------
=============================================
我基本上想要的是,在单击事件按钮时,仅将FoodName ...显示在我的列表视图中.
这是启动程序"单击事件按钮上的代码,但是由于某种原因,它给了我一个错误.
What I basically want is, on click event button to display on my listview only the the FoodName...
Here is the code on Starter click event button, but for some reason it gives me an error.
public partial class StockIn : Form
{
private DataSet DtposMenuDS = new DataSet("DtposMenu");
private DataTable starterTable = null;
public StockIn()
{
InitializeComponent();
}
private void cmdExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void cmdStarters_Click(object sender, EventArgs e)
{
OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
connBuilder.DataSource = @"C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposMenu.accdb";
connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
connBuilder.Add("Jet OLEDB:Engine Type", "5");
// Food SQL Query
string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
{
try
{
conn.Open();
// TODO: Get Food here
OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
foodType.Value = "Starters";
foodsDa.Fill(DtposMenuDS, "Starters");
starterTable = DtposMenuDS.Tables["Starter"];
ListViewItem foodItem = new ListViewItem();
foodItem.SubItems.Add(((DataRowView)DtposMenuBS.Current)["FoodName"].ToString());
this.listViewItemsInStock.Items.Add(foodItem);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
}
}
}
任何帮助都将非常非常好....
在此先感谢
Roni
Any help it would be very very greatfull....
Thanks in advance
Roni
推荐答案
好吧,给出错误消息后,似乎至少有一个您尝试使用的值为null的对象.我建议您设置一个断点,并尝试在Visual Studio中进行调试,以找出发生异常的确切位置.
Well, given the error message, it seems there is at least one object having null value which you are trying to use. I would suggest you to put a break point and try to debug in Visual Studio to find out exactly where the exception is occurring.
我复制了代码并添加了注释:
I copied your code and added comments:
public partial class StockIn : Form
{
private DataSet DtposMenuDS = new DataSet("DtposMenu");
private DataTable starterTable = null;
public StockIn()
{
InitializeComponent();
}
private void cmdExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void cmdStarters_Click(object sender, EventArgs e)
{
OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder();
connBuilder.DataSource = @"C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposMenu.accdb";
connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
connBuilder.Add("Jet OLEDB:Engine Type", "5");
// Food SQL Query
string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType";
using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString))
{
try
{
conn.Open();
// TODO: Get Food here
OleDbCommand foodsCommand = new OleDbCommand(foodTypeSql, conn);
OleDbParameter foodType = foodsCommand.Parameters.Add("@foodType", OleDbType.VarChar, 15);
OleDbDataAdapter foodsDa = new OleDbDataAdapter(foodsCommand);
//The entry in the columns foodTpe in your table reads Starter and not Starters
// WRONG: foodType.Value = "Starters";
foodType.Value = "Starter";
// Your table is called Food
foodsDa.Fill(DtposMenuDS, "Food");
// Your table is called Food
starterTable = DtposMenuDS.Tables["Food"];
ListViewItem foodItem = new ListViewItem();
foodItem.SubItems.Add(((DataRowView)DtposMenuBS.Current)["FoodName"].ToString());
this.listViewItemsInStock.Items.Add(foodItem);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
}
}
}
希望对您有所帮助!
干杯,
曼弗雷德(Manfred)
Hope that helps!
Cheers,
Manfred
这篇关于如何在单击事件按钮上使用数据库记录填充ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!