In previous article, I have explained about creating dropdown list in ASP.NET MVC, but in this article, we will see how to create dropdown from database values and then save selected dropdown value and text value in SQL Server database in ASP.NET Web-Forms.

Step 1: Create a new project in your Visual Studio by navigating to "File"-> "New" -> "Project" -> Select "Visual C#" from left-pane and "ASP.NET Web-Application" from right-pane, name your project and click "Ok"


In Second Window, check "Web-Forms" and select "Empty", so Visual Studio can generate basic Web-Forms files.

Step 2: Create new "Default.aspx" page by right-clicking on Solution Explorer, Select "Add" -> Select "New Item" -> Select "Web-Forms" -> Name it as Default.aspx and click "Ok"

Suppose, we have these name values to fill in dropdown from database


and we have an Empty table (Student_details) where we will save new values, as below, from dropdown and text.


Use the below code to create Dropdown and Textbox in Default.aspx Page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SaaveDropdownTextValue.Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <asp:DropDownList ID="DropDownList1" runat="server" Width="100px">  
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
             <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Save Values in Database" OnClick="SaveValuesInDatabase" />


and in the code-behind (Default.aspx.cs), use the below C# code to load dropdown from database and insert or save values in database from dropdown and text-boxes in "Student_details" table.

using System;
using System.Data;
using System.Data.SqlClient;

namespace SaaveDropdownTextValue
    public partial class Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
                //using Students Database, with Windows Security as Login to SQL Server DB
                string connectionString = @"Data Source=VKS-PC-NEW\SQLEXPRESS;Initial Catalog=Students;Integrated Security=True;";
                // connection string  
                SqlConnection con = new SqlConnection(connectionString);

                SqlCommand com = new SqlCommand("select * from Names", con);
                // table name   
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                da.Fill(ds);  // fill dataset  
               //assigning datasource to the dropdownlist           
                DropDownList1.DataSource = ds.Tables[0];

                DropDownList1.DataTextField = ds.Tables[0].Columns["Name"].ToString(); // text field name of table dispalyed in dropdown, "Name" column from Students table       
                DropDownList1.DataValueField = ds.Tables[0].Columns["Id"].ToString();

                DropDownList1.DataBind();  //binding dropdownlist  

        protected void SaveValuesInDatabase(object sender, EventArgs e)
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=VKS-PC-NEW\SQLEXPRESS;Initial Catalog=Students;Integrated Security=True;";

            string query1 = "insert into Student_details(Name,Email,Class) values ('"+ DropDownList1.SelectedItem.Text + "','" + TextBox1.Text + "','" + TextBox2.Text + "')";

            SqlCommand cmd1 = new SqlCommand(query1, con);


Now, build and run it in your browser, you will see output like below


Once you will select dropdown value and enter text values in it, it will save data in database.

Here is the complete GIF sample, which shows database table before and after exeucting above code


You may also like to read:

File Upload in Web-Forms

Image control in ASP.NET (Upload Image & Display it in Image Control)

Understanding ASP.NET Gridview control with an example