Hi my name is Abdul munawar

I am using two optional radio buttons in asp.net front end,... but ui have store single option in databse.. wat is the coding to select single Like male or feemale 2) buy or sell

So i need to write the code for selecting value and to store in databse

Pls kindly help me in this process

Dani AI

Generated

Quick practical guidance for : decide a compact column type for the choice (for example a single-character code or a small integer) and send that value from the server-side code rather than concatenating SQL. and touched on schema and insert logic; the important additions for a real app are: ensure the radio inputs are mutually exclusive on the page, validate server-side that a choice was made, and use parameterized commands so user input never gets concatenated into SQL.

Below is a concise WebForms-style example that reads two RadioButton controls, validates the selection, and inserts using MySQL Connector/NET with parameters and using blocks. Replace connection string and control names to match your page.

using MySql.Data.MySqlClient;

protected void btnSave_Click(object sender, EventArgs e)
{
    string name = txtName.Text.Trim();
    string details = txtDetails.Text.Trim();

    // radio controls: rbOption1, rbOption2
    string choice = rbOption1.Checked ? "A" : rbOption2.Checked ? "B" : null;
    if (string.IsNullOrEmpty(choice)) return; // handle no-selection

    string cs = "Server=localhost;Database=yourdb;Uid=you;Pwd=pass;";
    using (var conn = new MySqlConnection(cs))
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = "INSERT INTO mytable (name, details, choice) VALUES (@name, @details, @choice)";
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@details", details);
        cmd.Parameters.AddWithValue("@choice", choice);

        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

Notes and cautions: do server-side validation and exception handling around the DB call; prefer explicit parameter types for production code instead of AddWithValue; and pick a fixed storage format (CHAR(1), TINYINT or ENUM) so queries and indexes remain predictable. For Connector/NET parameter usage and examples see the MySQL Connector/NET programming guide (https://dev.mysql.com/doc/connector-net/en/connector-net-programming.html).

Recommended Answers

All 4 Replies

based on the column type for these radiobuttons u can send data to database.
For example if u have column of type bit ,u have to send 0/1.
if u have column type string ,u can send male/female

Actualy, here i am using 2 textbox and 2 radio buttons...
if iam writting code like insert into table values ("Textbox1text+"'. ........+"' whwt is the code to insert radio button value in ado.net using ASp.net c#.
Pls help me da

Actualy, here i am using 2 textbox and 2 radio buttons...
if iam writting code like insert into table values ("Textbox1text+"'. ........+"' whwt is the code to insert radio button value in ado.net using ASp.net c#.
Pls help me da

I think you should use RadioButtonList instead of RadioButton.

String SQL;
Byte gender;
if(RadioButtonList1.SelectedIndex==0)
{
     gender=0;
}
else
{
     gender=1;
}
SQL = "INSERT INTO TABLES VALUES ('" + TextBox1.Text + "', " + gender + ")";

Thanks,

Kusno.

You can also use this: RadioButton.GroupName Property

Use the GroupName property to specify a grouping of radio buttons to create a mutually exclusive set of controls. You can use the GroupName property when only one selection is possible from a list of available options.

When this property is set, only one RadioButton in the specified group can be selected at a time.

This topic is in MSDN with code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.