?
Solved

ComboBoxes loaded by multiple SQL query?

Posted on 2006-06-28
2
Medium Priority
?
243 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
[X]
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
2 Comments
 
LVL 25

Accepted Solution

by:
dstanley9 earned 1000 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 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