?
Solved

ComboBoxes loaded by multiple SQL query?

Posted on 2006-06-28
2
Medium Priority
?
245 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 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

Technology Partners: 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

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.
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

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