关于dropdownlist的查询数据绑订到gridview上
我想用这种方法实现,但不出结果,大侠帮我改改
:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings[ "SQLCONNECTIONSTRING "]);
con.Open();
string search = this.DropDownList1.SelectedValue.ToString();
switch (search)
{
case "11111111 ":
SqlDataAdapter sda = new SqlDataAdapter( "Selecte * from MapDevice where DevOnlyId=11111111 ", con);
DataSet ds = new DataSet();
sda.Fill(ds, "MapDevice ");
this.GridView1.DataSource = ds.Tables[ "MapDevice "];
this.GridView1.DataBind();
break;
case "22222222 ":
SqlDataAdapter sdb = new SqlDataAdapter( "Selecte * from MapDevice where DevOnlyId=22222222 ", con);
DataSet da = new DataSet();
sdb.Fill(da, "MapDevice ");
this.GridView1.DataSource = da.Tables[ "MapDevice "];
this.GridView1.DataBind();
break;
default:
Response.Write( "出错了 ");
break;
}
}
[解决办法]
DropDownList1_SelectedIndexChanged
他的方法必须返回到 服务器才能处理 所以DropDownList1的autopostback属性 设置成true
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings[ "SQLCONNECTIONSTRING "]);
con.Open();
string search = "Selecte * from MapDevice where DevOnlyId= ' "+this.DropDownList1.SelectedValue.ToString()+ " ' ";
try
{
SqlDataAdapter sda = new SqlDataAdapter(search , con);
}
cath
{出错信息}
DataSet ds = new DataSet();
sda.Fill(ds, "MapDevice ");
this.GridView1.DataSource = ds.Tables[ "MapDevice "];
this.GridView1.DataBind();
[解决办法]
use 数据库名字
go
create proc a
(
@id int
)
as
select * from MapDevice where DevOnlyId=@id
你在数据库运行上面的这个代码
上面的叫存储过程```
存储过程的名字叫a
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings[ "SQLCONNECTIONSTRING "].Tostring(););
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "a ";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
SqlParameter para = new SqlParameter( "@id ",SqlDataType.Int);
para.Value = this.DropDownList1.SelectedValue;
cmd.Parameters.Add(para);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "MapDevice ");
this.GridView1.DataSource = ds.Tables[ "MapDevice "];
this.GridView1.DataBind();
我人都搞糊涂了```
照上面的写``