How to save user input from text box and radio button to the SQL database using C# in ASP.Net? Please help me...

Recommended Answers

All 3 Replies

>How to save user input from text box and radio button to the SQL database using C# in ASP.Net?

1. Store input from textbox and radio button into the variables.

String name=TextBox1.Text;
 String gender="Female";

2. Create connection and command object.

SqlConnection cn=new SqlConnection("put_connection_string");
  SqlCommand cmd=new SqlCommand("INSERT INTO YourTable (Name,Gender) VALUES (@Name,@Gender)",cn);

 //Open connection

i have problem with my radio button. i don't know how to declare my radio button and how to save the user selection from the radio button into the SQL database using C#. Let say that my radio button is gender. It should have two values that is male and female. I have try the following code (the code that i get from this forum before...), but there is an error.

1.      String gender="Female";
2.	 if(RadioButton1.Checked)
3.	          gender="Male";

the error that appear is -->
'System.Web.UI.WebControls.RadioButtonList' does not contain a definition for 'Checked' and no extension method 'Checked' accepting a first argument of type 'System.Web.UI.WebControls.RadioButtonList' could be found (are you missing a using directive or an assembly reference?)

I don't know what to do... please help me... thanks...

The error message indicates that you are using a RadioButtonList control instead of a standard RadioButton. To access the item that is selected in the control, you can utilize the .SelectedItem property (be careful, it can be null if the user has not made a selection) or to simply get the value selected, you can check the .SelectedValue property. In essence, it's just like using a DropDownList. Other lists that work in similar ways but allow for multiple selections are the CheckBoxList and the ListBox.

Below is an example of the RadioButtonList control.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="radiobuttonlistdemo.aspx.cs" Inherits="radiobuttonlistdemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:RadioButtonList ID="rdoDemo" runat="server">
            <asp:ListItem Text="Option A" Value="A" />
            <asp:ListItem Text="Option B" Value="B" />
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
        <br />
        <asp:Literal ID="litDemoResult" runat="server" />

In the page layout, I have defined a RadioButtonList with two options included. In the code-behind below, I'm going to add some more options programmatically. In the button click event handler, I'll check the selected item and display its value back to the user.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class radiobuttonlistdemo : System.Web.UI.Page
    protected void Page_Init(object sender, EventArgs e)
        string[] options = new string[] { "C", "D", "E" };

        foreach (string option in options)
            rdoDemo.Items.Add(new ListItem("Option " + option, option));

    protected void btnSubmit_Click(object sender, EventArgs e)
        if (rdoDemo.SelectedItem != null)
            string selectedValue = rdoDemo.SelectedValue;
            litDemoResult.Text = "You selected option " + selectedValue;
            litDemoResult.Text = "Please make a selection.";
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.