-2

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();


        }
Oron
  • 43
  • 7
  • "the program creats new table for each receipts" You may want to think about this and maybe read a book about database design. This sounds like a "bad idea" (tm). – nvoigt Mar 30 '14 at 10:58
  • o.k, than what is your solution? enter all the receipts in one single table? u should know that each receipts include several fields such as ItemName, ItemPrice, Quantity and CartSum... any advice my friend? – Oron Mar 30 '14 at 11:02
  • Yes, read a good book on database design. Storing multiple fields per record is *exactly* what tables are for and explaining database design is really out of scope for this Q&A site. – nvoigt Mar 30 '14 at 11:03
  • so meanwhile, whats the syntax for getting the amount of tables in the db? – Oron Mar 30 '14 at 11:04
  • Yes, Receipts table with receipt number in one table. Receipt number/ item number/quantity/price in another table. Items with Item number/ Item Name in another table. – ray Mar 30 '14 at 11:05
  • btw, regard to your advice, do you recommand to put each receipts (including all the items) in one ItemID? – Oron Mar 30 '14 at 11:06
  • http://stackoverflow.com/questions/2629211/can-we-list-all-tables-in-msaccess-database-using-sql – ray Mar 30 '14 at 11:07
  • again please, how many tables should i create and what each table should contain? – Oron Mar 30 '14 at 11:08
  • No, the table with "Receipt number/ item number/quantity/price" details will have mutiple entries for each item in that receipt. – ray Mar 30 '14 at 11:08
  • 1
    You really, really need to read a text on Relational Database Management Systems (RDBMS) so that you obtain a firm grasp of the concepts of tables, fields, keys, indexes, etc. Once you have that knowledge to hand, read something on database design, so that you gain an understanding of data modelling and representation of physical objects as database objects. – Eight-Bit Guru Mar 30 '14 at 11:13

1 Answers1

0

Items

ItemId
Item Name
Item Price

Recipet

ReciptID
ReciptInvoiceNo
ReciptDateTime

ReciptItem

ReciptID
ItemID
Quantity
UnitPrice

ray
  • 457
  • 3
  • 9