?
Solved

C# ADO.NET get cell values first row only

Posted on 2011-09-14
9
Medium Priority
?
351 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 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