Click here to Skip to main content
15,908,618 members
Please Sign up or sign in to vote.
2.00/5 (5 votes)
See more:
Dear People

Im 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 Im 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 Im missing something and Im also stuck.

If someone could help me with this it would be very greatfull (I mean with a full sample of code)...

Here I will try to explain based on my code.

- Here is the code for MenuItems.cs class

public class MenuItems
    {
        private int itemNum = 0;
        public int ItemNum
        {
            get{return itemNum;}
            set{itemNum = value;}
        }
        private string itemName;
        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;
        }
    }

- Here is the UserControl1.cs class
- From this class I want to display database records to my listview (in Form1)...

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
//This a part that when I click this button to display the data in to my Listview...
private void cmdOlives_Click(object sender, EventArgs e)
        {
            if (menuItems.Count > 0)
            {
                System.Windows.Forms.ListViewItem newItem = new System.Windows.Forms.ListViewItem(menuItems[1].ToString());
                for (int i = 1; i < menuItems[1].ItemName.Length; i++)
                {
                    newItem.SubItems.Add(menuItems[i].ToString());
                }
                TableOrderListView.Items.Add(newItem);
            }
        }
        private void cmdSoup_Click(object sender, EventArgs e)
        {
        }
        .......
        .......
    }


- When Form1 Loads (see the code below I have created database connection)

public partial class DtposMDIParentSystem : Form
{
     List<menuitems> menuItems = new List<menuitems>();
     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();
               while (reader.Read())
               {
                   MenuItems mi = new MenuItems();
                   mi.ItemName = reader["Name"].ToString();
                   mi.ItemPrice = Double.Parse(reader["Price"].ToString());
                   menuItems.Add(mi);
               }
               reader.Close();
               aConnection.Close();
          }
          catch (InvalidOperationException ex)
          {
               MessageBox.Show("Invalid Masseage = " + ex.Message);
          }
     }
}</menuitems></menuitems>

- Click the button1 (inside Form1) calls the UserControl1 and displays it into Form1 (see the code below)
UserControl1 userC = new UserControl();
        private void cmdStarters_Click(object sender, EventArgs e)
        {
            this.StartersPanel.Visible = true;
            this.userC.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.userC.Location = new System.Drawing.Point(0, 0);
            this.userC.Size = new System.Drawing.Size(696, 556);
            this.userC.Enabled = true;
            this.userC.Visible = true;
            this.StartersPanel.Controls.Add(userC);
        }

- The UserControl1 has 10 Buttons (such as cmdOlives, cmdSoup etc. etc...)
- Now when I click on:
- cmdOlives_Click event button I want to display rocord1 (such as Olives) from database table and display it into my TableOrderListView...
- Also when I click on:
- cmdSoup_Click event button I want to display rocord2 (such as Soup) from database table and display it into my TableOrderListView... and so on
Here is how I want the data to be shown in my ListView....
ListView
_________________________________________
Num Name Price
_________________________________________
1 Olives £2.95
1 Soup £4.95
. . .
_________________________________________
I hope my explonation is clear so who ever reads my question can understand what im trying to do...
kind regards
Roni
Posted
Comments
JF2015 17-Dec-10 10:12am    
Repost. Please stop it.

1 solution

You are reading one by one record from database using datareader object instead of that fetch record in dataset and manage u r own cursor pointer to each row of dataset. U can call then one by one record from dataset on the basis of this pointer any code required feel free to ask
any query regarding mail me on garadshree25@gmail.com
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900