[英]Query Won't Work in ASP.net
I am trying to hide this button if the query result is = to btn1.CommandArgument. 如果查询结果为btn1.CommandArgument =,我试图隐藏此按钮。
The query works, because I have tested it, but the whole solution is not work. 该查询有效,因为我已经对其进行了测试,但是整个解决方案均无效。
If I replace 如果我更换
myCommand.ExecuteScalar().ToString()
in the if
statement to the query result, the button is hidden. 在查询结果的if
语句中,按钮是隐藏的。
I have looked several times, but can't find any problems. 我看了好几次,但是找不到任何问题。 Thank you. 谢谢。
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;
}
}
}
You need to execute the .ExecuteScalar()
call only once ! 您只需执行一次 .ExecuteScalar()
调用! Grab the result (of type object
) and then check to make sure it's not null
and if it is, call .ToString()
on it and compare to the other string you want to check against: 抓取结果(类型为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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.