Retrieve data from db to data bound controls

Posted by Arjunan_Csharp under ASP.NET on 11/22/2012 | Points: 10 | Views : 1194 | Status : [Member] | Replies : 3
Hi Friends,This is my first post from this site.I am newbie in the dotnet world.I have so many doubts.

I want to retrieve data from the database and add those items to data bound controls.
asp:RadioButtonList
asp:CheckBoxList
asp:DropDownList - [http://www.dotnetfunda.com/codes/code77-retriving-data-from-database-and-add-items-to-dropdownlist.aspx]
asp:Listbox

I got an idea for the dropdownlist from the above link.I want the same code for Radiobuttonlist,CheckboxList and Listbox.I want to bind data using datareader and dataset.Please give me a simple example.




Responses

Posted by: Sandhyab on: 11/23/2012 [Member] Starter | Points: 25

Up
0
Down
HI,
In aspx page take controls to bind as shown below
<asp:DropDownList ID="DropDownList1" runat="server" DataValueField="Id" DataTextField="Language">

</asp:DropDownList>
<asp:ListBox ID="ListBox1" runat="server" DataValueField="Id" DataTextField="Language"></asp:ListBox>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataValueField="Id" DataTextField="Language" >
</asp:CheckBoxList>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" DataTextField="Language" DataValueField="Id">
</asp:RadioButtonList>

And in code behibnd I am calling Different methods to retreive data from database and to bound to controls. I used dataset.
using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindPersonDetails();
}
BindData();
PopulateData();
GetData();
}

private void GetData()
{
DataSet dset = new DataSet();
//DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "SELECT Id, Language FROM Data";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(dset);
}
}
}
RadioButtonList1.DataSource = dset.Tables[0];
RadioButtonList1.DataBind();

}

private void PopulateData()
{
DataSet dset = new DataSet();

using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "SELECT Id, Language FROM Data";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(dset);
}
}
}
CheckBoxList1.DataSource = dset.Tables[0];
CheckBoxList1.DataBind();


}

private void BindData()
{
DataSet dset = new DataSet();

using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "SELECT Id, Language FROM Data";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(dset);
}
}
}
ListBox1.DataSource = dset.Tables[0];
ListBox1.DataBind();

}

private void BindPersonDetails()
{
DataSet dset = new DataSet();

using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "SELECT Id, Language FROM Data";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(dset);
}
}
}
DropDownList1.DataSource = dset.Tables[0];
DropDownList1.DataBind();
}
}

GetData method for RadiobuttonList, PopulateData for Checkboxlist,BindData for ListBox and BindPersonDetails for dropdown list

Thanks & Regards

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 11/23/2012 [Member] Starter | Points: 25

Up
0
Down
Using Datreader:
private void  BindPersonDetails()

{
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "SELECT Id, Language FROM Data";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
// If you want to control the database connection behavior, simply call the ExecuteReader method without any parameter
// using (SqlDataReader reader = cmd.ExecuteReader()
// if you want to close the connection as soon as reader is closed, pass the CommandBehavior, the way I have passed below
using (SqlDataReader reader =
cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
ListBox1.DataSource = reader;
ListBox1.DataBind();
}
}
}

}


And repalace ListBox1 with Your RadiobuttonList,Checkboxlist and dropdown Ids

Thanks & Regards

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 11/23/2012 [Member] Starter | Points: 25

Up
0
Down
Please Mark as answer if this helped you. This might help others who will search for the same.

Thanks & Regards

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response