Home > OS >  Insert data from radio button into database
Insert data from radio button into database

Time:12-20

Building a form application in C# for selling phones as a project. I have two radio buttons which the user checks based on what type of payment method they want cash or card.

How do I insert that data into the database based on what the user selects?

enter image description here

CodePudding user response:

You can try this,

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!!!";
    }

CodePudding user response:

Luka,

Try it below. Add HTML Markup like:

<asp:CheckBox ID="chkCash" runat="server" />
.cs file add below code.

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

And send or add value like.

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

  • Related