2017-07-04 108 views
0

这里使用SELECT语句时,我做了什么 我做一个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(); 
} 

然后我就SelectedIndexChangedDetailsView方法: 却是露出空DetailsView在输出上选择“选择”选项 这里是代码图像 enter image description here

回答

1

你已经使用了DataSet不是DataTable。所以,你可以做这一点:

da.Fill(ds,"tbl"); 
GridView1.DataSource = ds.Tables[0]; 

或者使用DataTable代替:

DataTable dt = new DataTable(); 
da.Fill(dt); 
GridView1.DataSource = dt; 

而且你应该总是使用parameterized queries避免SQL Injection。事情是这样的:

SqlCommand cmd = new SqlCommand("select * from Pm where [email protected]", con1); 
cmd.Parameters.AddWithValue("@userId", (String)Session["uid"]); 

也看看这个:Can we stop using AddWithValue() already?

+0

先生, 实际上,在网格视图中,数据被正确地表示,但我想在上的“选择”选项“详细视图”中选择一个特定的行会被打开,并其与“网格视图联'。我还附上了我的问题中的代码的形象,请看看它,并建议我该怎么做。 –

0

你需要设置的,而不是DataSet中的DataTable

GridView1.DataSource = ds.Tables["Pm"]; 
0

@PIYUSH Itspk请查询并更换

SqlCommand cmd = new SqlCommand("select * from Pm where user_id=" +Session["uid"].tostring()+ ", con1); 

,我认为它帮助你,如果其他任何查询,请通知我