RSS

Search Engine

Tuesday, May 18, 2010

ASP.Net RadioButtonList Control with SQL DataBinding

The ASP.Net RadioButtonList control also supports SQL DataBinding feature that enables you to display the data retrieved from the SQL Database in the form of radio button type list items. You can either use the SqlDataSource control to bind the SQL data with radiobuttonlist control or C# code to bind the data programmatically. Using SqlDataSource control you can bind data items with radiobuttonlist control without writing any single line of C# code. You can display the data items by specifying the ID of the SqlDataSource control as a value of DataSourceID property of the RadioButtonList control. While using C# code to bind the SQL data dynamically you have to use the DataSource property of radiobuttonlist control to bind the data object.

But specifying only DataSourceID or DataSource property of ASP.Net RadioButtonList control is not enough. You have to specify the values for 2 more properties to display the text content of radiobutton list items and set the value for each list item. Following are the two properties that enable you to specify the text and value property of list items of RadioButtonList control:

DataTextField: It accepts the name of the Field of the data table retrieved using datasource control or data access C# code that you want to display as the text content of the list item.

DataValueField: It accepts the name of the field of the data table retrieved using datasource control or data access C# code that you want to set as the value attribute of each list item. This value part does not appear as text content.

Sample Code for ASP.Net RadioButtonList SQL DataBinding

C# Code:

public void BindRadioButtonListData()
{
// connection string
string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

// Sql connection object initialized with connection string used to connect
// it with Northwind SQL database
using (SqlConnection mySqlConnection = new SqlConnection(connectionString))
{
try
{
// open the Sql connection
mySqlConnection.Open();

// Sql Command object initialized with SQL query to retrieve the categories
SqlCommand mySqlCommand = new SqlCommand("Select CategoryID, CategoryName from Categories", mySqlConnection);

// Sql Data Adapter object initialized by passing the Sql Command object
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySqlCommand);


// DataSet object to store the retrieved SQL data items
DataSet myDataSet = new DataSet();

// fill the DataSet
mySqlDataAdapter.Fill(myDataSet);

// Set DataSet object as DataSource for the RadioButtonList
RadioButtonList1.DataSource = myDataSet;

// Specify the Field Name that you want to display as
// text label for Radio Button list item
RadioButtonList1.DataTextField = "CategoryName";

// Specify the Field Name that you want to use as
// value for each list item
RadioButtonList1.DataValueField = "CategoryID";

// Finalize the DataBinding
RadioButtonList1.DataBind();

}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
finally
{
// close the Sql Connection
mySqlConnection.Close();
}
}
}

You can call the above function in the Page load event of the ASP.Net web page to bind the SQL data with RadioButtonList control.

1 comments:

Anonymous said...

All grids use binding to data collections of (IList, IBindingList, IListSource) type as the main and often the only method of working with data. Besides, connection to IBindingList, Dapfor NetGrid significantly expands data binding features enabling operations in unbound mode.more help and see the example visit dapfor. com

Post a Comment