Solved

ComboBoxes loaded by multiple SQL query?

Posted on 2006-06-28
2
239 Views
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);
cn.Open();
SqlDataReader rdr = da.SelectCommand.ExecuteReader();

While(rdr.Read())
{
       cbxDateTime.Items.Add(rdr["datetime"].ToString());
       cbxNameFirst.Items.Add(rdr["namefirst"].ToString());  // ERROR
       cbxNameLast.Items.Add(rdr["namelast"].ToString());
}
rdr.Close();
cn.Close();
0
Comment
Question by:kvnsdr
2 Comments
 
LVL 25

Accepted Solution

by:
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:


While(rdr.Read())
{
       cbxDateTime.Items.Add(rdr["datetime"].ToString());
}
if rdr.NextResult()
{
While(rdr.Read())
{
       cbxNameFirst.Items.Add(rdr["namefirst"].ToString());
}
}
if rdr.NextResult()
{
While(rdr.Read())
{
       cbxNameLast.Items.Add(rdr["namelast"].ToString());
}
}

If the latter, use:

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

Author Comment

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

Thank you very much......
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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 …

776 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