Answered by:
insert into the database mdf does not working

Question
-
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
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); }
- Edited by Ali Dayan Thursday, July 6, 2017 9:43 AM
Thursday, July 6, 2017 8:06 AM
Answers
-
I have solved. It gets value and doesn't return value.
- Marked as answer by Ali Dayan Friday, July 7, 2017 12:59 PM
Friday, July 7, 2017 12:59 PM
All replies
-
Hi Ali Dayan,
>>insert into the database mdf does not working
Please change the following Sql statement:
SqlCommand comm = new SqlCommand("INSERT INTO buyer_Tab (customer, pho, adr) VALUES ('@customer', '@pho', '@adr')");
To:
SqlCommand comm = new SqlCommand("INSERT INTO buyer_Tab (customer, pho, adr) VALUES (@customer, @pho, @adr)");
That means you can not add the '' to the (@customer, @pho, @adr) in the SQL statements with parameters.
Hope this helps!
Best Regards,
Stanly
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Stanly Fan Friday, July 7, 2017 6:11 AM
- Proposed as answer by Stanly Fan Friday, July 7, 2017 11:20 AM
Friday, July 7, 2017 6:11 AM -
I have solved. It gets value and doesn't return value.
- Marked as answer by Ali Dayan Friday, July 7, 2017 12:59 PM
Friday, July 7, 2017 12:59 PM