Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SQL INSERT.. Please Help..

SQL INSERT.. Please Help..

Scheduled Pinned Locked Moved C#
helpdatabasetestingbeta-testing
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fire85
    wrote on last edited by
    #1

    Sorry please help me out :) I got this error when doing SQL INSERT. "Object reference not set to an instance of an object." This was my coding: string connStr = ConfigurationSettings.AppSettings["ConnectionString"]; string desc = txtMailing.Text; OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\testing_K_\db1.mdb"); OleDbCommand cmd = new OleDbCommand(); OleDbDataAdapter oleDbDataAdapter1 = new OleDbDataAdapter(cmd); oleDbDataAdapter1.InsertCommand.CommandText = "INSERT INTO Mailing (MailingField)" + "VALUES ('"+ desc +"')"; //open the bridge between the application and the datasource con.Open(); oleDbDataAdapter1.InsertCommand.Connection = con; //execute the qurey oleDbDataAdapter1.InsertCommand.ExecuteNonQuery(); //close the connection con.Close(); The underlined sentence is the error. Please give me tips or advice to solve this. Thanks :) fire85.

    M L 2 Replies Last reply
    0
    • F fire85

      Sorry please help me out :) I got this error when doing SQL INSERT. "Object reference not set to an instance of an object." This was my coding: string connStr = ConfigurationSettings.AppSettings["ConnectionString"]; string desc = txtMailing.Text; OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\testing_K_\db1.mdb"); OleDbCommand cmd = new OleDbCommand(); OleDbDataAdapter oleDbDataAdapter1 = new OleDbDataAdapter(cmd); oleDbDataAdapter1.InsertCommand.CommandText = "INSERT INTO Mailing (MailingField)" + "VALUES ('"+ desc +"')"; //open the bridge between the application and the datasource con.Open(); oleDbDataAdapter1.InsertCommand.Connection = con; //execute the qurey oleDbDataAdapter1.InsertCommand.ExecuteNonQuery(); //close the connection con.Close(); The underlined sentence is the error. Please give me tips or advice to solve this. Thanks :) fire85.

      M Offline
      M Offline
      Mattias Olgerfelt
      wrote on last edited by
      #2

      Hi. To make an update you do not need the data adapter. Write like this and you will be happy: SqlConnection con = ... try { using (SqlCommand cmd = new SqlCommand("INSERT ...", con)) { // cmd.CommandType = CommandType.Text; Default is text, ut if you change to something else you should change this here. cmd.ExecuteNonQuery(); } catch (SqlException ex) { // Do stuff... } if (con != null) { con.Dispose(); con = null; } By the way, using is nice, it runs Dispose() on your command-object automatically. :) /M

      F 1 Reply Last reply
      0
      • F fire85

        Sorry please help me out :) I got this error when doing SQL INSERT. "Object reference not set to an instance of an object." This was my coding: string connStr = ConfigurationSettings.AppSettings["ConnectionString"]; string desc = txtMailing.Text; OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\testing_K_\db1.mdb"); OleDbCommand cmd = new OleDbCommand(); OleDbDataAdapter oleDbDataAdapter1 = new OleDbDataAdapter(cmd); oleDbDataAdapter1.InsertCommand.CommandText = "INSERT INTO Mailing (MailingField)" + "VALUES ('"+ desc +"')"; //open the bridge between the application and the datasource con.Open(); oleDbDataAdapter1.InsertCommand.Connection = con; //execute the qurey oleDbDataAdapter1.InsertCommand.ExecuteNonQuery(); //close the connection con.Close(); The underlined sentence is the error. Please give me tips or advice to solve this. Thanks :) fire85.

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        The SqlCommand parameter in the SqlDataAdapter constructor is for the SelectCommand, not the InsertCommand. But the other response is correct, you don't need the SqlDataAdapter. The SqlCommand.ExecuteNonQuery call is enough. -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        F 1 Reply Last reply
        0
        • M Mattias Olgerfelt

          Hi. To make an update you do not need the data adapter. Write like this and you will be happy: SqlConnection con = ... try { using (SqlCommand cmd = new SqlCommand("INSERT ...", con)) { // cmd.CommandType = CommandType.Text; Default is text, ut if you change to something else you should change this here. cmd.ExecuteNonQuery(); } catch (SqlException ex) { // Do stuff... } if (con != null) { con.Dispose(); con = null; } By the way, using is nice, it runs Dispose() on your command-object automatically. :) /M

          F Offline
          F Offline
          fire85
          wrote on last edited by
          #4

          Yeah Thanks alot :) It works :) fire85.

          1 Reply Last reply
          0
          • L Luis Alonso Ramos

            The SqlCommand parameter in the SqlDataAdapter constructor is for the SelectCommand, not the InsertCommand. But the other response is correct, you don't need the SqlDataAdapter. The SqlCommand.ExecuteNonQuery call is enough. -- LuisR


            Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

            F Offline
            F Offline
            fire85
            wrote on last edited by
            #5

            Sorry i am a newb to programming, cant get the meaning of what you trying to say. Is there any article or book that clearly explain about database linking?? Please introduce to me :) Thanks :) fire85.

            L 1 Reply Last reply
            0
            • F fire85

              Sorry i am a newb to programming, cant get the meaning of what you trying to say. Is there any article or book that clearly explain about database linking?? Please introduce to me :) Thanks :) fire85.

              L Offline
              L Offline
              Luis Alonso Ramos
              wrote on last edited by
              #6

              You need an SqlCommand object to represent execute any SQL statement on the database. The SqlDataAdapter is only a bridge that connects the data obtained from the database by executing a SELECT command, to a DataSet or DataTable (which can hold data on which you can work, without being connected to the database.) The SqlDataAdapter can also contain other commands (specifically, one INSERT, UPDATE and DELETE) that will be used when you ask it (the data adapter) to save the changes you've made to the offline data (DataTable or DataSet) to the actual database. So, it works like this: 1. You create a SqlCommand object representing your SELECT statement (it can be a stored procedure wrapping a SELECT statement.) 2. You create a new SqlDataAdapter object and pass it the SELECT statement. 3. You create an empty DataTable (or DataSet) 4. When you call myDataAdapter.Fill(myDataTable), the SELECT statement is executed in the database, and all the returned records are stored into the myDataTable object. 5. You start working with the data in the myDataTable object. You can add, modify or delete records offline, and your changes will not be saved in the real database. At this point you are not connected to the database. When you want to update the database with the changes you made to myDataTable, you follow these steps: 1. Create SqlCommand objects for the INSERT, UPDATE and DELETE queries. 2. Assign those command objects to the InsertCommand, UpdateCommand, and DeleteCommand properties of SqlDataAdapter object (myDataAdapter here). 3. Call myDataAdapter.Update(myDataTable). 4. The data adapter will look at each record in myDataTable and see what kind (if any) of modification it has had, and execute the appropiate command (INSERT if the row was added, UPDATE if it was modified, or DELETE if it was removed.) So, to your question. First see this[

              F 1 Reply Last reply
              0
              • L Luis Alonso Ramos

                You need an SqlCommand object to represent execute any SQL statement on the database. The SqlDataAdapter is only a bridge that connects the data obtained from the database by executing a SELECT command, to a DataSet or DataTable (which can hold data on which you can work, without being connected to the database.) The SqlDataAdapter can also contain other commands (specifically, one INSERT, UPDATE and DELETE) that will be used when you ask it (the data adapter) to save the changes you've made to the offline data (DataTable or DataSet) to the actual database. So, it works like this: 1. You create a SqlCommand object representing your SELECT statement (it can be a stored procedure wrapping a SELECT statement.) 2. You create a new SqlDataAdapter object and pass it the SELECT statement. 3. You create an empty DataTable (or DataSet) 4. When you call myDataAdapter.Fill(myDataTable), the SELECT statement is executed in the database, and all the returned records are stored into the myDataTable object. 5. You start working with the data in the myDataTable object. You can add, modify or delete records offline, and your changes will not be saved in the real database. At this point you are not connected to the database. When you want to update the database with the changes you made to myDataTable, you follow these steps: 1. Create SqlCommand objects for the INSERT, UPDATE and DELETE queries. 2. Assign those command objects to the InsertCommand, UpdateCommand, and DeleteCommand properties of SqlDataAdapter object (myDataAdapter here). 3. Call myDataAdapter.Update(myDataTable). 4. The data adapter will look at each record in myDataTable and see what kind (if any) of modification it has had, and execute the appropiate command (INSERT if the row was added, UPDATE if it was modified, or DELETE if it was removed.) So, to your question. First see this[

                F Offline
                F Offline
                fire85
                wrote on last edited by
                #7

                Ya is much more clearer to me now. Thanks alot LuisR, you have been a great help :) Thanks :) fire85.

                L 1 Reply Last reply
                0
                • F fire85

                  Ya is much more clearer to me now. Thanks alot LuisR, you have been a great help :) Thanks :) fire85.

                  L Offline
                  L Offline
                  Luis Alonso Ramos
                  wrote on last edited by
                  #8

                  fire85 wrote: Ya is much more clearer to me now. Thanks alot LuisR, you have been a great help You're very welcome! I'm glad I could help! :) -- LuisR


                  Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups