我想用这种方法实现,但不出结果,大侠帮我改改:
 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;            
        }
    }

解决方案 »

  1.   

    DropDownList1的autopostback勾了吗?我也不太清楚,希望楼主能够解决
      

  2.   

    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();
      

  3.   

    <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList>
      

  4.   

    <asp:DropDownList ID="DropDownList1" runat="server" 
    AutoPostBack="True">
            </asp:DropDownList>
      

  5.   

    谢谢lanymy(﹎蓝言觅ぷ雨) 
    我把他改成
                        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();
     
      

  6.   

    你在数据库里写了存储过程没```
    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();这样应该可以了吧
      
      

  7.   

    Selecte * from MapDevice where DevOnlyId=11111111 ", con);
    select 多个e
      

  8.   

    不行啊,首先你的代码中有错
    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[] 对象。 
      

  9.   

    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("@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();之前的代码是手写的 没运行```
    你再看看这个``
      

  10.   

    写了
    运行后有错
    未能找到存储过程 'Selecte * from MapDevice where DevOnlyId='22222222''。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 未能找到存储过程 'Selecte * from MapDevice where DevOnlyId='22222222''。
      

  11.   

    在数据库里执行select * from MapDevice where DevOnlyId=22222222有数据啊
      

  12.   

    use 数据库名字
    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();     我人都搞糊涂了```
    照上面的写``
      

  13.   

    你的ID是int的还是 varchar的``