Solved

C# ADO.NET get cell values first row only

Posted on 2011-09-14
9
349 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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