gridview不显示数据 求帮助!!!
http://localhost:54658/project/ConsultServerDetail.aspx?
id=01
想借助传来的id值在gridview中进行相应的查询 出不来 求高手指点!!!!不胜感激!!!
.cs代码如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class ConsultServerDetail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind(); //如果以一次打开页面执行,如果单击其他按钮就不执行
}
}
public void Bind()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
if (Request.QueryString["id"] != null)
{
int Id = int.Parse(Request.QueryString["id"]);
string cmd = "SELECT title,description FROM consultServer WHERE id='" + Id + "'";
SqlDataAdapter da = new SqlDataAdapter(cmd, conn);
DataSet ds = new DataSet();
//conn.Open();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
------解决方案-------------------- string cmd = "SELECT title,description FROM consultServer WHERE id=" + Id;
------解决方案--------------------你的int Id是整型,WHERE id='" + Id + "'";这样查询表示ID是字符串,数据类型不统一。
直接
string cmd = "SELECT title,description FROM consultServer WHERE id=" +Request.QueryString["id"] ;
------解决方案--------------------da.Fill(ds);这里再加一个参数试试,第二个参数是表名。
GridView1.DataSource = ds.table["表名"];
GridView1.DataBind();