In this article I will explain with an example, how to insert value from RadioButtonList to
SQL Server database in ASP.Net using C# and VB.Net.
Database
I have made use of the following table Hobbies with the schema as follows.
I have already inserted few records in the table
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The following HTML Markup consists of:
RadioButtonList – For displaying data.
Button – For updating records into the
SQL Server database.
The Button has been assigned with an OnClick event handler.
Hobbies:
<asp:RadioButtonList ID="rblHobbies" runat="server"></asp:RadioButtonList>
<br />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="UpdateHobbies" />
Namespaces
You will need to import the following namespaces.
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
using System.Configuration;
using System.Data.SqlClient;
Populating the RadioButtonList from Database
Inside the Page Load event handler, the PopulateHobbies method is called.
PopulateHobbies
Inside the
PopulateHobbies method, the records are fetched from the
SQL Server database using
ExecuteReader function.
The ListItem class object is created and Text, Value and Selected properties are set.
The Checked and Unchecked state of RadioButtons will be decided based on
IsSelected column value fetched from the
SQL Server database.
If fetched value of IsSelected column is 1 then the RadioButton will be checked and if it is 0 then the RadioButton will be set as unchecked.
Finally, the ListItem class object is added to the RadioButtonList Items.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateHobbies();
}
}
private void PopulateHobbies()
{
string query = "SELECT HobbyId, Hobby, IsSelected FROM Hobbies";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ListItem item = new ListItem();
item.Text = sdr["Hobby"].ToString();
item.Value = sdr["HobbyId"].ToString();
item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
rblHobbies.Items.Add(item);
}
}
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.PopulateHobbies()
End If
End Sub
Private Sub PopulateHobbies()
Dim query As String = "SELECT HobbyId, Hobby, IsSelected FROM Hobbies"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query, con)
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Dim item As New ListItem()
item.Text = sdr("Hobby").ToString()
item.Value = sdr("HobbyId").ToString()
item.Selected = Convert.ToBoolean(sdr("IsSelected"))
rblHobbies.Items.Add(item)
End While
End Using
con.Close()
End Using
End Using
End Sub
Updating record based on the Selection into the Database
When the UpdateHobbies button is clicked, the value of the selected RadioButton from the RadioButtonList is passed as parameter for HobbyId to SqlCommand class object.
Finally, the record is updated into the
SQL Server database using
ExecuteNonQuery function.
Here, two queries will be executed:
1. Update query to update all IsSelected column values to 0 as only one RadioButton can be selected at a time.
2. Update query to update the IsSelected column value of the selected Hobby to 1 based on the passed HobbyId parameter.
C#
protected void UpdateHobbies(object sender, EventArgs e)
{
string query = "UPDATE Hobbies SET IsSelected = 0;UPDATE Hobbies SET IsSelected = 1 WHERE HobbyId = @HobbyId";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@HobbyId", rblHobbies.SelectedItem.Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub UpdateHobbies(sender As Object, e As EventArgs)
Dim query As String = "UPDATE Hobbies SET IsSelected = 0;UPDATE Hobbies SET IsSelected = 1 WHERE HobbyId = @HobbyId"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query, con)
con.Open()
cmd.Parameters.AddWithValue("@HobbyId", rblHobbies.SelectedItem.Value)
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Screenshots
The Form
Records after Updating
Demo
Downloads