Save Selected Dropdown and Text value in database in ASP.NET Web-Forms
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="//www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> Name: <asp:DropDownList ID="DropDownList1" runat="server" Width="100px"> </asp:DropDownList> <br/> Email: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br/> Class: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br/> <asp:Button ID="Button1" runat="server" Text="Save Values in Database" OnClick="SaveValuesInDatabase" /> </div> </form> </body> </html>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) { if(!IsPostBack) { //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); con.Open(); 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); con.Open(); cmd1.ExecuteNonQuery(); con.Close(); } } }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
Subscribe Now
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly
Add your solution here
B I U S small BIG code var < > & link [^] encode untab case indent outdent
Preview 0
Existing Members | ...or Join usDownload, Vote, Comment, Publish. | ||
|
- Read the question carefully.
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
- If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
- Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.