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.

Thursday, March 20, 2008

Inserting Data into a Database using OleDb

This snippet is shows you a simple way to insert data into a database using OleDb.

protected void Button1_Click(object sender, EventArgs e)
{
//Setup Connection string
String cnstr= "Provider=type in OLE DB Provider;server=type in IP address;
+ "Uid=type in database user;Pwd=type in database password;"
+ "database=type in database
name;"

//Create a new OleDbConnection
OleDbConnection cn = new OleDbConnection(cnstr);

//Create a SQL insert statement
//Don't forget to concatenate when using TextBox1.Text
String insert = "INSERT INTO table name (column name) VALUES ('"+ TextBox1.Text + "')";

//Create a new OleDbCommand
OleDbCommand insertcmd = new OleDbCommand (insert, cn);

//Open the connection
cn.Open();

//Execute the Query
insertcmd.ExecuteNonQuery();

//Close the connection
cn.Close();
}

*Quick Note: Don't forget to add 'using System.Data.OleDb;' 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.

Tuesday, March 18, 2008

Dynamically Populating a Gridview with a Search Button and aTextbox

This snippet shows you a simple way to dynamically populate a GridView with a search button and a textbox. Your SQL Select statement will determine which columns you want displayed in your GridView.

//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)
{
Label1.Text =
"0";
}

protected void Button1_Click(object sender, EventArgs e)
{

// 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
//Don't forget to concatenate when using TextBox1.Text
SqlDataAdapter da= new SqlDataAdapter("SELECT columname FROM tablename WHERE VendorID LIKE '" + TextBox1.Text + "', cn);
// Fill the Data Adapter
da.Fill(ds);

//Set the Datasource for the Repeater equal to the DataSet
GridView1.DataSource = ds;
//Bind your Data
GridView1.DataBind();

Label1.Text = GridView1.Rows.Count.ToString();
}

*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, March 17, 2008

Dynamically Populating a Repeater with SQL Data Adapter


This snippet shows you a simple way to dynamically populate a Repeater. Your SQL Select statement will determine which columns you want displayed in your Repeater.

//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)
{
// 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
Repeater1.DataSource = ds;
//Bind your Data
Repeater1.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. Depending on how many feilds/columns you want to display, will determine how many controls you use. In this example I use a Label to display the data, you don't have to use a Label, you can use another control, such as a TextBox, if you choose to. 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.

Dynamically Populating a FormView with SQL Data Adapter

This snippet shows you a simple way to dynamically populate a FormView. Your SQL Select statement will determine which columns you want displayed in your FormView.

//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)
{
// 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( "Your Select Statement" , cn);
// Fill the Data Adapter
da.Fill(ds);

//Set the Datasource for the FormView equal to the DataSet
FormView1.DataSource = ds;
//Bind your Data
FormView1.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. Depending on how many feilds/columns you want to display, will determine how many controls you use. In this example I use a Label to display the data, you don't have to use a Label, you can use another control, such as a TextBox, if you choose to. 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.

Dynamically Populating a DetailsView with SQL Data Adapter

This snippet shows you a simple way to dynamically populate a DetailsView. Your SQL Select statement will determine which columns you want displayed in your DetailsView.

//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)
{
// 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( "Your Select Statement" , cn);
// Fill the Data Adapter
da.Fill(ds);

//Set the Datasource for the DetailsView equal to the DataSet
DetailsView1.DataSource = ds;
//Bind your Data
DetailsView1.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. In addition, you don't have to use a Label, you can use a Textbox if you choose to. 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.

Dynamically Populating a DataList with SQL Data Adapter

This snippet shows you a simple way to dynamically populate a DataList. Your SQL Select statement will determine which columns you want displayed in your DataList.

//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)
{
// 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( "Your Select Statement" , cn);
// Fill the Data Adapter
da.Fill(ds);

//Set the Datasource for the DataList equal to the DataSet
DataList1.DataSource = ds;
//Bind your Data
DataList.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. Depending on how many feilds/columns you want to display, will determine how many controls you use. In this example I use a Label to display the data, you don't have to use a Label, you can use another control, such as a TextBox, if you choose to. 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.

Dynamically Populating a Gridview with SQL Data Adapter

This snippet shows you a simple way to dynamically populate a GridView. Your SQL Select statement will determine which columns you want displayed in your GridView.

//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)
{
// 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( "Your Select Statement" , cn);
// Fill the Data Adapter
da.Fill(ds);

//Set the Datasource for the GridView equal to the DataSet
GridView1.DataSource = ds;
//Bind your Data
GridView1.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.