现在又20个dropdownlist,标号分别为dropdownlist1一直到dropdownlist20.
现在我的想法是设置一个循环,20次,每次分别将dropdownlist里的seletedvalue值存储到数据库
但是dropdownlist后边的1到20这个编号我无法直接通过变量来控制,所以必须先进行字符串转换,然后再叠加,代码如下
 int a = 1;
        string ddl = "DropDownList" + a;
      Label1.Text = DropDownList(ddl).selectedvalue;
        if (a <= 24)
        {
            a=a+1;
            SqlConnection con = new SqlConnection("Server=.;database=Login;uid=sa;pwd=;");
            try
            {
                con.Open();                SqlDataAdapter da = new SqlDataAdapter("update zonghe_score set score=ddl where id=a", con);            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }前边几行是我的想法,可是总是有点问题,不知道哪位大牛能帮我解决一下

解决方案 »

  1.   


    for(int i=1;i<=20;i++)
    {
        DropDownList ddl = this.form1.FindControl("DropDownList" + i) as DropDownList;
        if(ddl != null)
        {
            //to do...
        }
    }
      

  2.   

     for (int i = 1; i <= 20; i++)
            {
                DropDownList ddl = this.Form.FindControl("DropDownList" + i) as DropDownList;
               
                if (ddl != null)
                {
                    int a = ddl.SelectedValue;这一行显示无法将string的字符串转化成int,这个怎么处理啊
      

  3.   


    int a = int.Parse(ddl.SelectedValue);
      

  4.   

    int a =  int.Parse( ddl.SelectedValue); 
      

  5.   

    谢谢您,可是我现在读取了一下发现还是无法将dropdownlist里的value值存储到数据库中,代码如下:
      protected void Button2_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 20; i++)
            {
                DropDownList ddl = this.Form.FindControl("DropDownList" + i) as DropDownList;
               
                if (ddl != null)
                {
                    int a = int.Parse(ddl.SelectedValue);
                    SqlConnection con = new SqlConnection("Server=.;database=Login;uid=sa;pwd=179425532;");
                    try
                    {
                        con.Open();                    SqlDataAdapter da = new SqlDataAdapter("update zonghe_score set score=a where id=a", con);
    //数据库是login,表是zonghe_score 字段是score//                }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
            
        }
    请问哪里出现了问题么?谢谢您了
      

  6.   

    int a = Convert.ToInt32(ddl.SelectedValue);
    //必须强制转换
      

  7.   

    update语句怎么会用SqlAdapter?奉劝楼主还是先把基础的ado.net弄清楚了再接着做,不然以后碰到类似的问题你还是不知道如何下手:  protected void Button2_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 20; i++)
            {
                DropDownList ddl = this.Form.FindControl("DropDownList" + i) as DropDownList;
             
                if (ddl != null)
                {
                    int a = int.Parse(ddl.SelectedValue);
                    SqlConnection con = new SqlConnection("Server=.;database=Login;uid=sa;pwd=179425532;");
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand(string.Format("update zonghe_score set score={0} where id={1}",a),con);
                        cmd.ExecuteNonQuery();                }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
           
        } 
      

  8.   

    谢谢您,我是个新手,不是学这个专业的,正在自学。
    按您这个方法试了试,还是无法将selectedvalue值存储到数据库里,数据库没有任何反应。score那一列还是null的状态
      

  9.   

    这句写错了:
    SqlCommand cmd = new SqlCommand(string.Format("update zonghe_score set score={0} where id={0}",a),con);
      

  10.   

    个人建议 把try catch 去掉,然后把错误发上来