Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Any help would be appreciated!!

New to c# and trying to do some basic database edit / update / add etc.

Cant seem to get any data to update into my table.
I am picking up the data and able to display and step thru. Its as if there is a read only attribute somewhere !!!


C#
private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = dbProvider + dbSource;
            string queryString=sql;
            DataSet dataSet = new DataSet();
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                connection.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                adapter.SelectCommand = new OleDbCommand(queryString, connection);
                OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
                adapter.Fill(dataSet);

                string st = "Dizzy blond";

                dataSet.Tables[0].Rows[1].ItemArray[2] = st;

                // Code to Modify

                adapter.UpdateCommand = builder.GetUpdateCommand();
                adapter.Update(dataSet);
            }
Posted
Updated 13-Aug-14 7:42am
v2
Comments
ZurdoDev 13-Aug-14 13:04pm    
That will call the Update statement. Where is that set?
Member 11008177 13-Aug-14 13:40pm    
Sorry ??
Not sure what you want ??
ZurdoDev 13-Aug-14 16:25pm    
1. Reply to the comment so the user is notified.
2. You have to give it an update statement, "UPDATE table SET ..." so it knows how to actually update the data.
PIEBALDconsult 13-Aug-14 13:50pm    
I haven't used a DataAdapter (ptui) in many years. If I recall correctly...
The commands will be set if you pass the query to the constructor, so you don't need to do it again.
You may need to use AcceptChanges before calling Update.
You don't actually need to open the connection when using a DataAdapter.

But what may be happening is if there is no dataSet.Tables[0].Rows[1].ItemArray[2] then you may be getting an index out of range exception and the Update won't be called.

Try using the debugger to step through the code.
Anand Gunasekaran 13-Aug-14 14:13pm    
check your query again and again, there may be some spelling mistakes?

1 solution

Dear Member 11008177,


As above you had shown uses of COMMANDBUILDER things.

With CommandBuilder try this:
http://www.homeandlearn.co.uk/csharp/csharp_s12p9.html[^]



But I suggest don't use COMMANDBUILDER try use your own ADO.NET Code.Try, this below mention link.


http://altafhussainbd.wordpress.com/tag/crud-operation-in-c-and-access/[^]


https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4291&lngWId=10[^]


ADO.NET
http://www.dbtutorials.com/advanced/access-db-ado-net-csharp/[^]

http://www.c-sharpcorner.com/uploadfile/mimrantaj/connect-to-access-database-in-C-Sharp-and-ado-net/[^]

Why I am telling you (Member 11008177) because If you use your own code. You will having more control on code rather commandbuilder.

Any fruther assist you require feel free to contact me.

Thank You.
Manoj Kalla
 
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