Database Structure
Query:
- CREATE TABLE tbl_ins
- (
- UserID [int] IDENTITY(1,1) ,
- Username varchar(30),
- Password varchar(30),
- Address varchar(100),
- Mobile varchar(12),
- Email varchar(50)
- )
DB Design:
So here am explain how to populate database value in dropdownlist.
Database Information
Default.aspx:
Code:
- <asp:Label ID=”lbluser” runat=”server” Text=”User Name”></asp:Label>
- <asp:DropDownList ID=”drpusername” runat=”server”></asp:DropDownList>
Screen Design:
WEBCONFIG:
Default.aspx.cs:
(Here bind the username column from database to dropdownlist).
Code:
- protected void Page_Load(object sender, EventArgs e)
- {
- if(!IsPostBack)
- {
- SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[“userconnstring”].ConnectionString);
- con.Open();
- SqlCommand cmd = new SqlCommand(“Select UserName from tbl_ins”, con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- da.Fill(dt);
- drpusername.DataSource = dt;
- drpusername.DataTextField = “UserName”;
- drpusername.DataValueField = “UserName”;
- drpusername.DataBind();
- }
- }
Output: