Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6875819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:24:08+00:00 2026-05-27T04:24:08+00:00

private DataRow getDataRowFromReader(IDataReader reader) { DataRow row = new DataRow(); DataTable tbl = new

  • 0
private DataRow getDataRowFromReader(IDataReader reader)
{
    DataRow row = new DataRow();
    DataTable tbl = new DataTable();
    for (int i = 0; i < reader.FieldCount; i++)
    {
        Type type = reader[i].GetType();
        if(type.Equals(Type.GetType("DBNull")) || type.Equals(Type.GetType("System.DBNull")))
            type = typeof(string);
        DataColumn col = new DataColumn(reader.GetName(i), type);
        tbl.Columns.Add(col);
    }
    row = tbl.NewRow();
    for (int i = 0; i < reader.FieldCount; i++)
    {
        Type readerType = reader[i].GetType();
        Type rowType = row[i].GetType();
        if (readerType.Equals(rowType))
            row[i] = reader.GetValue(i);
        else
            row[i] = reader.GetString(i);
    }
    return row;
}

I’m trying to handle DBNull in the 1st loop, but in the second I have then all rows created by NewRow() of DBNull type.

e.g. rowType ~ DBNull

Why this is happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:24:08+00:00Added an answer on May 27, 2026 at 4:24 am

    You can use this to find out if the value you’re looking at is DBNull:

    reader.IsDBNull(i)
    

    see http://msdn.microsoft.com/en-us/library/system.data.idatarecord.isdbnull.aspx

    To find the type of the column you’re on, try this:

    reader.GetFieldType(i)
    

    see http://msdn.microsoft.com/en-us/library/system.data.idatarecord.getfieldtype.aspx

    Also, I think there’s no need for new DataRow() on the first line, if you are abandoning that reference by doing row = tbl.NewRow() after the first loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: private User PopulateUsersList(DataRow row) { Users user
private void FillInvoiceList() { DataTable distinctInvoice = new DataTable(); using (SqlConnection conn = new
private void DoSomeWork() { ManualResetEvent[] waitEvents = new ManualResetEvent[rowCount]; int i = 0; foreach
I have the following method: private JobCard PopulateObject(JobCard jc, DataRow dataRow) { PropertyInfo[] proplist
private const int THE_ANSWER = 42; or private const int theAnswer = 42; Personally
. private void Form1_Load(object sender, EventArgs e) { List<CaclulatedData> tests = new List<CaclulatedData> {
private List<String> subList; private List<List<String>> records = new ArrayList<List<String>>(); for(....){ subList = new ArrayList<String>();
Below is the code behind in my view model: private DataRow selectedErrorRow; public DataRow
I have some function like private static UserInfo FillUserInfoFromDataRow(DataRow dr) { UserInfo user =
I'm trying to store the DataType field value (type System.Type) of a DbDataReader.GetSchemaTable.DataRow to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.