By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,948 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,948 IT Pros & Developers. It's quick & easy.

Get column names via Me.RowChanging's "e"?

P: 36
I have written a chunk of code that automatically writes a SQL string, using parameters. The parameters themselves are easy, but getting the column names has been a problem.

Is possible to get column names from the "e" of the Me.RowChanging event? Otherwise, writing
Expand|Select|Wrap|Line Numbers
  1. "Insert Into TableName (each, column, name) Values (@0, @1, @2)" 
is going to be a bit more difficult.

The other pieces of information I have made use of are:

e.Row.Table.Columns.Count to iterate through each column index.
e.Row.Item(index) to use as the parameter name - "@1", etc.
e.Row.Item(index).GetType to get the System.Type, which I convert into a System.DbType

I could write a class that holds the column names of the dateset I made in the designer, but that would be silly (and counter-productive, too). The names of the columns are already in the dataset!
Jun 25 '09 #1
Share this Question
Share on Google+
2 Replies


P: 3
Are you sure it's Me.Rowchanging? What's the instance type whose RowChanging event you are handling? Probably try to cast the sender object to the type and get the column name there.

However, the column name in the data table doesn't have to be same as the actual column name in the database

for eg., if the select query is: select dbColumn1 as DisplayColumn from Table, the column name from the datatable/dataset will be DisplayColumn. If you use this column name in your insert query, it will fail.
Jun 25 '09 #2

P: 36
Thank you! I hadn't even thought of that.

At the moment, the dataset's column names are the same as the database column names. I have an ArrayList that stores a coding name and a database-use name for each table and an ArrayList that stores the names and connection strings for all the databases I connect to, so adding an ArrayList that stores coding-use/database-use column names for each database wouldn't be a problem.

Here is the start of the dataset code (just fyi):
Expand|Select|Wrap|Line Numbers
  1. Partial Class DataSet1
  2.     Partial Class JobsDataTable
  3.  
  4.         Private Sub JobsDataTable_RowChanging(ByVal sender As System.Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles Me.RowChanging
And this is what I'll be using to solve my problem:
Expand|Select|Wrap|Line Numbers
  1.  ' My records for manipulation
  2.  Dim ds_Primary As DataSet1 = MDIParent1.DataSetMaster
  3.  
  4. ' Get each column name for this table
  5. For i = 0 To e.Row.Table.Columns.Count - 1
  6.         'Use the CodeReference to find which in-memory table I need
  7.         strSQL &= ds_Primary.Tables.Item(strCodeReference).Columns(i).ColumnName()
  8.         If i + 1 = e.Row.Table.Columns.Count Then
  9.                 ' Finish the column names, and move to the parameter names
  10.                 strSQL &= ") ("
  11.         Else
  12.                 ' Make way for the next column name
  13.                 strSQL &= ", "
  14.         End If
  15. Next i
  16.  
Jun 25 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.