Click here to Skip to main content
15,888,984 members
Home / Discussions / C#
   

C#

 
QuestionTrying to create a new array Pin
turbosupramk316-Dec-10 14:46
turbosupramk316-Dec-10 14:46 
AnswerRe: Trying to create a new array Pin
PIEBALDconsult16-Dec-10 15:25
mvePIEBALDconsult16-Dec-10 15:25 
GeneralRe: Trying to create a new array Pin
turbosupramk316-Dec-10 15:28
turbosupramk316-Dec-10 15:28 
GeneralRe: Trying to create a new array Pin
turbosupramk316-Dec-10 15:33
turbosupramk316-Dec-10 15:33 
GeneralRe: Trying to create a new array [modified] Pin
turbosupramk316-Dec-10 15:42
turbosupramk316-Dec-10 15:42 
GeneralRe: Trying to create a new array Pin
PIEBALDconsult16-Dec-10 16:37
mvePIEBALDconsult16-Dec-10 16:37 
AnswerRe: Trying to create a new array Pin
Hiren solanki16-Dec-10 19:07
Hiren solanki16-Dec-10 19:07 
QuestionC# - How to call database records one-by-one and display it in ListView? [modified] Pin
LAPEC16-Dec-10 10:50
LAPEC16-Dec-10 10:50 
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<Object[]> result = new List<Object[]>();
        
     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.Location = 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.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.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

AnswerRe: C# - How to call database records one-by-one and display it in ListView? Pin
Luc Pattyn16-Dec-10 11:06
sitebuilderLuc Pattyn16-Dec-10 11:06 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC16-Dec-10 11:12
LAPEC16-Dec-10 11:12 
AnswerRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute16-Dec-10 11:30
Henry Minute16-Dec-10 11:30 
AnswerRe: C# - How to call database records one-by-one and display it in ListView? Pin
Luc Pattyn16-Dec-10 11:36
sitebuilderLuc Pattyn16-Dec-10 11:36 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC16-Dec-10 12:21
LAPEC16-Dec-10 12:21 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Luc Pattyn16-Dec-10 12:26
sitebuilderLuc Pattyn16-Dec-10 12:26 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC16-Dec-10 12:59
LAPEC16-Dec-10 12:59 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Luc Pattyn16-Dec-10 13:26
sitebuilderLuc Pattyn16-Dec-10 13:26 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC16-Dec-10 13:32
LAPEC16-Dec-10 13:32 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute16-Dec-10 12:27
Henry Minute16-Dec-10 12:27 
AnswerRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute17-Dec-10 9:48
Henry Minute17-Dec-10 9:48 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC17-Dec-10 14:30
LAPEC17-Dec-10 14:30 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute17-Dec-10 14:40
Henry Minute17-Dec-10 14:40 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC17-Dec-10 14:56
LAPEC17-Dec-10 14:56 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute17-Dec-10 15:03
Henry Minute17-Dec-10 15:03 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
Henry Minute17-Dec-10 14:56
Henry Minute17-Dec-10 14:56 
GeneralRe: C# - How to call database records one-by-one and display it in ListView? Pin
LAPEC17-Dec-10 15:11
LAPEC17-Dec-10 15:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.