现在又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();
}
}前边几行是我的想法,可是总是有点问题,不知道哪位大牛能帮我解决一下
现在我的想法是设置一个循环,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();
}
}前边几行是我的想法,可是总是有点问题,不知道哪位大牛能帮我解决一下
for(int i=1;i<=20;i++)
{
DropDownList ddl = this.form1.FindControl("DropDownList" + i) as DropDownList;
if(ddl != null)
{
//to do...
}
}
{
DropDownList ddl = this.Form.FindControl("DropDownList" + i) as DropDownList;
if (ddl != null)
{
int a = ddl.SelectedValue;这一行显示无法将string的字符串转化成int,这个怎么处理啊
int a = int.Parse(ddl.SelectedValue);
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();
}
}
}
}
请问哪里出现了问题么?谢谢您了
//必须强制转换
{
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();
}
}
}
}
按您这个方法试了试,还是无法将selectedvalue值存储到数据库里,数据库没有任何反应。score那一列还是null的状态
SqlCommand cmd = new SqlCommand(string.Format("update zonghe_score set score={0} where id={0}",a),con);