怎样利用下拉菜单,在数据库中插入数据?

jasonf512 2008-05-06 11:55:42
怎样利用下拉菜单,在数据库中插入数据?我做了代码,但是,还差下拉菜单中的数据,无法读入。代码如下,请帮忙修改一下。
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string connStr = ConfigurationManager.ConnectionStrings["schoolConnectionString"].ConnectionString;
SqlConnection Con = new SqlConnection(connStr);
Con.Open();
string InsertSql = "Insert Into Teacher_Pwd(username,name,'" +this.DropDownList1.Text+ "')values('" + username.Text + "','" + name.Text + "','" + pwd.Text + "')";
SqlCommand com = new SqlCommand(InsertSql, Con);
com.ExecuteNonQuery();
SqlDataAdapter ada = new SqlDataAdapter("Select * From Teacher_Pwd where name = '" + username.Text + "'", Con);
DataSet ds = new DataSet();
ada.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
Con.Close();
Response.Write("<script language=javascript>alert('添加成功!')</script>");
}
catch
{
Response.Write("<script language=javascript>alert('添加失败!')</script>");
}

}
...全文
103 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jasonf512 2008-05-06
  • 打赏
  • 举报
回复
我已经解决了~~谢谢~~~
t88266236 2008-05-06
  • 打赏
  • 举报
回复
name用中括号括起来[name]
还有你是不是想获取dropdownlist选中项的value,而不是选中项的text
jia818915 2008-05-06
  • 打赏
  • 举报
回复
这么写
DropDownList1.selectedvalue.text;(selectedvalue好像是这么写,你再查一下)
懒牛科技 2008-05-06
  • 打赏
  • 举报
回复
从dropdownlist取得列名,应该可以的
你断点跟踪下.看看InsertSql 得到的值是否可以直接在sql中执行!
jasonf512 2008-05-06
  • 打赏
  • 举报
回复
对。正是这个意思。请问该怎么办?
juge001 2008-05-06
  • 打赏
  • 举报
回复
string InsertSql = "Insert Into Teacher_Pwd(username,name,'" +this.DropDownList1.Text+ "')values('" + username.Text + "','" + name.Text + "','" + pwd.Text + "')";

??

你的表格的列名怎么会是动态的??

你想从DropDownList1里面取吗?

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧