Link to home
Create AccountLog in
Avatar of Jimbo99999
Jimbo99999Flag for United States of America

asked on

VB.Net - DataTable add Field and Data

Good Day Experts!

Due to circumstances, I have a DataTable that will be populated that I need to add a Column to and then put a value in it after running a query.  This is the first time I have tried this.

Here is what I have so far(sqlRS3tbl is my DataTable that is already populated):

sqlRS3tbl.Columns.Add("mycountMth")
For Each sqlRSGetMthCount As DataRow In sqlRS3tbl.Rows
       sqlRSGetCountcmd = New SqlCommand(Select Count(Distinct [ProNumber])
       sqlRSGetMthCount("mycountMth") = sqlRSGetCountcmd .ExecuteNonQuery()
Next

Will this execute my query and put the count in the column that I added to my DataTable?

Thanks,
jimbo99999
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Jimbo99999

ASKER

Thanks for replying

I am not sure I understand...I do sqlRS3tbl.Columns.Add("mycountMth") ten I loop on it to add the values.

Is it that the DataTable is already populated that I cannot add a column?

Thanks,
jimbo99999
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
.Fill method reinitializes the datatable so it looses existing data and you will just get this new column.
Thanks for both of your responses.  I am learning more and more each day from everyone that responds.