Previous Thread

7/31/2006 4:21:52 AM    Column Data Type problem (ADO.NET, C#)
Hi, 
 
I need to be able to add or remove columns from an Access database 
 
table (table is bound to a DataGridView). I know I have to use ALTER 
 
TABLE to do this. After I add a System.String column (AllowDBNull = 
 
false, MaxLength = 35) to my DataSet and a VARCHAR(35) column to the DB 
 
("ALTER TABLE tbl ADD col VARCHAR(35) NOT NULL" -> 
 
cmd.ExecuteNonQuery()) I'm able to generate correct INSERT, DELETE and 
 
UPDATE commands using CommandBuilder, but dAdapter.Update(dSet, "tbl") 
 
ends up in an exception "Data types in the expression do not match." In 
 
case I do not add any columns .Update(...) is executed without an 
 
exception being thrown. 
 
Here's the code: 
 
dSet.Tables["tbl"].Columns.Add("col"); 
 
dSet.Tables["tbl"].Columns["col"].MaxLength = 35; 
 
// column is filled in with values which I need to be there (in case 
 
// this would be done after AllowDBNull = false an exception 
 
// would be thrown...) 
 
dSet.Tables["tbl"].Columns["col"].AllowDBNull = false; 
 
// a column is added to the DataGridView and bound to the dSet column 
 
// created above now 
 
// other code here 
 
OleDbCommand cmd = new OleDbCommand("ALTER TABLE tbl ADD col 
 
VARCHAR(35) NOT NULL", conn); 
 
conn.Open(); 
 
cmd.ExecuteNonQuery();   // columns in dSet match columns in DB now 
 
bindingSource.EndEdit(); 
 
dAdapter.Update(dSet, "tbl"); 
 
conn.Close(); 
 
I'm not able to figure out why this doesn't work... Any help would be 
 
appreciated. 
 
With regards 
 
nvx



7/31/2006 9:37:05 PM    Re: Column Data Type problem (ADO.NET, C#)
nvx (nvx2004@hotmail.com) writes: 
 
Looking at your code, I fail to see how the DataAdapter would automatically 
 
know about your new column directly after you have added it. 
 
Then again, this newsgroup focues on the native OLE API, so you may not 
 
get the best answer here. You may have better success in 
 
microsoft.public.dotnet.framework.adonet. 
 
-- 
 
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se 
 
Books Online for SQL Server 2005 at 
 
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx 
 
Books Online for SQL Server 2000 at 
 
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

7/31/2006 11:53:22 PM    Re: Column Data Type problem (ADO.NET, C#)
Hi Erland, 
 
thank you for your reply and for your suggestion. 
 
As for the DataAdapter: well, it probably knows I've added something 
 
because after generation of INSERT, DELETE and UPDATE commands 
 
(dAdapter.InsertCommand = commandBuilder.GetInsertCommand()) it 
 
generates everything that's necessary for all columns including the 
 
added one. But I might be wrong... 
 
With regards 
 
nvx 
 
Erland Sommarskog wrote:

8/1/2006 1:13:02 AM    Re: Column Data Type problem (ADO.NET, C#)
I'm sorry, that paragraph about DataAdapter is quite a nonsense... It 
 
is obvious that DataAdapter does not have to know about the added 
 
columns automatically... 
 
nvx 
 
nvx wrote: