Solved

How do I update a DataSource with new columns?

Posted on 2010-09-15
7
357 Views
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;
      Table.Columns.Add(newColumn);

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.Update(Table);

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?
0
Comment
Question by:MiloDCooper
7 Comments
 
LVL 12

Expert Comment

by:Kaushal Arora
Comment Utility
try using the command

DataTableName.AcceptChanges();

after making modifications in the DataTable.

Hope it helps.
0
 
LVL 8

Expert Comment

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

Accepted Solution

by:
Gururaj Badam earned 125 total points
Comment Utility
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

http://davidhayden.com/blog/dave/archive/2006/01/27/2775.aspx
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 14

Expert Comment

by:existenz2
Comment Utility
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.
0
 

Author Comment

by:MiloDCooper
Comment Utility
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;
      tbl.Columns.Add(newColumn);

... 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.

Ideas?
0
 

Author Comment

by:MiloDCooper
Comment Utility
P.S. The exception thrown by tbl.Alter() is "Alter failed for Table 'dbo.my_table'."
0
 

Author Comment

by:MiloDCooper
Comment Utility
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.AddDefaultConstraint();
      newColumn.DefaultConstraint.Text = "''";
      tbl.Columns.Add(newColumn);
      tbl.Alter();
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now