弱弱的问:GRIDVIEW数据绑定问题~~
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session.Count == 0)
{
Response.Redirect("login.aspx");
}
else
{
string str = "select [zuname] from [zu-username] where username='" + Session["strUserName"].ToString().Trim() + "'";
SqlConnection con = db.createconnection();
con.Open();
SqlCommand cmd = new SqlCommand(str, con);
SqlDataReader sdr = cmd.ExecuteReader();
//con.Close();
if (sdr.Read())
{
string zu = sdr["zuname"].ToString();
con.Close();
switch (zu)
{
case "第一组":
//do something;
SqlConnection con1 = db.createconnection();
con1.Open();
string str1 = "select [username] from [zu-username] where zuname='"+zu+"'";
SqlDataAdapter da1 = new SqlDataAdapter(str1, con1);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "zu1");
this.GridView1.DataSource = ds1;
this.GridView1.DataBind();
this.Label1.Text = Session["strUserName"].ToString().Trim();
con.Close();
break;
case "第二组":
//do something
break;
}
}
}
}
}
在this.GridView1.DataBind();断点,正常,但是gridview没有绑定数据,数据库是有数据的。为什么,身边没人学这个的,每次遇到弱弱的问题就上来问,感谢大家~~~
------解决方案--------------------string str1 = "select [username] from [zu-username] where zuname= '"+zu+" '";
查询分析器里边执行过吗?取出数据了吗?
------解决方案--------------------给段代码你 自己参照下吧
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
FillDgd();
}
}
private void FillDgd()
{
SqlConnection cn = new SqlConnection(_con);
SqlDataAdapter ad = new SqlDataAdapter("select * from jobs",cn);
DataSet ds = new DataSet();
ad.Fill(ds,"Job");
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataMember = "Job";
this.DataBind();
}
------解决方案--------------------先检查下ds1.Tables["zu1"].Rows.Count是否为0 如果为0 就没数据
还有就是你的con1没必要打开 而且你也没关闭 你只是两次关闭了con 并没关闭con1
------解决方案--------------------......
if (sdr.Read())
{
string zu = sdr["zuname"].ToString();
sdr.Close();
switch (zu)
{
case "第一组":
//do something;