Listing A—DataSet binding
//NOTE: this snippet is assumed to be in a codebehind event handler
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=local;Initial Catalog=NWIND;Integrated Security=SSPI;";
SqlCommand cmd = new SqlCommand("SELECT * FROM Categories");
cn.Open();
cmd.Connection = cn;
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(ds);
                    
this.ListBox1.DataSource = ds;
this.ListBox1.DataTextField = "CategoryName";
this.ListBox1.DataValueField = "CategoryId";
this.ListBox1.DataBind();