i'm developing a program for a cachier using c#. The program using ms access for the database. for somehow, the database does not enterd into the table. where is my problem? here is the code:
private void buttonCloseCart_Click(object sender, EventArgs e) { connect.ConnectionString = connString; connect.Open(); OleDbCommand cmd1 = new OleDbCommand("INSERT INTO ReceiptItem (ItemID, Quantity, UnitPrice) VALUES (@itemID, @temp_item_quantity, @temp_item_price)", connect); cmd1.Parameters.Add("@temp_item", OleDbType.Char, 20); cmd1.Parameters.Add("@temp_item_quantity", OleDbType.Integer, 20); cmd1.Parameters.Add("@temp_item_price", OleDbType.Double, 20); cmd1.Parameters.Add("@cart_sum", OleDbType.Double, 20); cmd1.Parameters.Add("@itemID", OleDbType.Integer, 20);
for (int i = 0; i < baught_items.Count; i++)
{
/*Set temporary reference to the objects located on the List*/
string temp_item = baught_items[i].ToString();
int temp_item_quantity = baught_items_quantity[i];
double temp_item_price = baught_items_price[i];
double temp_total_item_price = total_items_price[i];
/*Set the connection to the CachierPro DataBase*/
OleDbConnection myconnection = new OleDbConnection(connect.ConnectionString);
myconnection.Open();
//OleDbConnection myconnection1 = new OleDbConnection(connect.ConnectionString);
//myconnection1.Open();
/*********************************************/
/*This Action get the Item Name directly from the database according to the item name in the cart*/
//OleDbConnection dataConnection = new OleDbConnection();
//dataConnection.ConnectionString = connString;
//dataConnection.Open();
//OleDbConnection oledbConn = new OleDbConnection(connString);
//oledbConn.Open();
//OleDbCommand myCommand1 = new OleDbCommand();
//myCommand1.Connection = myconnection1;
//myCommand1.Connection.Close();
OleDbCommand cmd = new OleDbCommand("SELECT [ItemID] FROM ItemsList WHERE [ItemName] = " + "'"+temp_item +"'" , connect);
OleDbDataReader dataReader = cmd.ExecuteReader();
if (dataReader.Read()) { }
int itemID = dataReader.GetInt32(0);
/********************************************/
/*Initialize the Command ment for the Query*/
OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = myconnection;
//myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
//dataConnection.Close();
//oledbConn.Close();
myconnection.Close();
dataReader.Close();
/*Enter the first query*/
if (connect.State == ConnectionState.Open)
{
try
{ int addedCount = cmd.ExecuteNonQuery(); }
catch (Exception expe)
{ MessageBox.Show(expe.Message); connect.Close(); }
}
else
{
MessageBox.Show("Connection Failed");
}
}
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand("SELECT * FROM ReceiptItem", connect);
DataTable dt = new DataTable();
da.Fill(dt);
textBoxCurrentCartSumTXT.Clear();
textBoxPricePerOneTXT.Clear();
textBoxQuantityTXT.Clear();
textBoxSumForCurrentItemTXT.Clear();
connect.Close();
}