المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : C# - How to call database records one-by-one and display it in ListView? [modified]



C# Programming
12-17-2010, 11:11 AM
Dear People

I'm facing a big problem...

I have created a C# Project. In this project I have a Form1 (inside this form I have a Button1 , Panel1 and ListView1 ), a reusable UserControl1 (inside this usercontrol I have 10 buttons such as: cmdOlives , cmdSoup , etc, etc ..) and also I have a Access 2007 Database (In my database I have a table created with 10 records). Oh I almost forgot, I have created a class aswell called MenuItems.cs (see code below of this class)...

What I have done so far is:
- From Form1 the Button1_Click event calls UserControl1 and displays it (inside Panel1 ) into my Form1 .

What I'm trying to achieve is this:
- The 10 buttons in UserControl1 to be linked dynamically with my Database, what I mean by dynamically is that I dont want to create for every button_click event an opening db connection, exec query, and so on , but to create one and only one SQL query (lest say like an array query) so when I click cmdOlives to get the rocord1 from database table and display it into my ListView1 , same for cmdSoup and so on...

As you can see from the code below I have created the sql query but for some reason its not displaying any data into my listview I'm missing something and I'm stuck.

If someone could help me with this it would be very greatfull...

Polite Note: I have posted this question before but no one seem to help me
I'm new to programming, and I thought people in this forum will help
other people when posting a question
----------------------------------------------------------------

Here is the code of my C# project:
Form1


public partial class DtposMDIParentSystem : Form
{

List result = new List();

public DtposMDIParentSystem()
{
InitializeComponent();

//create the database connection
OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposDatabase.accdb;");

//create the command object and store the sql query
OleDbCommand aCommand = new OleDbCommand("SELECT * FROM Food", aConnection);

try {
aConnection.Open();

//create the datareader object to connect to table
OleDbDataReader reader = aCommand.ExecuteReader();

int i = 0;
while (reader.Read())
{
result.Add(new Object[reader.FieldCount]);
reader.GetValues(result[i]);
}
reader.Close();
aConnection.Close();
}
catch (InvalidOperationException ex)
{
MessageBox.Show("Invalid Masseage = " + ex.Message);
}
}
private void DtposMDIParentSystem_Load(object sender, EventArgs e)
{
this.Panel1.******** = new System.Drawing.Point(332, 127);
this.Panel1.Name = "Panel1";
this.Panel1.Size = new System.Drawing.Size(700, 560);
}

UserControl1 userC = new UserControl();
//----------------------------------------------------------//
private void Button1_Click(object sender, EventArgs e)
{
this.Panel1.Visible = true;
this.userC.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.userC.******** = new System.Drawing.Point(0, 0);
this.userC.Size = new System.Drawing.Size(696, 556);
this.userC.Enabled = true;
this.userC.Visible = true;
this.Panel1.Controls.Add(userC);
}
}

UserControl1

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}

private void UserControl1_Load(object sender, EventArgs e)
{

}

private void cmdOlives_Click(object sender, EventArgs e)
{
if (result.Count > 0)
{
string temp = "";

for (int i = 0; i < result[1].Length; i++)
{
temp += result[1][i] + " ";
}
TableOrderListView.Items.Add(temp);
}
}

private void cmdSoup_Click(object sender, EventArgs e)
{

}
.
.
.
}
MenuItems.cs Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DtposApplication
{
public class MenuItems
{
private int itemNum = 0;

public int ItemNum
{
get {
return itemNum;
}
set {
itemNum = value;
}
}

private string itemName = string.Empty;

public string ItemName
{
get {
return itemName;
}
set {
itemName = value;
}

}

private double itemPrice = 0.0;

public double ItemPrice
{
get {
return itemPrice;
}
set {
itemPrice = value;
}
}

public void menuitemParam(int ItNo, string nam, double cost)
{
itemNum = ItNo;
itemName = nam;
itemPrice = cost;
}

public void meItem(ref MenuItems mi)
{
itemNum = mi.itemNum;
itemName = mi.itemName;
itemPrice = mi.itemPrice;
}
}
}


Thanks in advance

Kind Regards

Agron
modified on Thursday, December 16, 2010 5:07 PM