Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I can read datas from MDF but cannot save. My codes are below. I do stuffs inside DatabaseConfig.cs and send values from SalesScreen.cs

What I have tried:

DatabaseConfig.cs:
SqlConnection connect = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BombusDB.mdf;Integrated Security=True;");

        public int buyerCheckAndSave(string buyerPhone, string buyerName, string buyerAddress, bool containsItem)
        {
            int buyerID = 0;
            if (containsItem) //if customer exist just find id.
            {

                connect.Open();
                SqlCommand comm = new SqlCommand("SELECT TOP(1) ID FROM buyer_Tab WHERE pho LIKE @pho ORDER BY ID");
                comm.Parameters.AddWithValue("@pho", '%' + buyerPhone + '%');
                comm.Connection = connect;
                using (SqlDataReader ctr = comm.ExecuteReader())
                {
                    while (ctr.Read())
                    {
                        buyerID = ctr.GetInt32(0);
                    }
                }
                connect.Close();


                return buyerID;
            }
            else //if not first save
            {
                try
                {
                    connect.Open();
                    SqlCommand comm = new SqlCommand("INSERT INTO buyer_Tab (customer, pho, adr) VALUES ('@customer', '@pho', '@adr')");
                    comm.CommandType = CommandType.Text;
                    comm.Connection = connect;
                    comm.Parameters.AddWithValue("@customer", buyerName);
                    comm.Parameters.AddWithValue("@pho", buyerPhone);
                    comm.Parameters.AddWithValue("@adr", buyerAddress);
                    comm.ExecuteNonQuery();

                    connect.Close();
                }
                catch(SqlException e)
                {
                    System.Windows.Forms.MessageBox.Show("Hata Olustu! \n" + e);
                }

                buyerCheckAndSave(buyerPhone, buyerName, buyerAddress, true); //after saving get id
            }
            return buyerID;
        }

SalesScreen.cs (Winforms)
private void sellBtn_Click(object sender, EventArgs e)
        {
            billPrintDialog.Document = printBill;
            billPrintDialog.ShowDialog();
            printBill.Print();
            int customerID = 0;
            if (!String.IsNullOrEmpty(phoneTB.Text))
            {
                bool containsItem = customerPhones.Any(item => item.StartsWith(phoneTB.Text)); //We have existing customers on list
                customerID = db.buyerCheckAndSave(phoneTB.Text, nameTB.Text, adrRTB.Text, containsItem);
            }
            MessageBox.Show(""+customerID);
        }
Posted
Updated 6-Jul-17 0:02am

1 solution

Take the quotes off the parameter holders;

SqlCommand comm = new SqlCommand("INSERT INTO buyer_Tab (customer, pho, adr) VALUES (@customer, @pho, @adr)");
 
Share this answer
 
Comments
alidayan 6-Jul-17 6:05am    
Still the same :(
alidayan 6-Jul-17 6:18am    
I have solved. It saves but return wrong value thank you @Michael_Davies

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900