【问题标题】:Insert data from radio button into database将单选按钮中的数据插入数据库
【发布时间】:2022-01-22 01:02:54
【问题描述】:

用 C# 构建一个表单应用程序,以将手机作为项目销售。我有两个单选按钮,用户根据他们想要现金或卡的付款方式进行检查。

如何根据用户的选择将该数据插入数据库?

【问题讨论】:

    标签: c# sql forms radio-button


    【解决方案1】:

    你可以试试这个,

    protected void Button1_Click(object sender, EventArgs e)
        {
            string cs = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
            SqlConnection cn = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into sample values(@payment)";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@payment", payment.SelectedValue);
            if (cn.State == ConnectionState.Closed)
                cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            lblmsg.Text = "Data entered successfully!!!";
        }
    

    【讨论】:

    • 感谢您的帮助,但我正在尝试使用单选按钮而不是 regu;ar 按钮将数据插入数据库
    【解决方案2】:

    如果有人需要,我找到了我自己问题的答案

    try
    {
        var connection = getConnection();
        connection.Open();
    
        if (CashRadio.Checked == true)
        {      
            var command = new SqlCommand
            {
                Connection = connection,
                CommandText ="INSERT INTO type_payment(cash,card) values(1,0)"
            };          
            command.Parameters.Clear();
    
            int result = command.ExecuteNonQuery();
            if (result > 0)
            {
                MessageBox.Show("Succsefully picked type");
            }
            else
            {
                MessageBox.Show("error");
            }
        }
        else if (CardRadio.Checked == true)
        {
            var command = new SqlCommand
            {
                Connection = connection,
                CommandText = "INSERT INTO type_payment(cash,card) values(0,1)"
            };
            command.Parameters.Clear();
    
            int result = command.ExecuteNonQuery();
            if (result > 0)
            {
                MessageBox.Show("Succesfully picked type");
            }
            else
            {
                MessageBox.Show("Error");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    

    【讨论】:

      【解决方案3】:

      卢卡,

      在下面试试。 添加 HTML 标记,如:

      <asp:CheckBox ID="chkCash" runat="server" />
      

      .cs 文件添加以下代码。

      string Cash = chkCash.Checked ? "Y" : "N";
      

      然后发送或添加类似的值。

      SqlCommand cmd = new SqlCommand("INSERT INTO Employees(Cash) VALUES(@Cash)"))
          {           
              cmd.Parameters.AddWithValue("@Cash", Cash);
          }
      

      【讨论】:

      • 感谢您的评论,但我正在使用 C#
      猜你喜欢
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      相关资源
      最近更新 更多