Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have simple form which reads users name,email and city. I have created a database and a table with fields id,name,email and city. When i run the code i don't encounter any error or exceptions. But unable to see the inserted data into table.

following is my code
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=E:\\Database3.mdf;Integrated Security=True;Connect Timeout=30");
                con.Open();
                SqlCommand cmd = new SqlCommand(@"insert into tbl_insert values(@Id,@name,@city)", con);
                cmd.Parameters.AddWithValue("@Id", Convert.ToInt32(txtId.Text));
                cmd.Parameters.AddWithValue("@name", txtName.Text);
                cmd.Parameters.AddWithValue("@city", txtCity.Text);


What I have tried:

i have database file (.mdf) and logfile (.ldf) in one directory and rest of the project in visual studio workspace. by debugging i am pretty sure that data is inserted into table but i am not able to see data in the table.
Posted
Updated 24-Jan-17 20:35pm

add below code at the last after all parameters have added.
cmd.ExecuteNonQuery();

I did not see below ExecuteNonQuery code in your code.
<pre>command.ExecuteNonQuery();
 
Share this answer
 
v3
if the number of columns in the table tbl_insert is more than 3

then u should specify the specific column name also in the insert query...
 
Share this answer
 
if the number of columns in the table tbl_insert is more than 3

then u should specify the specific column name also in the insert query...
 
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