Click here to Skip to main content
15,903,784 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i want to insert some data using oledbdataadapter and dataset..

i am writing code for this.. this code executes inserted sucessfully.. but the data is not inserted in the database.. the code seems ok.. i am not able to figure it out.. where is the problem..
can anyone help me to correct this code..

C#
try
            {
                MAconn = new OleDbConnection();
                MAconn.ConnectionString = connectionString;
                MAconn.Open();
                DataSet oDS = new DataSet();
                
                string query = "SELECT * FROM info";

                OleDbDataAdapter oOrdersDataAdapter = new OleDbDataAdapter(query, connectionString);
                                OleDbCommandBuilder oOrdersCmdBuilder = new OleDbCommandBuilder(oOrdersDataAdapter);
                   oOrdersDataAdapter.Fill(oDS);

                DataTable pTable = oDS.Tables["Table"];
                pTable.TableName = "info";

                // Insert the Data
                DataRow oOrderRow = oDS.Tables["info"].NewRow();
                oOrderRow["name"] = textBox1.Text;
                oOrderRow["cell"] = textBox2.Text;
                
                oOrdersDataAdapter.Update(oDS, "info");
                MessageBox.Show("inserted");
            }catch(Exception ex)
            { MessageBox.Show("some prob"+ex);}
Posted
Updated 30-Apr-11 13:03pm
v3
Comments
Tarakeshwar Reddy 30-Apr-11 19:03pm    
Added pre tags

After you create a DataRow you have to add it to the DataTable.I put the code that you should modify it.

C++
// Insert the Data
                DataRow oOrderRow = oDS.Tables["info"].NewRow();
                oOrderRow["name"] = textBox1.Text;
                oOrderRow["cell"] = textBox2.Text;
                oDs.Tables["info"].Rows.Add(oOrderRow); // This code should be added.



I hope this will resolve your problem.
 
Share this answer
 
v2
Comments
codegeekalpha 1-May-11 3:29am    
thx bro.. it works..
Wonde Tadesse 6-Dec-11 10:30am    
You're most welcome
you need to add newly created rows to data-tables so use below code just before to your this line of code
oOrdersDataAdapter.Update(oDS, "info");

take a look how
MIDL
pTable.Rows.Add(oOrderRow);
               oOrdersDataAdapter.Update(oDS, "info");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900