Solved

ComboBoxes loaded by multiple SQL query?

Posted on 2006-06-28
2
240 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Title # Comments Views Activity
How to reference objects of the calling class without passing them as parameters 29 206
Wav problem 4 28
Hey Audio! 9 44
Calculate number of nights between two dates 5 53
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

839 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