none
Couldn't able to insert data in database(sql server) RRS feed

  • Question

  • Hi, I am having the problem in inserting the data in sql server 2008 with visual studio 2008.

    The main scenario is, i can get (select) the data from database but wouldn't able to insert in it, i m using right code because there is no error shown by compiler and it shows that your record is inserted but Actually it is not inserted u can also see my code.....

    System.Data.SqlClient.SqlConnection con;

                con = new System.Data.SqlClient.SqlConnection();

                con.ConnectionString = "Data Source=Ahmad-PC; AttachDbFilename=|DataDirectory|\\New.mdf; Integrated Security = TRUE";

                System.Data.SqlClient.SqlDataAdapter da;

                da = new System.Data.SqlClient.SqlDataAdapter();

                da.InsertCommand = new SqlCommand("INSERT INTO Good VALUES (@FName,@LName)",con);

                da.InsertCommand.Parameters.Add("@FName", SqlDbType.VarChar).Value = textBox1.Text;

                da.InsertCommand.Parameters.Add("@Lname", SqlDbType.VarChar).Value = textBox2.Text;

                con.Open();

                da.InsertCommand.ExecuteNonQuery();

                con.Close();

                MessageBox.Show("Record Added");

    Plz help me out to insert data or do tell me if there is any other problem other than my coding mistake....Thanx in advance

    • Moved by Hongye Sun - MSFT Friday, November 19, 2010 8:25 AM ADO.NET issue (From:Getting Started With Visual Studio Team System)
    • Moved by VMazurModerator Wednesday, December 1, 2010 11:43 AM (From:ADO.NET DataSet)
    Wednesday, November 17, 2010 10:07 AM

Answers

  • Hello,

    Have you debuged the code? Is there any exception/error when you debugging (step through the code line by line)? It works fine on my side.

    Moreover, please try to specify the absolute path to your database file, for example,

    @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Projects\New.mdb;Integrated Security=True;User Instance=True";

    Please have a try and let me know if it does not work.


    Best Regards,
    Roahn Luo
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    • Marked as answer by liurong luo Tuesday, November 23, 2010 3:45 AM
    Saturday, November 20, 2010 7:06 AM

All replies

  • Hello,

    Have you debuged the code? Is there any exception/error when you debugging (step through the code line by line)? It works fine on my side.

    Moreover, please try to specify the absolute path to your database file, for example,

    @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Projects\New.mdb;Integrated Security=True;User Instance=True";

    Please have a try and let me know if it does not work.


    Best Regards,
    Roahn Luo
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    • Marked as answer by liurong luo Tuesday, November 23, 2010 3:45 AM
    Saturday, November 20, 2010 7:06 AM
  • Thanx for replying.

    Upon your advise i check the path there is some mistake in it, now i can insert any thing in data base.

    And Thanx for your help also


    M.Azeem
    Sunday, November 21, 2010 8:59 AM