ComboBoxes loaded by multiple SQL query?

Posted on 2006-06-28
Last Modified: 2010-04-16
In an attempt to eliminate much C# code, I would like to make the following code work.

It compiles fine but errors upon inital loading to run. It errors by reading the second ComboBox in the While statement.

I've combined SQL statements in C# elsewhere if I place a ; between the SQL syntax.....

private void LoadComboBoxes()
string SQL = " SELECT DISTINCT datetime FROM TimeClock ORDER BY datetime ASC ; " +
                   " SELECT DISTINCT  namefirst FROM TimeClock ORDER BY namefirst ASC ; " +
                   " SELECT DISTINCT  namelast FROM TimeClock ORDER BY namelast ASC " ;

cn = new SqlConnection((ConfigurationSettings.AppSettings["cn"]);
SqlDataAdapter da = new SqlDataAdapter(SQL,cn);
SqlDataReader rdr = da.SelectCommand.ExecuteReader();

       cbxNameFirst.Items.Add(rdr["namefirst"].ToString());  // ERROR
Question by:kvnsdr
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
LVL 25

Accepted Solution

dstanley9 earned 250 total points
ID: 17002623
The DataReader will only give you the first ResultSet initially.  Are you wanting ALL unique first names, jast names, and dates, or just the unique COMBINATIONS of names and dates.  

If the former, use:

if rdr.NextResult()
if rdr.NextResult()

If the latter, use:

string SQL = " SELECT DISTINCT datetime. namefirst , namelast  FROM TimeClock ORDER BY datetime ASC";

Author Comment

ID: 17003037
Yes, you guessed right. I need DISTINCT on everything.

Thank you very much......

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call Controller Action Method from ASPX 2 31
VB.NET 2008 - SQL Timeout 9 51
tableview is not updating 1 28
Save json data from URL using SSIS 1 28
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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 Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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