2

Here is a summary of the behavior: while loop iterates to the end of the recordset, retrieves values. Values are then displayed in their respective places.

The issue is, I only want to display one set of values. Using while displays as many values as are records in the table being queried. Sample below:

What should display:

<div>
    <div>Service One is Stopped</div>
    <div>Service Two is Running</div>
    <div>Service Three is Stopped</div>
</div>

What actually happens:

<div>
    <div>Service One is </div> 
    <div>Service Two is Running</div>
    <div>Service Three is </div>
    <div>Service One is </div>
    <div>Service Two is </div>
    <div>Service Three is Running</div>
    <div>Service One is Stopped</div>
    <div>Service Two is </div>
    <div>Service Three is </div>
</div>

Below is the offending code. I apologize if this is a trivial question. I'm shaking off 2+ years of rust not writing code, and sometimes even the simple things get you :-). Many thanks for the assistance!!!

@using System.Configuration;
@using System.Data.SqlClient;
@using System.Data;
@using System.Linq;

@{
  System.Data.SqlClient.SqlConnection cn = null;
  cn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString());
  cn.Open();
  System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT DISTINCT * FROM servicehealth", cn);
  var myreader = cmd.ExecuteReader();
}
@{
int rowCount = 0;
}
@while (myreader.Read())
{
        rowCount++;
        string service1status = "";
        string service2status = "";
        string service3status = "";
        string servicename = @myreader["servicename"].ToString();

        switch (servicename.ToLower())
        {
            case "serviceone":
                service1status = @myreader["servicestate"].ToString();
                break;
            case "servicetwo":
                service2status = @myreader["servicestate"].ToString();
                break;
            case "service3":
                service3status = @myreader["servicestate"].ToString();
                break;
        }

<div>
    <div>Service One is @service1status</div>
    <div>Service Two is @service2status</div>
    <div>Service Three is @service3status</div>
</div>
}

EDIT: Here's an abbreviated snapshot of the table in question:

ServiceId                   |  ServiceName  |   ServiceState  |  StartTrackTime  |  LastUpdated
1111-11111-111111-11111111  |  ServiceOne   |   Stopped       |  1/30/2012 0:00  |  1/30/2012 17:57
2222-22222-222222-22222222  |  ServiceTwo   |   Running       |  1/30/2012 0:00  |  1/30/2012 17:57
3333-33333-333333-33333333  |  ServiceThree |   Running       |  1/30/2012 0:00  |  1/30/2012 17:57
1
  • Having the servicehealth columns and data would help diagnose this. Commented Jan 31, 2012 at 5:03

2 Answers 2

2

Looks like you could just output the service and it's status during each iteration of the while and place it between the containing div tags. You should get one line for each service found.

<div>
@while (myreader.Read())
{
     string servicestatus = servicestatus = @myreader["servicestate"].ToString();
     string servicename = @myreader["servicename"].ToString();
    <div>@servicename is @servicestatus</div>

}
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - much cleaner solution. Would just add a order by clause in the ... inline SQL...... :)
0

You need to move your string declarations and div tags outside of the while loop:

@{
int rowCount = 0;
string service1status = "";
string service2status = "";
string service3status = "";

while (myreader.Read())
{
    rowCount++;
    string servicename = @myreader["servicename"].ToString();
    switch (servicename.ToLower())
    {
        case "serviceone":
            service1status = @myreader["servicestate"].ToString();
            break;
        case "servicetwo":
            service2status = @myreader["servicestate"].ToString();
            break;
        case "service3":
            service3status = @myreader["servicestate"].ToString();
            break;
    }
}
}
<div>
    <div>Service One is @service1status</div>
    <div>Service Two is @service2status</div>
    <div>Service Three is @service3status</div>
</div>

3 Comments

I thought so too - however, the servicestatus variables are not recognized once I do this
Renders CS0103: The name 'service1status' does not exist in the current context
Updated to put the while loop in the same code block: @{ while() } instead of @while() as it is self contained scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.