Answered by:
Adding row to Typed datatable in C#

Question
-
Below is the code i m using to add row to typed datatable, but it doesnt work. It says use of unassigned local variable.can some one please tell why?
TypedDataTableSchema
.TypedDataTableRow r;
r.ID = UserID;
dt.Rows.Add(r);
if i try to assign it
TypedDataTableSchema.TypedDataTableRow r = dt.newRow();
Then also it does not work.Wednesday, December 31, 2008 11:42 AM
Answers
-
Your first attempt will leave the variable r null, so you will get at the line r.ID = UserID; a NullReferenceException.
What error do you receive at the second attempt?
You might have to cast the newRow result:
TypedDataTableSchema.TypedDataTableRow r = (TypedDataTableSchema.TypedDataTableRow) dt.newRow();
Ewald - Please remember to mark the replies as answers if they help.- Marked as answer by 123M Wednesday, December 31, 2008 12:28 PM
Wednesday, December 31, 2008 12:19 PM -
I was mistaken.
dt.IDColumn will return the datacolumn ID of the datatable, thus dt.IDColumn.Name will return "ID"
dt.Rows[0].ID will return the value in the ID column of the first row in the datatable.
Ewald - Please remember to mark the replies as answers if they help.- Marked as answer by 123M Friday, January 2, 2009 11:21 AM
Friday, January 2, 2009 11:05 AM
All replies
-
Your first attempt will leave the variable r null, so you will get at the line r.ID = UserID; a NullReferenceException.
What error do you receive at the second attempt?
You might have to cast the newRow result:
TypedDataTableSchema.TypedDataTableRow r = (TypedDataTableSchema.TypedDataTableRow) dt.newRow();
Ewald - Please remember to mark the replies as answers if they help.- Marked as answer by 123M Wednesday, December 31, 2008 12:28 PM
Wednesday, December 31, 2008 12:19 PM -
Thanks, your post helped me a lot.
But cud u please tell how do i use a Typed dataTable as input to a method??
Suppose i have created Datatable schema (in TableSchema.XSD) with table name as "MemberTableschema".
Now i generate a datatable out of it (using the above schema).
i have another method say "Test" which takes this TYped datatable as input.
how should the definition be ?
Test( DataTable dt) or Test(MemberTableschema dt)?
Also,
The MemberTableschema has follwoing columns:
Name
ID
In this "Test" Method i want to acess the "ID" column's columnName. (to put a filter with distinct criteria on table). How can i get the "ID" column's columnName?Friday, January 2, 2009 10:16 AM -
I would use the second option.
public void Test(MemberTableschema dt)
{
int id = dt.ID;
}
Ewald - Please remember to mark the replies as answers if they help.Friday, January 2, 2009 10:27 AM -
Thats the correct approach, Thank you.
But Ewald,
int id = dt.ID
would the above give me the column name of the ID column??
that means
string colName = dt.Name
will give me "Name" column's columnName?Friday, January 2, 2009 10:57 AM -
I was mistaken.
dt.IDColumn will return the datacolumn ID of the datatable, thus dt.IDColumn.Name will return "ID"
dt.Rows[0].ID will return the value in the ID column of the first row in the datatable.
Ewald - Please remember to mark the replies as answers if they help.- Marked as answer by 123M Friday, January 2, 2009 11:21 AM
Friday, January 2, 2009 11:05 AM