Answered by:
how to get the value for a column in a table in a DataSet

Question
-
I just need to get a value out of a column in a table in a DataSet:
string x = ds.Tables["efile_Q"].Columns["accountNumber"]
This provides the value "accountNumber" (the column name), yet all I want is the value.
Seems simple enough, yet I cannot find any examples of how to do this.
Can someone point me on how to do this?
Thanks,
JayWednesday, September 9, 2009 10:36 PM
Answers
-
Hi Jay,
Try the following code...
public string GetColumnValue(int iRecordsetIndex, string sColumnName, int iRowIndex)
{
DataTable ObjDataTable;
string sContent = "";
try
{
ObjDataTable = ObjDataSet.Tables[iRecordsetIndex];
DataRow ObjDataRow = ObjDataTable.Rows[iRowIndex];
sContent = Convert.ToString(ObjDataRow[sColumnName]);
}
catch (Exception e)
{
Console.WriteLine(e);
}
return sContent;
}
Note:
You can get DataTable by using the table name also.
baskaran r- Proposed as answer by Michael Aspengren - MSFT Thursday, September 10, 2009 7:49 AM
- Marked as answer by Yichun_Feng Wednesday, September 16, 2009 1:21 AM
Thursday, September 10, 2009 5:54 AM -
To follow up on baskrans reply.
The reason you are seeing what you are seeing is that you get hold of the DataColumn object it self, not the value for a column on a specific row.// Gets the DataColumn object, so returns column name string s1 = ds.Tables["tablename"].Columns["columnname"].ToString(); // Gets the column value for row 1, so returns value string s2 = ds.Tables["tablename"].Rows[0]["columnname"].ToString();
//Michael
This posting is provided "AS IS" with no warranties.- Marked as answer by Yichun_Feng Wednesday, September 16, 2009 1:24 AM
Thursday, September 10, 2009 7:49 AM -
You mean out of a row by a specific column.
String x = ds.Tables["efile_Q"].Rows[rowNumber].Field<String>("accountNumber");
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Marked as answer by Yichun_Feng Wednesday, September 16, 2009 1:24 AM
Thursday, September 10, 2009 8:36 PM
All replies
-
Hi Jay,
Try the following code...
public string GetColumnValue(int iRecordsetIndex, string sColumnName, int iRowIndex)
{
DataTable ObjDataTable;
string sContent = "";
try
{
ObjDataTable = ObjDataSet.Tables[iRecordsetIndex];
DataRow ObjDataRow = ObjDataTable.Rows[iRowIndex];
sContent = Convert.ToString(ObjDataRow[sColumnName]);
}
catch (Exception e)
{
Console.WriteLine(e);
}
return sContent;
}
Note:
You can get DataTable by using the table name also.
baskaran r- Proposed as answer by Michael Aspengren - MSFT Thursday, September 10, 2009 7:49 AM
- Marked as answer by Yichun_Feng Wednesday, September 16, 2009 1:21 AM
Thursday, September 10, 2009 5:54 AM -
To follow up on baskrans reply.
The reason you are seeing what you are seeing is that you get hold of the DataColumn object it self, not the value for a column on a specific row.// Gets the DataColumn object, so returns column name string s1 = ds.Tables["tablename"].Columns["columnname"].ToString(); // Gets the column value for row 1, so returns value string s2 = ds.Tables["tablename"].Rows[0]["columnname"].ToString();
//Michael
This posting is provided "AS IS" with no warranties.- Marked as answer by Yichun_Feng Wednesday, September 16, 2009 1:24 AM
Thursday, September 10, 2009 7:49 AM -
You mean out of a row by a specific column.
String x = ds.Tables["efile_Q"].Rows[rowNumber].Field<String>("accountNumber");
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Marked as answer by Yichun_Feng Wednesday, September 16, 2009 1:24 AM
Thursday, September 10, 2009 8:36 PM