SQL表:
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拿到桌子:
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)
{
}
}我的ASP.net页面中有以下标签:<asp:Label ID="lblMessage" Font-Size="x-small" runat="server" Text="" ClientIDMode="Static"></asp:Label>
我怎样才能这样更新标签:
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 />";
}发布于 2015-03-04 17:14:26
按照我的理解,您需要使用不包含"SpecialityText“的行来更新您的标签。问题是你不知道怎么做。
假设在您的页面中使用的DataSet在获取它之后仍然被命名为myDataSet,下面的代码应该可以做到这一点。请注意,我假设您已经导入了一些数据访问名称空间。DBNull.Value应该替换为您的助手使用的空表示形式。
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;发布于 2015-03-04 17:13:17
我看不出你所显示的2for循环有什么不同。
如果它只是一个表,您应该使用DataTable而不是DataSet
你可以这样做:
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 />";
}
}然后,如果需要,添加其他块。您也可以使用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
}https://stackoverflow.com/questions/28860134
复制相似问题