Google
 

Tuesday, April 8, 2008

Dynamically Popluating a ListBox with SQL Data Adapter

This snippet shows you a simple way to dynamically populate a ListBox. Your SQL Select statement will determine the data that the ListBox's will display as well as its value.

//declare two variables - one for the connection string and the other for the dataset
private String cnstr;
private DataSet ds;

protected void Page_Load(object sender, EventArgs e)
{
// Call the method you created
_source();
}

protected void _source()
{

// Setup Connection string
cnstr= "server=type in IP address;user=type in
database user;password=type in database password;"
+ "database=type in database
name;"
;

// Create a new Sql Connection

SqlConnection cn = new SqlConnection(cnstr);

//Create the DataSet

ds = new
DataSet("ds");

//Create a new Sql Data Adapter
//In your SQL Select statement, only select
the columns you want to be displayed
//If you use the select all (*) it will obviously display all the columns
SqlDataAdapter da= new SqlDataAdapter("SELECT columname FROM tablename", cn);
// Fill the Data Adapter
da.Fill(ds);

//Set the Datasource for the Repeater equal to the DataSet
ListBox1.DataSource = ds;
//Set the ListBox's DataTextField and DataValueField to the Column you selected
ListBox1.DataTextField = "columnname";
ListBox1.DataValueField = "columnname";
//Bind your Data
ListBox1.DataBind();
}

*Quick Note: Don't forget to add 'using System.Data.SqlClient;' to the top of the code behind page.

In your .aspx page add the following code in between the body tags. Click on the image to view full size.

If you have any questions or there are discrepancies in the code please feel free to leave a comment and I will try to answer/fix them as soon as I can.

Monday, April 7, 2008

Dynamically Popluating a DropDownList with SQL Data Adapter

This snippet shows you a simple way to dynamically populate a DropDownList. Your SQL Select statement will determine the data that the DropDownList's will display as well as its value.

//declare two variables - one for the connection string and the other for the dataset
private String cnstr;
private DataSet ds;

protected void Page_Load(object sender, EventArgs e)
{
// Call the method you created
_source();
}

protected void _source()
{

// Setup Connection string
cnstr= "server=type in IP address;user=type in
database user;password=type in database password;"
+ "database=type in database
name;"
;

// Create a new Sql Connection

SqlConnection cn = new SqlConnection(cnstr);

//Create the DataSet

ds = new
DataSet("ds");

//Create a new Sql Data Adapter
//In your SQL Select statement, only select the c
olumns you want to be displayed
//If you use the select all (*) it will obviously display all the columns
SqlDataAdapter da= new SqlDataAdapter("SELECT columname FROM tablename", cn);
// Fill the Data Adapter
da.Fill(ds);

//Set the Datasource for the Repeater equal to the DataSet
DropDownList1.DataSource = ds;
//Set the DropDownList's DataTextField and DataValueField to the Column you selected
DropDownList1.DataTextField = "columnname";
DropDownList1.DataValueField = "columnname";
//Bind your Data
DropDownList1.DataBind();
}

*Quick Note: Don't forget to add 'using System.Data.SqlClient;' to the top of the code behind page.

In your .aspx page add the following code in between the body tags. Click on the image to view full size.

If you have any questions or there are discrepancies in the code please feel free to leave a comment and I will try to answer/fix them as soon as I can.