Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 18th, 2005, 02:29 PM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# If test not working

I'm trying to do an if test to disply a message if a record value in a db is less than 3, but it displays the message always, see code below. Also I'm looking for some sample code of a comboBox control in C#, I want to populate the combox using a stored procedure, any examples appreciated.

SqlDataAdapter da = new SqlDataAdapter
    ("SELECT MAX(FeedbackId) as FeedbackId, ComfortLevel from Feedbacks GROUP BY FeedbackId, ComfortLevel", StrConnection);
    DataSet ds = new DataSet();
     da.Fill(ds,"Feedbacks");

     foreach (DataRow dr in ds.Tables["Feedbacks"].Rows)

     {
         int id = (int)dr["ComfortLevel"];
         if (id == 3)
            {
                 message2.Text = "Contact a tutor";
            }

     }
 
Old December 18th, 2005, 03:07 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The use of a loop there doesn't entirely make sense.

The message could get set on one row, but then following rows may not hit the if. So the logic isn't really logical.

-Peter
 
Old December 18th, 2005, 04:00 PM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But the SQL should bring back the last record in the table, therefore there will only be one row inside the loop?

 
Old December 18th, 2005, 04:03 PM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry the oroginal code did not include my less than test

SqlDataAdapter da = new SqlDataAdapter
    ("SELECT MAX(FeedbackId) as FeedbackId, ComfortLevel from Feedbacks GROUP BY FeedbackId, ComfortLevel", StrConnection);
    DataSet ds = new DataSet();
     da.Fill(ds,"Feedbacks");

      foreach (DataRow dr in ds.Tables["Feedbacks"].Rows)

      {
          int id = (int)dr["ComfortLevel"];
           if (id < 3)
            {
                 message2.Text = "Contact a tutor";
            }

      }


 
Old December 18th, 2005, 04:09 PM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Whats the best way of getting the value from a column inside a table row? Avoid using a foreach loop? In classic ASP I would just do the following:

Create a recordset with the sql above then:

<% If RS_Message("ComfortLevel") < 3 Then Response.Write("Contact a tutor") %>

 
Old December 19th, 2005, 10:56 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry Peter you was correct, my sql was bringing back more than one row, I'm slightly embarrassed, here is the code working correctly:

 SqlDataAdapter da = new SqlDataAdapter
    ("SELECT * FROM dbo.Feedbacks WHERE (FeedbackId = (SELECT MAX(FeedbackID) FROM Feedbacks))", StrConnection);
    DataSet ds = new DataSet();
     da.Fill(ds,"Feedbacks");

      foreach (DataRow dr in ds.Tables["Feedbacks"].Rows)

      {
          int id = (int)dr["ComfortLevel"];
           if (id < 3)
            {
                          message2.Text = "Contact a tutor";
            }

      }

 
Old December 19th, 2005, 11:21 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Using the dataset, if you know that you'll only get one row, you can just access the row directly:

  int id = (int)ds.Table["Feedbacks"].Rows[0]["ComfortLevel"];


If you are doing a data aggregation and are looking for only a single value you could further simplify your code to something like this:

  SqlCommand objCmd = new SqlCommand("SELECT * FROM dbo.Feedbacks WHERE (FeedbackId = (SELECT MAX(FeedbackID) FROM Feedbacks))", new SqlConnection(StrConnection));
  objCmd.Connection.Open();
  int id = (int)objCmd.ExecuteScalar();
  objCmd.Connection.Close();
  if (id < 3)
  {
    message2.Text = "Contact a tutor";
  }

http://msdn.microsoft.com/library/en...calartopic.asp

-Peter
 
Old December 20th, 2005, 08:18 AM
Authorized User
 
Join Date: May 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peter






Similar Threads
Thread Thread Starter Forum Replies Last Post
window.opener working .... not working alyeng2000 Javascript How-To 5 January 5th, 2007 08:05 AM
Web.Config..Working or Not Working peace95 ASP.NET 1.0 and 1.1 Basics 1 September 18th, 2006 06:53 AM
C# If test not working RichardOrmiston ASP.NET 1.0 and 1.1 Professional 0 December 18th, 2005 02:23 PM
Local COM working , but not working at Web Serv nagen111 .NET Web Services 3 February 19th, 2005 04:22 AM
IIS test not working kevi3032 ASP.NET 1.0 and 1.1 Basics 21 December 20th, 2004 02:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.