How do I update a DataSource with new columns?

Posted on 2010-09-15
Last Modified: 2012-08-14
I am adding columns to a DataTable like so:

      DataColumn newColumn = new DataColumn("NEW_COLUMN", typeof(bool));
      newColumn.DefaultValue = false;
      newColumn.AllowDBNull = false;

According to what I see in my UI, this works just fine.  The DataGridView that I have bound to my DataTable Table gets updated with a new column as expected.

Later, I update the DataSource thusly:


adapter is an SqlDataAdapter.

When I close my application then look at my DataTable again, the columns that I added are not there.  Likewise, any columns that I removed are still there.

The only changes that persist are the addition and deletion of rows, and the changing of data in any cells of columns that were neither added nor deleted.

I suspect that the adding of columns is strictly an ALTER operation, not an INSERT, DELETE, or UPDATE operation, and I'm trying to find out how I can add ALTER functionality to my adapter.

Or maybe I'm on the wrong track entirely.

Any suggestions, out there?
Question by:MiloDCooper
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

by:Kaushal Arora
ID: 33679750
try using the command


after making modifications in the DataTable.

Hope it helps.

Expert Comment

by:Gururaj Badam
ID: 33679784
AcceptChanges is to accept the modifications done on the Rows of the DataTable. It doesn't actually create/remove column from the DB Table.

Accepted Solution

Gururaj Badam earned 125 total points
ID: 33679808
Using SMO may help generate table creations script to be used against the Database or you may use SQLCommand and write your own creation script
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

LVL 14

Expert Comment

ID: 33679825
The SqlDataAdapter can not create/delete columns. It's used for sending and retrieving data. You will have to do this with custom SqlCommand queryies or like Novice_Novice says with SMO.

Author Comment

ID: 33680088
OK so I'm looking into the SMO stuff, as advised by Novice_Novice.

The code I've got currently is:

      SqlConnection connection = new SqlConnection(CONNECTION_STRING);
      Server server = new Server(new ServerConnection(connection));
      Database db = server.Databases["my_db"];
      Table tbl = db.Tables["my_table"];
      Column newColumn = new Column(tbl, "NEW_COLUMN", DataType.Bit);
      newColumn.Nullable = false;

... after which I do a complete refresh on my original DataTable (i.e. reconnect to server and fill the DataTable again), whereupon I am still seeing no change to the table.

I set a breakpoint just after tbl.Columns.Add(newColumn), and tbl.Columns does indeed show NEW_COLUMN.

I tried tbl.Alter() and tbl.Refresh(), the first throws an exception and the second is seemingly inert.


Author Comment

ID: 33680136
P.S. The exception thrown by tbl.Alter() is "Alter failed for Table 'dbo.my_table'."

Author Comment

ID: 33680175
Got it working with the following code:

      SqlConnection connection = new SqlConnection(CONNECTION_STRING);
      Server server = new Server(new ServerConnection(connection));
      Database db = server.Databases["my_db"];
      Table tbl = db.Tables["my_table"];
      Column newColumn = new Column(tbl, "NEW_COLUMN", DataType.Bit);
      newColumn.Nullable = false;
      newColumn.DefaultConstraint.Text = "''";

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question