0

I am a newbie to c#. I am trying to insert the values from the text box into the table in my database.

Table Name : address

Fields : name(varchar(50)), age(int), city(nchar(10))

When i try to retrieve the values from the database it is working perfectly. Here is my code. Please help me rectify it.

            string s = @"Data      Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated  Security=True;User Instance=True";
            SqlConnection a = new SqlConnection(s);

            a.Open();
            SqlCommand comm = new SqlCommand("INSERT INTO [address](name,age,city)  VALUES(@na,@ag,@ci)");
            string na = textBox1.Text;
            int ag = int.Parse(textBox2.Text);
            string ci = textBox3.Text;

            comm.Parameters.Add("@na", System.Data.SqlDbType.VarChar,50).Value = na;
            comm.Parameters.Add("@ag", System.Data.SqlDbType.Int).Value = ag;
            comm.Parameters.Add("@ci", System.Data.SqlDbType.NChar,10).Value = ci;
            comm.Connection = a;
            comm.ExecuteNonQuery();
            MessageBox.Show("okay");
            a.Close();

The values are not reflected in the database.

6
  • Of the top of my head, I'm going to throw out there that it's a file system permissions problem since he's attaching a SQLExpress file. Just a wild guess, but let's see if it holds up. =) Commented Jun 26, 2012 at 17:52
  • @AustinSalonen am not getting any error but the values are not reflected in the database. Commented Jun 26, 2012 at 17:54
  • comm.Parameters.Add("@na", System.Data.SqlDbType.VarChar,50).Value = na; you might want to try to put a closing bracket. comm.Parameters.Add("@na", System.Data.SqlDbType.VarChar,50).Value = na); Commented Jun 26, 2012 at 17:55
  • Jane: that would make two closing brackets. Commented Jun 26, 2012 at 18:02
  • @JaneDoe How can i add a closing bracket without an opening one. Commented Jun 26, 2012 at 18:06

1 Answer 1

4

try with this code :

string connectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated  Security=True;User Instance=True";

using (SqlConnection connection = new SqlConnection(connectionString))
{

    using (SqlCommand insertCommand = connection.CreateCommand())

        {
            insertCommand.CommandText = "INSERT INTO address(name,age,city) VALUES (@na,@ag,@ci)";
            insertCommand.Parameters.Add("@na", na);
            insertCommand.Parameters.Add("@ag", ag);
            insertCommand.Parameters.Add("@ci", ci);


            insertCommand.Connection.Open();
            insertCommand.ExecuteNonQuery();


        }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Why are you closing the SqlConnection inside a using?
using call dispose in the end of treatment
It is working now. The inserted data is present in the database, but the table in Database explorer is not updated. I am getting the new values when i use the select query i did. And may i know why is the table not getting updated?
you must just refresh your table in your explorator
Yeah tried that it aint working. One more thing the data is just available temporarily. When i close the solution and open it again, the newly inserted rows are gone. Is there something like PERSIST that we got to do?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.