How to save dropdownlist selected value in database in asp.net mvc

Save Selected Dropdown and Text value in database in ASP.NET Web-Forms

: 3002
Last Updated : 20/01/2021
Posted By :- vikas_jk
Posted By :- vikas_jk

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


0

report this ad
Related Articles
2365
Using Generics in C# (With Example)
5987
Solving error "the breakpoint will not currently be hit" in Visual studio ( Multiple ways)
7441
Connect to Oracle Database in C# (ASP.NET Web-Forms Example)
100
Difference between AddTransient, AddScoped and AddSingleton in ASP.NET Core
3689
Using Chart in ASP.NET (Pie chart in ASP.NET)

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly


Follow us
Related Questions
12807
400. That’s an error. Error: redirect_uri_mismatch - Google OAuth Authentication
6866
how to generate dynamic url using .NET MVC
4594
How to convert JSON String into C# class object
10941
Cannot convert null to a value type JSON error
6123
DbArithmeticExpression arguments must have a numeric common type
report this ad
report this ad


Add your solution here

B I U S small BIG code var < > & link [^] encode untab case indent outdent

Preview 0

Existing Members

...or Join us

Download, Vote, Comment, Publish.

Your Email
This email is in use. Do you need your password?
Optional Password
When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. 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.
  4. 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.
Let's work to help developers, not make them feel stupid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900

Video liên quan

Postingan terbaru

LIHAT SEMUA