如果查询结果为= to btn1.CommandArgument,我试图隐藏此按钮。
查询有效,因为我已对其进行了测试,但整个解决方案无效。
如果我更换
myCommand.ExecuteScalar().ToString()
在查询结果的if
语句中,该按钮被隐藏。
我看了好几次,但找不到任何问题。谢谢。
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Button btn1 = (Button)e.Item.FindControl("addFollowerButton");
// request Query string
var querystring = Request.QueryString["ProjectId"];
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string select = "Select ProfileId from Project_Follower Where ProjectId = @ProjectId";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(select, myConnection);
myCommand.Parameters.AddWithValue("@ProjectId", querystring);
myCommand.ExecuteScalar();
if (myCommand.ExecuteScalar().ToString() == btn1.CommandArgument.ToString())
{
Button hdn = (Button)e.Item.FindControl("addFollowerButton");
btn1.Visible = false;
}
}
}
答案 0 :(得分:1)
您只需要执行.ExecuteScalar()
来电一次!抓取结果(object
类型),然后检查以确保它不是null
,如果是,请在其上调用.ToString()
并与要检查的其他字符串进行比较:
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(select, myConnection);
myCommand.Parameters.AddWithValue("@ProjectId", querystring);
object result = myCommand.ExecuteScalar();
if (result != null && result.ToString().Equals(btn1.CommandArgument.ToString()))
{
Button hdn = (Button)e.Item.FindControl("addFollowerButton");
btn1.Visible = false;
}
}