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

  • Home
  • SEARCH
  • 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 979671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:15:21+00:00 2026-05-16T04:15:21+00:00

Using C# I have a datareader that return a lsit of records from a

  • 0

Using C#

I have a datareader that return a lsit of records from a mysql database.

I am trying to write code that checks if the datareader isnull. The logic behind this is: If the datareader having field then display the info otherwise hide the field.

I have tried:

cmd1 = new OdbcCommand("Select * from tb_car where vehicleno = '" + textbox2.text  + "';", dbcon);
        dr1 = cmd1.ExecuteReader();

if (dr1["tb_car"]. != DBNull.Value) 
{ 
    textbox1.Text  = "contains data"; 
} 
else 
{
    textbox1.Text = "is null"; 
}

The above code gives me this error:

Exception Details: System.IndexOutOfRangeException: Additional

Any help would be greatly appreciated…

  • 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-16T04:15:21+00:00Added an answer on May 16, 2026 at 4:15 am

    I see a few problems here… First, it looks like you’re trying to access the table name in the line:

    if(dr1["tb_car"] != DBNull.Value
    

    You should be passing a FIELD NAME instead of the table name. So if the table named “tb_car” had a field called CarId, you would want to have your code look like:

    if(dr1["CarId"] != DBNull.Value)
    

    If I’m right, then there is probably no field named “tb_car”, and the Index is Out of Range error is because the DataReader is looking for an item in the column collection named “tb_car” and not finding it. That’s pretty much what the error means.

    Second, before you can even check it , you have to call the DataReader’s Read() command first to read a line from the database.

    so really your code should look like…

    while(dr.Read())
    {
       if(dr1["CarId"] != DBNull.Value)
       {
          ....
    

    and so on.

    See here for the proper use of a DataReader: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx


    Finally, if you’re just checking to see if there are any rows in the table, you can ignore all of the above and use the HasRows property as in

    if(dr.HasRows)
    {
       ....
    

    although if you’re using the while(dr.Read()) syntax, the code in the while loop will only execute if there are rows in the first place, so the HasRows could potentially be unnecessary if you don’t want to do anything with no results. You would still want to use it if you want to return a message like “no results found”, of course..

    Edit – Added

    I think there’s a problem also with the line

    if(dr1["CarId"] != DBNull.Value)
    

    You should be using if DataReader’s IsDbNull() method. as in

    if(dr.IsDbNull("CarId"))
    

    Sorry I missed that the first time around.

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

Sidebar

Related Questions

I have a datareader and i want to return collection of rows from it,
I have a query to return random distinct rows from an Access database. Here
I have a datareader that binds a sql select (with 10 columns from table1)
In my C# code I have an ODBC datareader that's the results of a
I have DataReader that holds the results from a stored procedure caal. The results
I have using the following code for developing tabs. $(document).ready(function() { $('#container-1').tabs(); } ....
I'm using the OleDB DataReader to get data from my excel files (but this
I'm using entity framework 4. I have a stored procedure that just updates one
I have this slice of code IQueryable<Dealer> dealers = from dealer in Dealers join
I'm developing a flash application that is getting information from a remote server using

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.