How to Data retrieve from Database in Dropdown Box

source :


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList id="list1" runat="server">

        </asp:DropDownList>

        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="search" />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

    </div>
    </form>
</body>
</html>



  • code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace dropdown_with_database
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string str = ConfigurationManager.ConnectionStrings["dbms"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dropdownlistbind();
            }

        }
        void dropdownlistbind()
        {
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand com= new SqlCommand("select * from data",con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable data = new DataTable();
            da.Fill(data);
            list1.DataSource = data;
            list1.DataTextField = "city";
            list1.DataBind(); 

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand com = new SqlCommand("select * from data where city=@city", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            da.SelectCommand.Parameters.AddWithValue("@city", list1.SelectedItem.Text);
            DataTable data = new DataTable();
            da.Fill(data);
            GridView1.DataSource = data;
           
            GridView1.DataBind(); 
        }
    }
}

  • web.config 
              <configuration>
    <system.web>
      
        <compilation debug="true" targetFramework="4.0" />
      
    </system.web>
  <connectionStrings>
    <add name="dbms" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\alex\documents\visual studio 2010\Projects\dropdown_with_database\dropdown_with_database\App_Data\Database1.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>

</configuration>


Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

Calendar in Asp.net

CheckBox in Asp.Net MVC (Insert Data of CheckBox in Database and GetData Of checkBox From DataBase)