Hi
I am Very new to C#. Extremely sorry for my long description. I am trying an application which connect to the database and inserts the records to the database. My application name is EMPAPP. I added database to my application like this. In the solution explorer, by right clicking on the project , selecting add new Item then selecting database. Database name is EMP.mdf. I added two text boxes and one button. In the button click event I wrote the following code.
private void button1_Click(object sender, EventArgs e)
{
string connString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\EMP.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand();
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "insert into EMPTAB(ID,NAME) values
('" + textBox1.Text + "','" + textBox2.Text + "')";
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("One Record Inserted Successfully");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
After running the application and clicking on the button, iam getting “One Record Inserted Successfully”. However if I check tables data in server explorer, no data is there in the table.
I don’t know what went wrong in this. Please somebody help me out.
I tried changing the connection string like this
1) string connString = "Data Source=.\\SQLEXPRESS;Initial Catalog =
EMP;Integrated Security=True";
When run this application and cliked on buton , then I am getting “Invalid object name EMPTAB”.
2) If I change connection like this
string connString = "Data Source=.\\SQLEXPRESS;Initial Catalog = EMP.mdf;Integrated Security=True" ;
Here also I am getting error
“A Connection was succesfuly established with server.but then an error occurred during the login process.(Provider : shared memory provider ,error=0-no process is on the other side of pipe.)”
Suthish NairPosted Jul 9, 2011, 9:27 AM
refer article: Find Database Connection and Generate Connection String using Visual Studio
Get the connection string as stated in above link and use it in your logic.
Vilas GitePosted Jul 9, 2011, 8:55 AM
http://msdn.microsoft.com/en-us/library/bb264564(v=sql.90).aspx
If this reply hepls your post…then "MARK AS ANSWER"!
suman sukhejaPosted Jul 9, 2011, 7:54 AM
There is problem with configuration tools in MICROSOFT SQL SERVER.THIS IS THE PROCEDURE TO SOLVE UR
PROBLEM- GO TO ALL PROGRAMS-MICROSOFT SQL SERVER 2005-CONFIGURATION TOOLS-SQL SERVER
CONFIGURATION MANAGER-SQL SERVER 2005 NETWORK CONFIGURATION-PROTOCOLS FOR SQL EXPRESS- THEN
IN THAT SHARED MEMORY,NAMED PIPES AND TCP/IP SHOULD BE BE ENABLED AND THEN IN NATIVE CLIENT
CONFIGURATION- CLIENT PROTOCOLS SAME THREE HAS TO BE ENABLED.
THIS IS THE SOLUTION WHICH I THOUGHT. HOPE SO IT MAY HELP YOU.
harika mPosted Nov 23, 2008, 11:31 PM