Thursday, August 2, 2012

How to Fill the Drop Down Values From DataBase

Design the DataTable





Default.aspx

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
    
    </div>
    </form>
</body>
</html>


using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("user id=sa;password=uday;database=uday;data source=localhost");
        SqlCommand cmd = new SqlCommand("select *from customers", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "customername";
        DropDownList1.DataValueField = "customerid";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0,new ListItem("--SelectItem--","0"));
    }
}







No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...