SQL INSERT.. Please Help..
-
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.
-
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.
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 -
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.
The
SqlCommand
parameter in theSqlDataAdapter
constructor is for theSelectCommand
, not theInsertCommand
. But the other response is correct, you don't need theSqlDataAdapter
. TheSqlCommand.ExecuteNonQuery
call is enough. -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
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 -
The
SqlCommand
parameter in theSqlDataAdapter
constructor is for theSelectCommand
, not theInsertCommand
. But the other response is correct, you don't need theSqlDataAdapter
. TheSqlCommand.ExecuteNonQuery
call is enough. -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
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.
You need an
SqlCommand
object to represent execute any SQL statement on the database. TheSqlDataAdapter
is only a bridge that connects the data obtained from the database by executing aSELECT
command, to aDataSet
orDataTable
(which can hold data on which you can work, without being connected to the database.) TheSqlDataAdapter
can also contain other commands (specifically, oneINSERT
,UPDATE
andDELETE
) that will be used when you ask it (the data adapter) to save the changes you've made to the offline data (DataTable
orDataSet
) to the actual database. So, it works like this: 1. You create aSqlCommand
object representing yourSELECT
statement (it can be a stored procedure wrapping aSELECT
statement.) 2. You create a newSqlDataAdapter
object and pass it theSELECT
statement. 3. You create an emptyDataTable
(orDataSet
) 4. When you callmyDataAdapter.Fill(myDataTable)
, theSELECT
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 themyDataTable
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 tomyDataTable
, you follow these steps: 1. CreateSqlCommand
objects for theINSERT
,UPDATE
andDELETE
queries. 2. Assign those command objects to theInsertCommand
,UpdateCommand
, andDeleteCommand
properties ofSqlDataAdapter
object (myDataAdapter
here). 3. CallmyDataAdapter.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, orDELETE
if it was removed.) So, to your question. First see this[ -
You need an
SqlCommand
object to represent execute any SQL statement on the database. TheSqlDataAdapter
is only a bridge that connects the data obtained from the database by executing aSELECT
command, to aDataSet
orDataTable
(which can hold data on which you can work, without being connected to the database.) TheSqlDataAdapter
can also contain other commands (specifically, oneINSERT
,UPDATE
andDELETE
) that will be used when you ask it (the data adapter) to save the changes you've made to the offline data (DataTable
orDataSet
) to the actual database. So, it works like this: 1. You create aSqlCommand
object representing yourSELECT
statement (it can be a stored procedure wrapping aSELECT
statement.) 2. You create a newSqlDataAdapter
object and pass it theSELECT
statement. 3. You create an emptyDataTable
(orDataSet
) 4. When you callmyDataAdapter.Fill(myDataTable)
, theSELECT
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 themyDataTable
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 tomyDataTable
, you follow these steps: 1. CreateSqlCommand
objects for theINSERT
,UPDATE
andDELETE
queries. 2. Assign those command objects to theInsertCommand
,UpdateCommand
, andDeleteCommand
properties ofSqlDataAdapter
object (myDataAdapter
here). 3. CallmyDataAdapter.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, orDELETE
if it was removed.) So, to your question. First see this[ -
Ya is much more clearer to me now. Thanks alot LuisR, you have been a great help :) Thanks :) fire85.
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!