做了一个简单的投票~从数据库的两个表中读出投票的主题及相对应的选项~但是点击radiobuttonlist中的一个~选择投票按键~实现的结果是数据库中相对应的选项应该加1~但是总是不能实现~想问各位高手怎么解决~投票按键中的代码如下:
SqlConnection Conn = new SqlConnection("server=.;database=hotel;uid=sa");
string dd = this.Request.QueryString["ID"].ToString();
string sql = "select ID,detail,num from votedetail where itemid='" + dd + "'";
SqlDataAdapter Comm = new SqlDataAdapter(sql, Conn);
DataSet ds = new DataSet();
Comm.Fill(ds, "votedetail");
RadioButtonList1.DataSource = ds.Tables["votedetail"].DefaultView;
RadioButtonList1.DataTextField = "detail";
RadioButtonList1.DataValueField = "ID";
RadioButtonList1.DataBind(); int count3 = Convert.ToInt16(ds.Tables[0].Rows[0]["num"]) + 1;
string newsql = "update votedetail set num='" + count3.ToString() + "' where ID='" + this.RadioButtonList1.SelectedValue + "'";
SqlCommand comm = new SqlCommand(newsql, Conn);
Conn.Open();
comm.ExecuteNonQuery();
Conn.Close();
Response.Write("<script> alert('投票成功!')</script>");其他都没有问题~就是where ID='" + this.RadioButtonList1.SelectedValue + "'这里出了问题~
所以点击投票按键~数据库的信息没有更新~
SqlConnection Conn = new SqlConnection("server=.;database=hotel;uid=sa");
string dd = this.Request.QueryString["ID"].ToString();
string sql = "select ID,detail,num from votedetail where itemid='" + dd + "'";
SqlDataAdapter Comm = new SqlDataAdapter(sql, Conn);
DataSet ds = new DataSet();
Comm.Fill(ds, "votedetail");
RadioButtonList1.DataSource = ds.Tables["votedetail"].DefaultView;
RadioButtonList1.DataTextField = "detail";
RadioButtonList1.DataValueField = "ID";
RadioButtonList1.DataBind(); int count3 = Convert.ToInt16(ds.Tables[0].Rows[0]["num"]) + 1;
string newsql = "update votedetail set num='" + count3.ToString() + "' where ID='" + this.RadioButtonList1.SelectedValue + "'";
SqlCommand comm = new SqlCommand(newsql, Conn);
Conn.Open();
comm.ExecuteNonQuery();
Conn.Close();
Response.Write("<script> alert('投票成功!')</script>");其他都没有问题~就是where ID='" + this.RadioButtonList1.SelectedValue + "'这里出了问题~
所以点击投票按键~数据库的信息没有更新~
解决方案 »
- 如何识别HTML中的不特定element
- 并行开发PLINQ的疑惑
- C#如何利用sharepoint的webService读取Library
- 为什么Form_load里用代码改不了button的backcolor属性
- 请教高手Web服务器跟分布式应用程序通信的问题,谢谢
- 连接oracle的com的组件是什么??
- 控件类型互相引用的问题
- 可能很难的问题,也许我太菜,请高手过来看看...
- VC6.0如何调用C#写的动态库
- 在一个项目中可不可以将C#的代码与VB.NET的模块集成到一起?容易实现吗?
- 使用 SetParent API 后 ToolStrip 失效问题
- 问一个UltraEdit操作的问题,达人入~
如:
<asp:ListItem Value="Value 1" Text="Item 1">Inner 1</asp:ListItem>
你检查是否设置了 Value属性
{ //绑定选项
SqlConnection Conn = new SqlConnection("server=.;database=hotel;uid=sa");
string dd = this.Request.QueryString["ID"].ToString();
string sql = "select ID,detail,num from votedetail where itemid='" + dd + "'";
SqlDataAdapter Comm = new SqlDataAdapter(sql, Conn);
DataSet ds = new DataSet();
Comm.Fill(ds, "votedetail"); RadioButtonList1.DataSource = ds.Tables["votedetail"].DefaultView;
RadioButtonList1.DataTextField = "detail";
RadioButtonList1.DataValueField = "ID";
RadioButtonList1.DataBind();
这里面RadioButtonList1.DataValueField = "ID";是绑定value啊~
this.RadioButtonList1.SelectedValue的值就是我选择的那个选项~选项是从数据库里读出来的~不是固定的~
请高手指教啊~
是radiobutton的属性值,控件有text属性,用来给外界显示的,value是用来后台处理的时候的值,就是前台的text对应的value,可以设置一样,也可以不一样,一般text是文本,value是编号之类的
DataTextField="aaid" DataValueField="aaname" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList> protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(RadioButtonList1.SelectedValue);
}
你跟踪看一下this.RadioButtonList1.SelectedValue有没有值呢,是不是正确的值呢
还有啊,你的数据库的id是什么类型,是int还是varchar,整型没有单引号
指定它的DataTextField 和DataValueField
然后才能用this.RadioButtonList1.SelectedValue来获取选定项的ID