I am trying to do an Ordering System where you can put many Products in one order. I have very little knowledge about this and this is where i am now
There are 3 tables, Product table,Order table and the Order-products table. I really don't know if this is right as i am beginner especially on foreign keys.
What I want to achieve is you can order many products and put that products into one "OrderID" like this example in pic below.
This are my only codes. Sorry but i am really lost at this.
public Form1()
{
InitializeComponent();
fillCart();
}
private void fillCart()
{
dgvCart.ColumnCount = 3;
dgvCart.Columns[0].Name = "ProductID";
dgvCart.Columns[1].Name = "ProductName";
dgvCart.Columns[2].Name = "Quantity";
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//dgvproducts
}
private void Form1_Load(object sender, EventArgs e)
{
crud.FillDataGrid("Select * from Products", ref dgvProducts);
crud.FillDataGrid("Select * from Orders", ref dgvOrder);
crud.FillDataGrid("Select * from Orderproducts", ref dgvOrderview);
lbldate.Text = DateTime.Now.ToShortDateString();
}
private void button2_Click(object sender, EventArgs e)
{
//button add to cart
addData(dgvProducts.CurrentRow.Cells[0].Value.ToString(), dgvProducts.CurrentRow.Cells[1].Value.ToString(), txtqty.Text);
}
private void addData(string p1, string p2, string p3)
{
String[] row = { p1, p2, p3 };
dgvCart.Rows.Add(row);
}
private void button1_Click(object sender, EventArgs e)
{
//button insert
}
Thank you very much and i hope someone can help me with my problem.
Method use for filling datagridview from SQLserver 2008:
public crud()
{
cnString = "Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True";
cn = new SqlConnection(cnString);
}
public void FillDataGrid(string sql, ref ns1.BunifuCustomDataGrid dg)
{
try
{
DataSet ds = new DataSet();
cn.Open();
cmd = new SqlCommand(sql, cn);
adptr = new SqlDataAdapter(cmd);
adptr.Fill(ds);
dg.DataSource = "";
dg.DataSource = ds.Tables[0];
}
catch (Exception e)
{
MessageBox.Show("" + e.Message);
}
cn.Close();
}
This may not answer your question completely but consider an object orientated approach to this. It's always better in my opinion to have a strongly typed method of accessing values returned from a database, although others may disagree. Here is some pseudo code to get you started and is by no means the entire solution but should encourage you to think how you can make your code more object orientated and strongly typed. Use the same methodology to save and update tables in your database.
example
Create foreign key relationship between Order - OrderProduct over OrderID and Product - OrderProduct over Product ID.
For each Product in order insert a row into OrderProduct with OrderId
How Linq 2 SQL dataclasses look for me:
The code that goes with it: