Solved

ComboBoxes loaded by multiple SQL query?

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

18 Experts available now in Live!

Get 1:1 Help Now