Friday, August 3, 2012

How To Fill ListBox using DataBase

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">

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

Default.aspx.cs


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);
        for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++)
        {
         ListBox1.Items.Add(new ListItem(ds.Tables[0].Rows[i]["customername"].ToString(), ds.Tables[0].Rows[i]["customerid"].ToString()));
        }
    }
}



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...