Solved

C# ADO.NET get cell values first row only

Posted on 2011-09-14
9
345 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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