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

Posted on 2007-10-01
Last Modified: 2008-01-09
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.
Question by:nishant009
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    you can use this instead:


    Author Comment

    in vs2005 datatable does not have GetString(field_number) method.
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    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..

    Author Comment

    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..
    LVL 142

    Accepted Solution


    SqlDataReader dr = DataTable.CreateDataReader();

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

       // do something with strName

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now