In this article I will explain with example, how to save (insert) DropDownList Selected value to 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 an ASP.Net DropDownList and a Button.
<asp:DropDownList ID="ddlHobbies" runat="server">
</asp:DropDownList>
<br />
<br />
<asp:Button Text="Save" runat="server" OnClick="Save" />
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Populating DropDownList with Selected Value from Database in ASP.Net
Inside the Page Load event of the page, a Select Query is executed over the Hobbies table and then using SqlDataReader, one by one each record is added to the DropDownList.
The selected item for the DropDownList is set using the value of the IsSelected column.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
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))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = 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"]);
ddlHobbies.Items.Add(item);
}
}
con.Close();
}
}
ddlHobbies.Items.Insert(0, new ListItem("--Select Hobby--", "0"));
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
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)
cmd.CommandType = CommandType.Text
cmd.Connection = 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"))
ddlHobbies.Items.Add(item)
End While
End Using
con.Close()
End Using
End Using
ddlHobbies.Items.Insert(0, New ListItem("--Select Hobby--", "0"))
End If
End Sub
Saving (Inserting) DropDownList Selected Value to Database in ASP.Net
When the Save Button is clicked, the IsSelected column is updated to 0 (False) for all records in the Hobbies table and then for the selected item of DropDownList, the IsSelected column is set to 1 (True).
C#
protected void Save(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))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@HobbyId", ddlHobbies.SelectedItem.Value);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub Save(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)
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
cmd.Parameters.AddWithValue("@HobbyId", ddlHobbies.SelectedItem.Value)
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Screenshot
Demo
Downloads