Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am currently working with a windows application project. In that I tried to insert data into a table. All the lines of codes are working as expected, but the data is not inserted into the table using code.I am using sql queries. I can enter data manually. I can update and select data from database. But insert is not possible. Could you please help me to find out the reason...?

here is my code:

SqlConnection con = new SqlConnection();
           con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Bill.mdf;Integrated Security=True;User Instance=True";
           SqlCommand cmd = new SqlCommand("insert into tblCustomer values('"+txtName.Text+"','"+txtPhone.Text+"','"+txtMobile.Text+"','"+txtEmail.Text+"','"+sales+"')");
           cmd.Connection = con;
           con.Open();
           cmd.ExecuteNonQuery();
           con.Close();



I didn't get any error message or exception. But the data is not inserted.


Thank you....
Posted
Updated 9-Mar-17 19:28pm
v3
Comments
OriginalGriff 29-Jul-12 11:43am    
Without seeing your code, we really can't tell you much.
Have you used the debugger to ensure that your insert commands are getting executed at all?
Use the "Improve question" widget to edit your question and provide better information.
bbirajdar 29-Jul-12 11:53am    
How can we ? If we are not able to see your computer screen and check what code you have written...!!!
Please post your codes and say what exception or error message you are getting.
Manoj Kumar Choubey 31-Jul-12 9:55am    
Please post full source code of the sub / function without the code we can't help ....
[no name] 23-Sep-14 4:56am    
Have you tried debugging? And are you getting any errors?

Most likely you are inserting if you are not receiving an error. I cannot see your code but I would hope you have done a try/catch to make sure there are no errors.

If there are no errors then the next step is to make sure you really need a user instance. A user instance will not update the master database. It is actually copied to a "local" instance of the database.

To check to see if it is inserting properly you can use a datareader to connect to the database, do the insert and bring back the results. This should confirm the insert statement has done what it is supposed to.

Again, the insert statement in a User Instance will only apply to the user instance NOT the main database.

For more information: Connecting to User Instances[^]
 
Share this answer
 
We need a lot of chance to answer your question exactly; but you should check these suggestions:

1. check your ConnectionString
2. check your CommandText
3. check your Parameters

Best suggestion is following your code by using break points and see they are doing what you want or not.

You want more help, so tell us more about your project.
 
Share this answer
 
Comments
Zoltán Zörgő 31-Jul-12 12:08pm    
Additionally running sql trace would give future information.
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Bill.mdf;Integrated Security=True;User Instance=True";
SqlCommand cmd = new SqlCommand("insert into tblCustomer( add column name with sequnce like fieldname name,phone,which is in database column name) values('"+txtName.Text+"','"+txtPhone.Text+"','"+txtMobile.Text+"','"+txtEmail.Text+"','"+sales+"')");
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
 
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