|
Hi again,
here are a few comments:
1.
I'm puzzled by your code.
AFAICT it should not even compile, as the "result" list is part of your DtposMDIParentSystem form, and not visible to your UserControl. How can it accept the if (result.Count > 0) line, it does not know about result??
2.
I've never used DbReader.GetValues, nor created an array of Object, like you do. I would rather have a struct or class (say FoodItem) that represents one row of your database table, and then create instances and fill them, one field at a time, using something like:
List<FoodItem> foodItems=new List<FoodItem>();
while (reader.Read()) {
FoodItem fi=new FoodItem();
fi.Name=reader["Name"];
fi.Calories=int.Parse(reader["Calories"]);
...
foodItems.Add(fi);
}
The advantage is all is fully typed, and all necessary conversions/parse operations are explicit.
3.
I would never create buttons one way (say with Visual Designer), and then try and match them up with a data structure that I obtain in another way (by reading a database). I'd rather just read the database and create the buttons (or whatever Controls are most suited) at run-time, so adding/removing a few FoodItems in the database is acceptable to the application as is.
FYI: Visual Designer stores all its stuff as regular C# code in a separate file, have a look; you can write similar code yourself, and have it execute when appropriate.
|
|
|
|
|
Hi Luc again
I got that part of your code so far is working but im having another problem at my
cmdOlives_Click event button...
private void cmdOlives_Click(object sender, EventArgs e)
{
if (menuItems.Count > 0)
{
System.Windows.Forms.ListViewItem newItem = new System.Windows.Forms.ListViewItem(menuItems[1]);
for (int i = 1; i < menuItems[1].Length; i++)
{
newItem.SubItems.Add(menuItems[i]);
}
TableOrderListView.Items.Add(newItem);
}
}
|
|
|
|
|
LAPEC wrote: im having another problem
if so, describe what you want it to do and what it actually does. Formulating accurately is what programming is all about, and it also is the way to get useful answers around here.
|
|
|
|
|
Hi Luc
Ok I will try to explain based on my code.
- 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();
OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposDatabase.accdb;");
OleDbCommand aCommand = new OleDbCommand("SELECT * FROM Food", aConnection);
try
{
aConnection.Open();
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);
}
}
- Clic 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
like so
ListView
_________________________________________
Num Name Price
_________________________________________
1 Olives £2.95
1 Soup £4.95
. . .
. . .
. . .
_________________________________________
I hope i've explained so you could understand what im trying to do...
kind regards
Roni
|
|
|
|
|
If you want a button to do something, you have to provide and connect a Click handler. I haven't seen any.
I don't know what your background is; your code is half there, and a bit bizarre at times. I think you'd be best served by a more fundamental approach. I suggest you select, buy and study an introductory book on C#; it will teach you the basics of the language, introduce you to programming in general, and do so in a systematic and meticulous way. And then I advice you to read some articles, there are lots of excellent ones here on CodeProject. Look for the ones with a high rating (above 4), and look how they handle things.
|
|
|
|
|
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);
}
}
|
|
|
|
|
Please say what the problem is.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I have created a working version of what I think you are trying to do. It does not use the same structures (List<> etc.) as you since I think that is not the right way to do things.
Would you like to see it?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Henry
I just came back from work.
If its not much to truble yes I would like to see it please
Kind regards
Roni
|
|
|
|
|
Look out for an email from hfam at live stop co stop uk.
I'll attach the zipped solution.
In it I am using SqlConnection, SqlDataAdapter etc. If you do not have SqlServer then change those to OleDbWhatevers and it should be OK.
The database structure I have used is designed for simplicity and would probably not be suitable for a 'real' environment.
There are two tables (Starter and Main) and both have the same structure.
Field: ItemID - int identity (autoincrement)
Field: ItemName - nvarchar(50)
Field: ItemPrice - Money
Give a shout if there are any problems with it.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
hi Henry
what email I should look and where is this hfam at live stop co stop uk.
roni
|
|
|
|
|
My most recent message makes this unnecessary.
But for future reference, it is a bad idea to post your email address in a public forum like this one because nasty evil people have bots scouring the internet for valid email addresses so that they can use them for naughty things.
So what I typed was a way of giving an email address without actually showing what it is.
So if my Email was bum@someisp.com I would tell you it was bum at someisp stop com
Geddit?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I've just realized that my previous idea (Email) won't work, Can't add attachments in CP Email.
So I've uploaded it to AddItemsToListView.zip[^] you can download it from there.
Good luck.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Henry
I've downloaded the zip file. thank you ever so much..
I will have to look it up tomorow, cos its getting late here and I'm working tomorow...
But in the mean time I will let you know if im facing any problem...
Kind regards
Roni
|
|
|
|
|
Hi Henry
I have managed to sort it out the zip file that you sendit and its working very well so far,
but I'm facing a bit problem with my database...
The problem I'm facing is that on my database I have 2 tables:
Food Table - with Field Names - Food_ID(Number), Food_Name(Text), Food_Type(Text), Food_Price(Currency)
Drink Table - with Field Names - Drink_ID(Number), Drink_Name(Text), Drink_Type(Text), Drink_Price(Currency)
Now what I'm concern is that what happens when I choose a different type of Food or Drink...
Kind regards
Roni
|
|
|
|
|
I'll have a think and get back to you tomorrow.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
|
I have an idea that might help but it depends on what purpose your Food_Type field serves. Does it signify that something is a starter, main etc. or is it something like meat, dairy etc.?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Henry
Well basically the Food_Type field on the Food table represents
such as
-Starter
-Main
-Dessert
-Side Order
-Salads
-etc. etc.
|
|
|
|
|
Good!
Once again if I am correct in assuming that what you are designing will not be used for entering data into the main tables but simply for taking orders or similar functions.
You can keep the DataSet and DataTable structure I used in my example. All that needs to change are the Sql SELECT statements, like this:
string starterSql = @"SELECT * FROM Food WHERE Food_Type = 'Starter'";
string mainSql = @"SELECT * FROM Food WHERE Food_Type = 'Main'";
using (SqlConnection conn = new SqlConnection(connString))
{
try
{
conn.Open();
SqlDataAdapter starterDa = new SqlDataAdapter(starterSql, conn);
starterDa.Fill(menuDS, "Starter");
SqlDataAdapter mainDa = new SqlDataAdapter(mainSql, conn);
mainDa.Fill(menuDS, "Main");
SqlDataAdapter dessertDa = new SqlDataAdapter(dessertSql, conn);
dessertDa.Fill(menuDS, "Dessert");
starterTable = menuDS.Tables["Starter"];
mainTable = menuDS.Tables["Main"];
dessertTable = menuDS.Tables["Dessert"];
}
catch (Exception e)
{
Console.WriteLine("Error: " + e);
}
}
One thing I will say is that you should not press ahead with designing all your user controls just yet. For now just have, maybe, two for testing purposes. The reason for this is that in reality you will not know what buttons will be required until run-time and the buttons will therefore need to be created and added dynamically depending on what items are in the Food database at that time. These things change, some items are dropped and others added. So what should happen is that there should only be one UserControl exactly the same as the one in my example but without the Buttons. When needed, a new one should be created by adding the buttons and hooking them up to the ItemSelected Event.
When you are ready for that I will help you, if you need it.
Good luck.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Is it neccesary to use LIKE clouse
|
|
|
|
|
I don't think so. Try without first and only use it if there are problems.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Henry
I have tried that but its not working.
Now another problem is rising.
When I run my application and when I click Starters Button it does work adding items to list view (such as olives, soup, etc etc) also it works for Mains button, but HERE IS THE THING PROBLEM!
If I wanted to add only the Mains to my listview (without adding any starters) it gives me this error.
DataMember property 'Food_Name' cannot be found on the DataSource.
I was gonna ask you something else, but I feel a bit Ridiculous...
Kind regards
Roni
|
|
|
|
|
Just to let you know I've got your message.
I no longer have Access on my system and I want to make sure that I give you correct statements, so I am writing a little application to enable me to create an Access Database.
Will get back to you shortly.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|