0% found this document useful (0 votes)
7 views

Code

This code handles saving order data to database tables. It inserts or updates records in the main and detail tables depending on whether an order already exists. It loops through order items to save each one, and refreshes the UI upon completion.

Uploaded by

Jonce Brown
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Code

This code handles saving order data to database tables. It inserts or updates records in the main and detail tables depending on whether an order already exists. It loops through order items to save each one, and refreshes the UI upon completion.

Uploaded by

Jonce Brown
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

private void btnKot_Click(object sender, EventArgs e)

string qry1 = ""; //Main Table


string qry2 = ""; //Detail Table

int detailID = 0;
//int proID = 0;
if (MainID == 0)
{
qry1 = @"insert into tblMain Values
(@aDate ,@aTime,@TableName,@WaiterName,@status,@orderType,@total,@received,@change)
;
select SCOPE_IDENTITY()";

}
else
{
qry1 = @"update tblMain set status = @status,total =
@total,received = @received,change = @change where MainID = @ID) ";

// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Hashtable ht = new Hashtable();
ht.Add("@id", cid);
ht.Add("@Name", txtCategoria.Text);
//...........................................................
SqlCommand cmd1 = new SqlCommand(qry1, MainClass.con);
cmd1.Parameters.AddWithValue("@ID", MainID);
cmd1.Parameters.AddWithValue("aDate",
Convert.ToDateTime(DateTime.Now.Date));
cmd1.Parameters.AddWithValue("aTime",
DateTime.Now.ToShortTimeString());
cmd1.Parameters.AddWithValue("TableName", lblTable);
cmd1.Parameters.AddWithValue("WaiterName", lblWaiter);
cmd1.Parameters.AddWithValue("status", "Pendente");
cmd1.Parameters.AddWithValue("orderType", orderType);
cmd1.Parameters.AddWithValue("total",
Convert.ToDouble(lblTotal.Text));
cmd1.Parameters.AddWithValue("received", Convert.ToDouble(0));
cmd1.Parameters.AddWithValue("change", Convert.ToDouble(0));
guna2MessageDialog1.Show("Vai pra frente.");
if (MainClass.con.State == ConnectionState.Closed)
{ MainClass.con.Open(); }
if (MainID == 0) {MainID = Convert.ToInt32
(cmd1.ExecuteScalar()); } else { cmd1.ExecuteNonQuery(); }
if (MainClass.con.State == ConnectionState.Open)
{ MainClass.con.Close(); }

foreach (DataGridViewRow row in guna2DataGridView1.Rows)


{
detailID = Convert.ToInt32(row.Cells["dCatId"].Value);

if (detailID == 0)
{
qry2 = @"insert into tblDetails Values (@MainID,
@proID,@qty,@price, @amount)";
}
else
{
qry2 = @"update tblDetails set proID = @proID,qty = @qty,
price = @price,amount = @amount
where DetailID = @ID ";
}

SqlCommand cmd2 = new SqlCommand(qry2, MainClass.con);


cmd2.Parameters.AddWithValue("@ID", detailID);
cmd2.Parameters.AddWithValue("MainID", MainID);
cmd2.Parameters.AddWithValue("@proID",
Convert.ToInt32(row.Cells["dgvproID"].Value));
cmd2.Parameters.AddWithValue("@qty",
Convert.ToInt32(row.Cells["dQnt"].Value));
cmd2.Parameters.AddWithValue("@price",
Convert.ToDouble(row.Cells["dPreco"].Value));
cmd2.Parameters.AddWithValue("@amount",
Convert.ToDouble(row.Cells["dTotal"].Value));

if (MainClass.con.State == ConnectionState.Closed)
{ MainClass.con.Open(); }
cmd2.ExecuteNonQuery();
if (MainClass.con.State == ConnectionState.Open)
{ MainClass.con.Close(); }

guna2MessageDialog1.Show("Venda guardada.");
MainID = 0;
detailID = 0;
guna2DataGridView1.Rows.Clear();

lblTable.Text = "";
lblWaiter.Text = "";
lblTable.Visible = false;
lblWaiter.Visible = false;
lblTotal.Text = "0,00";
}
}
}

You might also like