How to Upload Data From Database to Dropdown List

  • Updated date Mar 25, 2019
  • i.4m
  • 12

Today, I am providing an commodity showing you lot how to bind a DropDownList with a database and brandish the jump data in a GridView in ASP.NET.

Introduction

Today, I take provided an commodity showing you how to demark a DropDownList with a database and display the bound data in a GridView in ASP.NET. In this article, nosotros create a table in a SQL Server database and bind that table with a DropDownList control. After that we select an item in the DropDownList and match with the database table for the records that match with the database. It will display in the GridView control. To do that nosotros create a connection string object to connect the database with the application and read data from the database using the select command to display data in the DropDownList. All yous accept to do is implement and claw it up to your requirement or need. First, start Visual Studio .Cyberspace and brand a new ASP.Cyberspace web site using Visual Studio 2010.

Creating Tabular array in SQL Server Database

Now create a table named UserDetail with the columns id, name, state and city. Set the identity property=true for id. The table looks as in the following:

img3.jpg

Now insert some values in this table. The table looks like this:

img4.jpg

At present you have to create a web site.

  • Go to Visual Studio 2010
  • New-> Select a website application
  • Click OK

img5.jpg

Now add together a new page to the website.

  • Go to the Solution Explorer
  • Right-click on the Project name
  • Select add together new detail
  • Add new web folio and give it a name
  • Click OK

img6.jpg

Pattern the page and place the required control in it. Now drag and drib one DropDownList control, Button and GridView control on the form. Let's take a look at a practical example.

.aspx Lawmaking

  1. <%@ Folio Language= "C#"  AutoEventWireup= "true"  CodeFile= "Default2.aspx.cs"  Inherits= "Default2"  %>
  2. <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML one.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server" >
  5.     <title></title>
  6. </head>
  7. <trunk>
  8.     <form id="form1"  runat= "server" >
  9.     <div>
  10.         <asp:DropDownList ID="DropDownList1"  runat= "server" >
  11.         </asp:DropDownList>
  12.         <asp:Label ID="Label1"  runat= "server"  Text= "Label" ></asp:Characterization>
  13.         <br />
  14.         <br />
  15.         <asp:Button ID="Button1"  runat= "server"  Text= "Push button"  OnClick= "Button1_Click1"  Manner= "height: 26px"  />
  16.         <br />
  17.         <br />
  18.         <asp:GridView ID="GridView1"  runat= "server"  BackColor= "White"  BorderColor= "#999999"
  19.             BorderStyle="None"  BorderWidth= "1px"  CellPadding= "3"  GridLines= "Vertical" >
  20.             <AlternatingRowStyle BackColor="#DCDCDC"  />
  21.             <FooterStyle BackColor="#CCCCCC"  ForeColor= "Black"  />
  22.             <HeaderStyle BackColor="#000084"  Font-Assuming= "True"  ForeColor= "White"  />
  23.             <PagerStyle BackColor="#999999"  ForeColor= "Blackness"  HorizontalAlign= "Center"  />
  24.             <RowStyle BackColor="#EEEEEE"  ForeColor= "Black"  />
  25.             <SelectedRowStyle BackColor="#008A8C"  Font-Bold= "True"  ForeColor= "White"  />
  26.             <SortedAscendingCellStyle BackColor="#F1F1F1"  />
  27.             <SortedAscendingHeaderStyle BackColor="#0000A9"  />
  28.             <SortedDescendingCellStyle BackColor="#CAC9C9"  />
  29.             <SortedDescendingHeaderStyle BackColor="#000065"  />
  30.         </asp:GridView>
  31.     </div>
  32.     </class>
  33. </trunk>
  34. </html>

At present add together the following namespaces:

  1. using Organization.Information.SqlClient;
  2. using Organisation.Data;

At present write the connection cord to connect to the database:

  1. string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=rohatash;";

At present demark the DropDownList with the database table.

  1. SqlConnection con = new  SqlConnection(str);
  2. string com ="Select * from UserDetail" ;
  3. SqlDataAdapter adpt =new  SqlDataAdapter(com, con);
  4. DataTable dt =new  DataTable();
  5. adpt.Fill(dt);
  6. DropDownList1.DataSource = dt;
  7. DropDownList1.DataBind();
  8. DropDownList1.DataTextField ="Proper name" ;
  9. DropDownList1.DataValueField ="ID" ;
  10. DropDownList1.DataBind();

At present double-click on the Push control and add together the post-obit code:

  1. SqlConnection con = new  SqlConnection(str);
  2. SqlCommand cmd =new  SqlCommand( "select * from UserDetail where id = '"  + DropDownList1.SelectedValue + "'" , con);
  3. SqlDataAdapter Adpt =new  SqlDataAdapter(cmd);
  4. DataTable dt =new  DataTable();
  5. Adpt.Fill(dt);
  6. GridView1.DataSource = dt;
  7. GridView1.DataBind();
  8. Label1.Text ="tape found" ;

In the code-behind write the following code:

Lawmaking-backside

To display information in The GridViw use a DataAdapter object to retrieve the data from the database and place that data into a table.

  1. using  Organization;
  2. using  Organization.Collections.Generic;
  3. using  System.Linq;
  4. using  Organization.Web;
  5. using  Arrangement.Web.UI;
  6. using  System.Web.UI.WebControls;
  7. using  System.Data.SqlClient;
  8. using  Organization.Data;
  9. public  partial course  Default2 : Organisation.Web.UI.Page
  10. {
  11. string  str = "Information Source=.;uid=sa;pwd=wintellect;database=rohatash" ;
  12. protected void  Page_Load( object  sender, EventArgs e)
  13.     {
  14.         SqlConnection con =new  SqlConnection(str);
  15. cord  com = "Select * from UserDetail" ;
  16.         SqlDataAdapter adpt =new  SqlDataAdapter(com, con);
  17.         DataTable dt =new  DataTable();
  18.         adpt.Fill(dt);
  19.         DropDownList1.DataSource = dt;
  20.         DropDownList1.DataBind();
  21.         DropDownList1.DataTextField ="Name" ;
  22.         DropDownList1.DataValueField ="ID" ;
  23.         DropDownList1.DataBind();
  24.     }
  25. protected void  Button1_Click1( object  sender, EventArgs due east)
  26.     {
  27.         SqlConnection con =new  SqlConnection(str);
  28.         SqlCommand cmd =new  SqlCommand( "select * from UserDetail where id = '"  + DropDownList1.SelectedValue + "'" , con);
  29.         SqlDataAdapter Adpt =new  SqlDataAdapter(cmd);
  30.         DataTable dt =new  DataTable();
  31.         Adpt.Fill(dt);
  32.         GridView1.DataSource = dt;
  33.         GridView1.DataBind();
  34.         Label1.Text ="record found" ;
  35.     }
  36. }

Now run the application and select the name from DropDownList command and click on the Button.

img1.jpg

Now click on the Button.

img2.jpg

nationsthapplad.blogspot.com

Source: https://www.c-sharpcorner.com/UploadFile/rohatash/binding-dropdownlist-with-database-and-display-data-in-gridv/

0 Response to "How to Upload Data From Database to Dropdown List"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel