2
Answers

Writing dataset changes to access database

Fredrik Roaldset

Fredrik Roaldset

14y
4.4k
1
Hi

I am working on an application with comboboxes bound to an external datasource; an access database. So the items in the combobox are bound to a bindingsource, that I set up using the design view.

It reads fine from the database, but now I want to add new items to the database. I am able to add them to the dataset, and they show up in the comboboxes, but they do not write to the database. I am new to using databases with C#, so the solution is probably obvious.

Here is the code I am using to add it to the dataset. That part works. When I call the update method, however, nothing happens.

public static DataSet AddWebsite(OleDbDataAdapter da, DataSet ds, string website_name, string website_url)
        {
            //updating the dataset
            DataRow dr = ds.Tables["website"].NewRow();
            dr["website_name"] = website_name;
            dr["website_url"] = website_url;
            ds.Tables["website"].Rows.Add(dr);
            ds.AcceptChanges();


           
            //updating the database

            da.Update(ds);

            MessageBox.Show("Website added");

            return ds;
        }//end AddToDatabase

I tried to output the da.UpdateCommand.CommandText. The command makes no sense:

UPDATE 'website' SET 'website_name' = ?, 'website_url' = ? WHERE (('website_id' = ?) AND ((? = 1 AND 'website_name' IS NULL ) OR ('website_name' = ?)) AND ((? = 1 AND 'website_url' IS NULL) OR ('website_url' = ?)))

 I have not set this command manually. Should I? If you don't care to teach me all this stuff, could anyone please give me a link to a resource that could help me understand it a bit better?

-Fred

Answers (2)