I have a SQL Server table that has no rows. Programmatically in WinForms C#, I have created a DataTable with the same schema and populated it with data. I would then like to write the contents of this DataTable to the SQL Sever table using a SQLDataAdapter. Since the data adapter requires at least a SELECT statement, I've coded it like this:

using (SqlDataAdapter oAdapter = new SqlDataAdapter("SELECT * FROM invoice", oConnection))
{
    oAdapter.Fill(oDT);
    oAdapter.Update(oDT);
}

The trouble with this is that there are no rows returned because the SQL Server table is empty! So it seems the Fill and Update are working with no data, but I know my DataTable has over 7000 rows which I would like saved to the SQL Server table. Obviously, I'm doing something wrong, but I haven't been able to come across anything with a Google search like "SqlDataAdapter update empty SQL Server table" or similar phrases. It's probably a simple solution, so I'm hoping someone out there can tell me what it is. :) Forgive me if I've posted this in the wrong forum. TIA

Recommended Answers

All 2 Replies

You need to provide an update command to the adapter. You have to provide the select as that is the most common use, to display data.

SqlDataAdapter.Update

Thanks for the response. I used SqlCommandBuilder to auto generate the statement and all is well. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.