Example : How to hang/add Sql Server database data into combo box or drop down box in Asp .NET using C#.

Code write in 'Trial.aspx.cs' (C# file)


protected void Page_Load(object sender, EventArgs e)
        {
            string cs = "data source =.; database= SITEDB; integrated security=SSPI";
            //string cs = @"Data Source =.; Initial Catalog=SITEDB;";
            SqlConnection con = new SqlConnection(cs);
            Response.Write("Data Base Connected...");

            try
            {
                con.Open();
                Combobox1.Items.Clear();

                SqlCommand cmd = new SqlCommand("select distinct Slno from Traildb", con);
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {           

                    Combobox1.Items.Add(sdr.GetString(0).Trim());

                    //Response.Write(sdr[0]); To print all data
                }

            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
            }  
        }


/*  -------------------------------------------  Another Method  -----------------------------------------  */


protected void Page_Load(object sender, EventArgs e)
        {
            string cs = "data source =.; database= SITEDB; integrated security=SSPI";
            //string cs = @"Data Source =.; Initial Catalog=SITEDB;";
            SqlConnection con = new SqlConnection(cs);
            Response.Write("Data Base Connected...");

            try
            {
                con.Open();
                Combobox1.Items.Clear();

                string qurey = "select distinct Slno from Traildb";
                SqlCommand cmd = new SqlCommand(qurey, con);
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    string data1 = sdr.GetString(0).Trim();
                    Combobox1.Items.Add(data1);

                    //Response.Write(data1);
                }
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
            }  
        }





Code write in 'Trial.aspx' (Html Design Page File)




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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />        
    
</head>
<body>
       <section>
            <div class="container my-4">
                <h1>This is Test Page</h1>
            </div>
        </section>
        
        <section>
            <div class="container my-4">
                <div class="card">
                    <div class="card-header">
                        Registration Page
                    </div>
                    
                    <div class="card-body">
                        <form runat="server" id="form1" method="post" enctype="multipart/form-data">
                           <div class="form-row">
                               <div class="form-group col-md-6">
                                   <asp:DropDownList ID="SearchData" CssClass="form-control" runat="server">                                    
                                    <asp:ListItem Value="" Text="-Select-" />
                                    <asp:ListItem Value="1" Text="1" />
                                    <asp:ListItem Value="2" Text="2" />
                                    

                                </asp:DropDownList>
                               </div>
                                 
                               <div class="form-group  col-md-6">
                                   <asp:Button CssClass="btn btn-info form-control" ID="BtnSearch" Text="Search" runat="server" OnClick="BtnSearch_Click"/>
                               
                               </div>     
                               
                            </div>
                            <div class="form-group">
                                <asp:Label for="TxtSlno" Text="" runat="server" /> Slno
                               <asp:TextBox ID="TxtSlno"  CssClass=" form-control" onkeypress="return (event.keyCode>=48 && event.keyCode<=57 );" runat="server" OnTextChanged="TxtSlno_TextChanged" />
                                
                                <%-- <asp:TextBox ID="TxtSlno" CssClass=" form-control" onkeydown="return (!(event.keyCode>=65 && event.keyCode<=90 ) && event.keyCode!=32);" runat="server" /> --%>

                            </div>

                             <div class="form-group">
                                <asp:Label for="TxtName" Text="" runat="server" /> Name
                                <asp:TextBox ID="TxtName" CssClass="form-control " onkeypress="return ((event.keyCode>=97 && event.keyCode<=122 || event.keyCode>=65 && event.keyCode<=90) );" runat="server" />
                            </div>

                             <div class="form-group">
                                <asp:Label for="TxtPass" Text="" runat="server" /> Password
                               <asp:TextBox ID="TxtPass" TextMode="Password" CssClass="form-control" runat="server"></asp:TextBox>
                            </div>

                            <div class="form-group">
                                <asp:Label for="TxtEmail" Text="" runat="server" /> Email-Id
                               <asp:TextBox ID="TxtEmail" TextMode="Email" CssClass="form-control" runat="server"></asp:TextBox>
                            </div>
                            <div class="form-group">
                                <asp:Label for="DtDob"  runat="server" />Date of Birth
                                <asp:TextBox ID="DtDob" runat="server" TextMode="Date" CssClass="form-control"/>
                            </div>
                            <div class="form-group">
                                <asp:Label for="RdbGender" runat="server" /> Gender
                                <div class=" form-check">
                                    <asp:RadioButton ID="RdbGender1" GroupName="rd1"  Value="Male" runat="server" /> Male
                                    <asp:RadioButton ID="RdbGender2" GroupName="rd1" Value="Female" runat="server" /> Female
                                </div>
                            </div>


                            <div class="form-check-label">
                                <asp:Label runat="server"/>Qualification
                            </div>

                            <div class="form-check form-check-inline">                                
                                <asp:CheckBox ID="ChkMatric" CssClass=" form-check-input" runat="server" />
                                <asp:Label for="ChkMatric" CssClass="form-check-label" runat="server" /> Matric
                            </div>

                            <div class="form-check form-check-inline">                                
                                <asp:CheckBox ID="ChkInter" CssClass=" form-check-input" runat="server" />
                                <asp:Label for="ChkInter" CssClass="form-check-label" runat="server" /> Intermediate
                            </div>

                            <div class="form-check form-check-inline">                                
                                <asp:CheckBox ID="ChkGrad" CssClass=" form-check-input" runat="server" />
                                <asp:Label for="ChkGrad" CssClass="form-check-label" runat="server" />Graduation
                            </div>
                            <div class="form-check form-check-inline">                                
                                <%--<asp:CheckBox ID="CheckBox1" Text="&nbsp;Post Graduation" CssClass="form-check-input" runat="server" />--%>
                                <asp:CheckBox ID="ChkPgrad" CssClass="form-check-input" runat="server" />
                                <asp:Label for="ChkPgrad" CssClass="form-check-label" runat="server" />Post Graduation
                            </div>
                            

                            <div class="form-group">
                                <asp:Label for="TxrAdd" runat="server" />Address
                                <%--<textarea id="TextArea1" class="form-control" cols="20" rows="2"></textarea>--%>

                                <asp:TextBox runat="server" ID="TxrAdd" mlkTextMode="MultiLine" Height="100" CssClass="form-control" />
                            </div>
                            
                            <div class="form-group">
                                <asp:Label for="CmbCity" runat="server" />City
                                <asp:DropDownList ID="CmbCity" CssClass="form-control" runat="server">                                    
                                    <asp:ListItem Value="" Text="-Select-" />
                                    <asp:ListItem Value="Patna" Text="Patna" />
                                    <asp:ListItem Value="Delhi" Text="Delhi" />
                                    <asp:ListItem Value="Mumbai" Text="Mumbai" />
                                    <asp:ListItem Value="Lakhnau" Text="Lakhnau" />
                                    <asp:ListItem Value="Jharkhand" Text="Jharkhand" />
                                    <asp:ListItem Value="Panji" Text="Panji" />
                                    <asp:ListItem Value="Itanagar" Text="Itanagar" />

                                </asp:DropDownList>
                            </div>
                            <div class="form-group">
                                <asp:Label for="NmbPin" runat="server" />Pin Code
                                <asp:TextBox ID="NmbPin" CssClass="form-control" TextMode="Number" MaxLength="10" runat="server" />
                            </div>
                            <div class="form-group">
                                <asp:Label for="FileUpload" runat="server" />Upload Picture
                                <asp:FileUpload ID="FileUpload1" CssClass="custom-file" runat="server" />
                            </div>

                            <div class="form-group">
                                <asp:Button CssClass="btn btn-success" ID="Submit" Text="Submit" runat="server" OnClick="Submit_Click"  />
                                <asp:Button CssClass="btn btn-info" ID="Reset" Text="Reset"  runat="server" OnClick="Reset_Click"  />
                                <asp:Button CssClass="btn btn-danger" ID="Delete" Text="Delete"  runat="server" OnClick="Delete_Click" />
                                <asp:Button CssClass="btn btn-secondary" ID="Update" Text="Update"  runat="server" OnClick="Update_Click" />
                            </div>
                            <div>
                         <asp:GridView ID="GridView1" runat="server">       </asp:GridView>
                    </div>
                        </form>

                    </div>

                </div>
            </div>
        </section>
        


        <!-- Bootstrap JS, Popper.js, and jQuery -->
        <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
      
</body>
</html>

Loading

Categories: ASP .Net

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.