asp.net网页可以运行但是一直执行else的内容,到底哪里错了?
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != "" )
{
SqlConnection con = new SqlConnection("Data Source=(local)\\SQLEXPRESS; Initial Catalog=stan; Integrated Security=True");//创建连接
con.Open();//打开连接
string strsql = "select * from news where id = '" + Request.QueryString["id"] + "'";//查询数据
SqlDataAdapter da = new SqlDataAdapter(strsql, con);
DataSet ds = new DataSet();//填充数据
int count = da.Fill(ds, "table");
if (count > 0)
{
Label1.Text = ds.Tables["table"].Rows[0]["title"].ToString();//填充控件
Label2.Text = ds.Tables["table"].Rows[0]["content"].ToString(); //填充控件
Label3.Text = ds.Tables["table"].Rows[0]["author"].ToString();//填充控件
Label4.Text = ds.Tables["table"].Rows[0]["time"].ToString();//填充控件
}
else
{
Response.Redirect("default.aspx");//页面跳转
}
}
else
{
Response.Redirect("default.aspx");
}
} ASP.NET label 数据库
[解决办法]
咦,不对,你是怎么进入这个页面的,QueryString就没传值吧?
[解决办法]
if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
}
试试,如果还是执行 else 那么就是你的 Request.QueryString["id"] 没有任何值了。
[解决办法]
int count = da.Fill(ds, "table");
改成这样,试看看
da.Fill(ds, "table");
int count = ds.Tables.Rows.Count;