• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Get value from DataTable to string variable wihout using unboxing or conversion

How would i take the value from the datatable to an string variable without converting or using unboxing for example string strName = DataTable["Name"].ToString() if i use the string strName = DataTable["Name"]. It throws an error message and i want to rid from this conversion
I am having an table which having the lakhs of records for some business logic i have to loop throw this entire table and use the table values. This conversion from object to string hamper the performance so i want to skip this conversion is there any way so that i can escape from this conversion? I am using vs2005 with sqlserver2005.
0
nishant009
Asked:
nishant009
  • 3
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you can use this instead:
DataTable.GetString(field_number)

0
 
nishant009Author Commented:
in vs2005 datatable does not have GetString(field_number) method.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry... I confused with DataReader.
you could create a DataReader from the DataTable using CreateDataReader method

and from there, you have the GetString (and other) methods..
0
 
nishant009Author Commented:
but i thing GetString() is related with the DataReader and i am talking about getting the values from the datatable which does not have any GetString() method into it? So can you please send some code which shows what are you saying..
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:

SqlDataReader dr = DataTable.CreateDataReader();

while (dr.Read())
{
   strName = dr.GetString(0)   // 0 begin the ordinal position of the column "Name"

   // do something with strName
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now