Hey all,
I have my code to I can create a new receipt NEXT I am supposed to add my list of products to another table linked by my receiptId But my database automatically creates the receiptId and the only way to read the receipt from the database is if I have the receiptId So how would I get the receiptId so I could insert my list of names to another table having them linked together?
private void BtnConfirmCart_Click(object sender, RoutedEventArgs e)
{
if (listOfP.Count != 0)
{
string msg = "Are you sure you CONFIRM your cart?";
MessageBoxResult boxResult = MessageBox.Show(msg, "Confirmation", MessageBoxButton.YesNo);
if(boxResult == MessageBoxResult.Yes)
{
try
{
date = DateTime.Now;
decimal total = receiptCart.ReceiptTotal;
int userid = customer.UserId;
Extras.CreateReceipt(userid, total, date);
}
catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); }
Window WindowReceipt = new ReceiptWindow();
WindowReceipt.Show();
Close();
}
}
else
{
BtnGoBack.Visibility = Visibility.Hidden;
MessageBox.Show("No items in your cart.", "Add items.");
}
}
public static int CreateReceipt(int userid, decimal rtotal, DateTime rdate)
{
int result = -1;
using(SqlConnection connection = ConnectionString.GetSqlConnection())
{
string sqlCreate = "INSERT INTO Orders(UserId, ReceiptDate, ReceiptTotal) " +
"VALUES(@uid, @date, @rtotal)";
using(SqlCommand cmdCreate = new SqlCommand(sqlCreate, connection))
{
cmdCreate.Parameters.AddWithValue("@uid", userid);
cmdCreate.Parameters.AddWithValue("@date", rdate);
cmdCreate.Parameters.AddWithValue("rtotal", rtotal);
try
{
connection.Open();
result = Convert.ToInt32(cmdCreate.ExecuteScalar());
}
catch(Exception ex) { throw ex; }
}
}
return result;
}
public static Receipt ReadReceipt(int receiptID)
{
Receipt receipt = new Receipt();
using(SqlConnection connection = ConnectionString.GetSqlConnection())
{
string sqlRead = "SELECT * FROM Receipts WHERE ReceiptId = @rid";
using(SqlCommand cmdRead = new SqlCommand(sqlRead, connection))
{
cmdRead.Parameters.AddWithValue("@rid", receiptID);
connection.Open();
using(SqlDataReader reader = cmdRead.ExecuteReader())
{
if(reader != null)
{
while (reader.Read())
{
receipt.ReceiptID = Convert.ToInt32(reader["ReceiptId"].ToString());
receipt.UserId = Convert.ToInt32(reader["UserId"].ToString());
receipt.ReceiptDate = Convert.ToDateTime(reader["ReceiptDate"].ToString());
receipt.ReceiptTotal = Convert.ToDecimal(reader["ReceiptTotal"].ToString());
}
}
}
}
}
return receipt;
}
public static List<ReturnReceiptProductList> GetReceiptProductList(int rid)
{
List<ReturnReceiptProductList> productList = new List<ReturnReceiptProductList>();
ReturnReceiptProductList returnReceipt = new ReturnReceiptProductList();
using (SqlConnection connection = ConnectionString.GetSqlConnection())
{
string sqlList = "SELECT * FROM OrdersList WHERE ReceiptId = @rid";
using (SqlCommand cmdList = new SqlCommand(sqlList, connection))
{
cmdList.Parameters.AddWithValue("@rid", rid);
connection.Open();
using (SqlDataReader reader = cmdList.ExecuteReader())
{
if (reader != null)
{
while (reader.Read())
{
Product product = new Product();
Receipt receipt = new Receipt();
receipt.ReceiptID = Convert.ToInt32(reader["ReceiptId"].ToString());
receipt.UserId = Convert.ToInt32(reader["UserId"].ToString());
product.ProductName = reader["ProductName"].ToString();
product.ProductQuantity = Convert.ToInt32(reader["ProductQuantity"].ToString());
product.ProductPrice = Convert.ToDecimal(reader["ProductPrice"].ToString());
product.ProductTax = Convert.ToDecimal(reader["ProductTax"].ToString());
returnReceipt = new ReturnReceiptProductList(product, receipt);
productList.Add(returnReceipt);
}
}
}
}
}
return productList;
What I have tried:
inserting data but to link another table i need the id inserted in the same window and i cannot read from table because i need that specific id inserted...