How to Load Listbox using Asp.net C# Code

Jayakumars
Posted by Jayakumars under ASP.NET AJAX category on | Points: 40 | Views : 1884

SqlConnection con = new SqlConnection("data source =Test;initial catalog =Test; user id=sa;pwd =test;");
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
listboxbind();
}
}

private void listboxbind()
{
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
dt = ds.Tables["employee"];
ListBox1.DataTextField = "Department";
ListBox1.DataValueField = "EMPLOYEEID";
ListBox1.DataSource = ds.Tables["employee"];
ListBox1.DataBind();
con.Close();

}

Comments or Responses

Login to post response