Solved

C# ADO.NET get cell values first row only

Posted on 2011-09-14
9
347 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

813 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

11 Experts available now in Live!

Get 1:1 Help Now