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