Community discussion forum

problem in connecting to the database (through coding , not in design )

  • 1 year ago
    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.)”
  • 1 year ago
    i think t db might b having 2 tables wit same name created by 2 different users. in t connection string, specify t user name tat u require.

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback