Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i want to insert data into .mdf database
this my connection
C#
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True");


and this is add code
C#
cmd.CommandText = "insert into deviceProblem(id,clientName,receiptDate,deliveryDate"+
                    ",deviceNumber,deviceType,deviceProb,price) " + 
                    "values(" + Convert.ToInt64(id.Text) + ",'" + clientName.Text + "','" 
                    + maskedTextBox1.Text + "','" + maskedTextBox2.Text + "'," 
                    + Convert.ToInt64(deviceId.Text) + ",'" + deviceType.Text + "','" + problem.Text + "'," + price.Text + ")";
                    cmd.ExecuteNonQuery();
if (cmd.ExecuteNonQuery() == 1)
                    {
                        MessageBox.Show("success" + id.Text);
                    }


success appears but when close program nothing in mdf database
??
Posted
Comments
Richard Deeming 5-Nov-14 11:22am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
PIEBALDconsult 5-Nov-14 11:31am    
Other than the SQL injection vulnerablity and calling cmd.ExecuteNonQuery() twice, nothing seems obviously wrong there.
The most likely thing is you may be inserting to one copy of the file and checking another.

right click on your mdf file in solution explorer and go to properties > build options => select copy if newer option and also make sure that you are checking correct database file. if you run on debug mode, go to Bin\Debug folder and check the mdf file in there, if you run on release mode you need to check on Bin\Release folder
 
Share this answer
 
v2
We can't say - there are just too many variables here. So the first thing you need to do is gather information.

Start by checking what it going on: do you get your MessageBox? if not, then you have a exception happening somewhere that you are swallowing.

Check your database carefully: make sure that it is in a folder that has access permissions for read and write to all users. SQL does not use your user id to access files, even if you start your application under your login!

Then check how you are confirming any data. Make absolutely sure that the file is the same one: replace the path with an absolute path of the form C:\folder\file.mdf instead of working with |DataDirectory| and confirm that the updates have been done before you exit your application. Then check the data again to make sure it is still in there.

Then run your app again, and check that the data is still in the DB before you make any changes.

At what point do things go wrong? That should give you at least a clue as to what is happening.
 
Share this answer
 

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