简体   繁体   中英

Add SQL row to a ASP label based on a condition

SQL Table:

ID  CreatedDate     MessageText     CreatedBy   IsActive    ClientText      LocationText    ProviderText    SpecialtyText
1   March 4, 2015   dsdfsdf         Test1       False               
2   March 4, 2015   dsdfsdf         Test2       True                
3   March 4, 2015   dsdfsdf         Test2       True        Test Client     location1       Test2           MD
4   March 4, 2015   This is to add  Test1       False       Test Client     location2       Test3           DD

Getting the table:

using (SqlConnection conn = new SqlConnection(gloString))
{
    string strSql = @"SELECT * FROM [DB].[dbo].[table2]";
    try
    {
        // create data adapter
        SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
        // this will query your database and return the result to your datatable

        myDataSet = new DataSet();
        da.Fill(myDataSet);
    }
    catch (Exception ce)
    {
    }
}

I have the following label in my ASP.net page: <asp:Label ID="lblMessage" Font-Size="x-small" runat="server" Text="" ClientIDMode="Static"></asp:Label>

How can I update the label like this:

lblMessage.Text += "<b>ALL MESSAGES:</b> <br />";
foreach (ROW WHERE COLUMN {CLIENTTEXT}, {LOCATIONTEXT}, {PROVIDERTEXT}, AND {SPECIALTYTEXT} IS EMPTY)
{
    lblMessage.Text += {CREATEDDATE} + " - " + {MESSAGETEXT} + "<br /><br />";
}
lblMessage.Text += "<hr />";
lblMessage.Text += "<b>SPECIFIC MESSAGES:</b> <br />";

foreach (ROW WHERE COLUMN {CLIENTTEXT}, {LOCATIONTEXT}, {PROVIDERTEXT}, OR {SPECIALTYTEXT} IS NOT EMPTY)
{
    lblMessage.Text += {CREATEDDATE} + " - " + {MESSAGETEXT} + "<br /><br />";
}

I don't see a difference for the 2 for loops you showed.

If it's just one table you should be using a DataTable instead of a DataSet

You can do it like this:

foreach (DataRow row in table.Rows)//table is a DataTable
{
    var clientText = row["ClientText"].ToString();
    var locationText = row["LocationText"].ToString();
    var providerText = row["ProviderText"].ToString();
    var specialtyText = row["SpecialtyText"].ToString();
    var createdDate = row["CreatedDate"].ToString();
    var messageText = row["MessageText"].ToString();

    if (string.IsNullOrEmpty(clientText) && 
        string.IsNullOrEmpty(locationText) && 
        string.IsNullOrEmpty(providerText) && 
        string.IsNullOrEmpty(specialtyText))
    {
        lblMessage.Text += createdDate + " - " + messageText + "<br /><br />";
    }
}

then add else block if you need. You can also use linQ

foreach (var clientText in from DataRow row in table.Rows
                           let clientText = row["ClientText"].ToString()
                           let locationText = row["LocationText"].ToString()
                           let providerText = row["ProviderText"].ToString()
                           let specialtyText = row["SpecialtyText"].ToString()
                           where string.IsNullOrEmpty(clientText) && 
                                 string.IsNullOrEmpty(locationText) && 
                                 string.IsNullOrEmpty(providerText) && 
                                 string.IsNullOrEmpty(specialtyText)
                           select clientText)
{
    //some code here
}

The way I get it, you want to update your label using the rows that dont contain a "SpecialityText". The problem is you dont know how.

Let's say the DataSet you use in your page is still named myDataSet after you fetch it, the following code should do the trick. Please note I assumed you have some data access namespace imported. DBNull.Value should be replaced with whathever the null representation your helper uses.

string specific = "";
string generic = "";

foreach (DataRow r in myDataSet.tables[0].Rows){
    //Add all your conditions here. It seems you want two groups of messages
    if (r["SpecialtyText"].ToString == ""){
        generic += r["CreatedDate"] + " - " + r["MessageText"]  + "<br /><br />";
    }
    else{
        specific += r["CreatedDate"] + " - " + r["MessageText"] + "<br /><br />";
    }
}

lblMessage.Text = generic + "<hr /><b>SPECIFIC MESSAGES:</b> <br />" + specific;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM