C# 在网格视图中使用Select语句时在输出中获取空结果
这就是我所做的 我创建了一个C# 在网格视图中使用Select语句时在输出中获取空结果,c#,sql-server,ado.net,C#,Sql Server,Ado.net,这就是我所做的 我创建了一个GridView并选择数据键名称作为id,在id的基础上,我想显示DetailsView。这里是CS代码 using (SqlConnection con1 = new SqlConnection("Data Source= IA; initial catalog =aip; integrated Security=true;")) { con1.Open(); SqlCommand cmd = new SqlCommand("select * fro
GridView
并选择数据键名称作为id
,在id的基础上,我想显示DetailsView
。这里是CS代码
using (SqlConnection con1 = new SqlConnection("Data Source= IA; initial catalog =aip; integrated Security=true;"))
{
con1.Open();
SqlCommand cmd = new SqlCommand("select * from Pm where user_id='" +(String)Session["uid"]+ "'", con1);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con1.Close();
}
然后我在SelectedIndexChanged
上创建DetailsView
方法:
但在选择“选择”选项时,它在输出中显示空的DetailsView
这是代码图像
您没有使用
数据集。所以你可以这样做:
da.Fill(ds,"tbl");
GridView1.DataSource = ds.Tables[0];
或者使用数据表
:
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
此外,您还应始终避免使用。大概是这样的:
SqlCommand cmd = new SqlCommand("select * from Pm where user_id=@userId", con1);
cmd.Parameters.AddWithValue("@userId", (String)Session["uid"]);
还可以看看这个:您需要设置DataTable而不是DataSet
GridView1.DataSource = ds.Tables["Pm"];
@PIYUSH Itspk请检查查询并替换
SqlCommand cmd = new SqlCommand("select * from Pm where user_id=" +Session["uid"].tostring()+ ", con1);
我想如果您有任何其他疑问,请通知我先生,实际上,在网格视图中,数据显示正确,但我希望在“选择”选项上选择特定行时,“详细视图”将打开,并与“网格视图”链接。我还在问题中附上了该代码的图像,请查看,并建议我该怎么做。