我想用这种方法实现,但不出结果,大侠帮我改改:
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;
}
}
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;
}
}
解决方案 »
- get { return ConfigurationManager.ConnectionStrings["Mutouren"].ConnectionString
- 再发一个动态绑定控件的问题
- 如何读取字符串中的每一个字符?
- asp.net网站中调用Discuz! X2.5论坛的数据,怎么调用谁做过,求指点!
- 遍历查找radiobutton控件
- 请问这样的类型怎样转换?
- jQuery 合并单元格
- 如何用另外的控件在后台代码中设置网页calendar控件的年月日值
- 像桌面应用程序那样,实现图片、文本文档等的浏览
- C#中application 和session的数组怎么声明和使用?
- 很奇怪,同样在虚拟主机上面,asp程序的session老不过期,.net2.0的一下就过期了
- 看似简单,其实不简单的问题,大家进来一下.
他的方法必须返回到 服务器才能处理 所以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();
AutoPostBack="True">
</asp:DropDownList>
我把他改成
SqlDataAdapter sda = new SqlDataAdapter(search , con);
DataSet ds = new DataSet();
sda.Fill(ds, "MapDevice");
this.GridView1.DataSource = ds.Tables["MapDevice"];
this.GridView1.DataBind();
无try catch
不然老出错
又有了下面的错!!帮忙,谢谢
第 1 行: '*' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '*' 附近有语法错误。源错误:
行 218: SqlDataAdapter sda = new SqlDataAdapter(search , con);
行 219: DataSet ds = new DataSet();
行 220: sda.Fill(ds, "MapDevice");
行 221: this.GridView1.DataSource = ds.Tables["MapDevice"];
行 222: this.GridView1.DataBind();
create proc a
(
@id int
)
as
select * from MapDevice where DevOnlyId=@id这个就不需要那么麻烦在程序里面写switch case 了你只要传个参数里面就行了protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = System.Configuration.ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].Tostring();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
SqlParameter [] para = new SqlParameter[1];
para[0] = new SqlParameter("@id",SqlDataType.Int);
para[0].Value = this.DropDownList1.SelectedValue;
cmd.Parameter.Add(para);
SqlDataAdapter sda = new SqlDataAdapter(search , con);
DataSet ds = new DataSet();
sda.Fill(ds, "MapDevice");
this.GridView1.DataSource = ds.Tables["MapDevice"];
this.GridView1.DataBind();这样应该可以了吧
select 多个e
para[0] = new SqlParameter("@id",SqlDataType.Int);
cmd.Parameter.Add(para);
应该是
para[0] = new SqlParameter("@id",SqlDbType.Int);
cmd.Parameters.Add(para);改好了,出现
SqlParameterCollection 仅接受非空的 SqlParameter 类型对象,不接受 SqlParameter[] 对象。
{
SqlConnection con = new SqlConnection();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = System.Configuration.ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].Tostring();
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();之前的代码是手写的 没运行```
你再看看这个``
运行后有错
未能找到存储过程 'Selecte * from MapDevice where DevOnlyId='22222222''。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 未能找到存储过程 'Selecte * from MapDevice where DevOnlyId='22222222''。
go
create proc a
(
@id int
)
as
select * from MapDevice where DevOnlyId=@id你在数据库运行上面的这个代码
上面的叫存储过程```存储过程的名字叫aSqlConnection 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(); 我人都搞糊涂了```
照上面的写``