[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C# ADO.NET get cell values first row only

Posted on 2011-09-14
9
Medium Priority
?
355 Views
Last Modified: 2013-12-17
This sample code opens an excel file and loads the values in a data reader. I would like to read the cell values of the first row only and put that in a combobox.

This is what I have so far:

            OleDbConnection dbConnection = new OleDbConnection(_fileName);
            dbConnection.Open();
            try
            {
                OleDbCommand dbCommand = new OleDbCommand("SELECT * FROM [" + this.cboWorksheets.Text + "]", dbConnection);
                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                int i = 0;
                while (dbReader.Read())
                {
                    if (i == 0)  // first row only
                    {
 
                    }
                    i++;
                }
            }
            finally
            {
                dbConnection.Close();
            }
0
Comment
Question by:conrad2010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 13

Expert Comment

by:LIONKING
ID: 36539613
Since you're only grabbing the values of the first row (of the reader) you don't need a while loop.

Try something like:

OleDbConnection dbConnection = new OleDbConnection(_fileName);
            dbConnection.Open();
            try
            {
                OleDbCommand dbCommand = new OleDbCommand("SELECT * FROM [" + this.cboWorksheets.Text + "]", dbConnection);
                OleDbDataReader dbReader = dbCommand.ExecuteReader();

if(dbReader.HasRows())
{
dbReader.Read();
myText1.text=dbReader.Item("Name").toString();
}
          }
            finally
            {
                dbConnection.Close();
            }
0
 

Author Comment

by:conrad2010
ID: 36539649
dbReader.Item is not showing up in Intellisense...
0
 
LVL 13

Expert Comment

by:LIONKING
ID: 36539671
Sorry... Try
dbReader["Name"]
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:conrad2010
ID: 36539710
ok, that worked... another error:

Exception Details: System.IndexOutOfRangeException: Name

I am looking for the value in the cells that have data and I don't know the call values...
0
 

Author Comment

by:conrad2010
ID: 36539714
call values = cell values
0
 
LVL 13

Expert Comment

by:LIONKING
ID: 36539718
"Name" is an invalid name for a column in the dbReader. If you don't have the names of the columns, then you'll have to use the column number. Something like:

dbReader[0] or dbReader[1]
0
 

Author Comment

by:conrad2010
ID: 36539767
ok, that worked... now the trick is that I don't know how many columns to retrieve, there could be 5, or 10 or 20... how can I loop through the cells (in that one row) that are not empty?
0
 
LVL 13

Accepted Solution

by:
LIONKING earned 2000 total points
ID: 36539786
Have you tried using the FieldCount property of the dbReader to loop through the columns?
Try that and let us know.
0
 

Author Closing Comment

by:conrad2010
ID: 36539936
Good deal!
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 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