【问题标题】:How to store the value from radiobuttonlist to database in C# ASP.NET如何在 C# ASP.NET 中将单选按钮列表中的值存储到数据库中
【发布时间】:2015-04-23 16:52:13
【问题描述】:

我需要帮助将单选按钮列表中的值存储到 SQL Server 2008。我的代码工作正常,但是当我检查数据库时,如果单选按钮列表中的值已存储,则数据库中没有任何内容。

asp.net

    <p>
       Hypertension<asp:RadioButtonList 
        ID="RadioButtonList1" 
        RepeatColumns ="2" RepeatDirection = "vertical" 
        RepeatLayout= "table" runat="server" 
        AutoPostBack="True" 
         onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" 
         Width="250px" Height="10px" style="margin-left: 0px" BorderStyle="None" 
          CellPadding="1" CellSpacing="1">
          <asp:ListItem Value="rbtrue">TRUE</asp:ListItem>
       <asp:ListItem Selected="True" Value="rbfalse">FALSE</asp:ListItem>
      </asp:RadioButtonList>
   </p> 

在 C# 中:

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Response.Redirect("WebForm1.aspx");
        string selectedValue1 = this.RadioButtonList1.SelectedValue;
        string selectedValue2 = this.RadioButtonList2.SelectedValue;

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TumorRegistryConnectionString1"].ConnectionString))
        {
            string sql = "Insert into tbTRcBase(HPN,HPNTreatement,ISH,ISHTreatment,Asthma,AsthmaTreatment,DM,DMTreatment,OtherCo,OtherCoTreatment,SecondHandSmoke,Smoker,StopSmoking,Occupation,CancerFamilyHistory,FamilyWithCancer,ParentWithCancer) value(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17)";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("@d1", selectedValue1);
            cmd.Parameters.AddWithValue("@d2", selectedValue2);

            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                int result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return;
            }
          }
      } 

【问题讨论】:

  • 数据库中保存了什么?
  • SqlCommand 执行没有任何错误?
  • SQL 命令应该是 Insert into tbl() values()。你提到了价值
  • 已经修复了,但仍然没有变化

标签: c# asp.net sql-server radiobuttonlist


【解决方案1】:

如果你使用这个

string selectedValue2 = this.RadioButtonList2.SelectedValue;

您将获得选择的列表按钮的值,即 rbtrue 或 rbfalse

如果你想获得选中的radiolistbuttons 文本使用这个

 string selectedValue2 = this.RadioButtonList1.SelectedItem.Text;

试试这个查询

string sql = "Insert into tbTRcBase(HPN,HPNTreatement,ISH,ISHTreatment,Asthma,AsthmaTreatment,DM,DMTreatment,OtherCo,OtherCoTreatment,SecondHandSmoke,Smoker,StopSmoking,Occupation,CancerFamilyHistory,FamilyWithCancer,ParentWithCancer) values(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17)";

希望对你有帮助

【讨论】:

  • 这可行,但我的问题是它没有出现在数据库中
  • 检查你的查询有错误你使用值而不是值
  • 感谢查询错误已经修复了该问题,我尝试了 .SelectedItem.Text 属性,但是当我尝试提交表单时,c# 代码出现错误,“错误处理的 nullexception”
  • 如果您不想将值传递给剩余参数,只需将它们留空并尝试仅当您在 db 中为所有参数都允许空值时才有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 2012-12-23
相关资源
最近更新 更多