Sep 02 2009

Asp.net CheckBoxList DataSource

step1: create table category in your database, the query of the table link below

tblCategory Table

step2: insert some values into tblcategory table as shown in below figure

tblCategory

tblCategory table data

step3: Drag CheckBoxList control into Default.aspx Page

<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server” RepeatColumns=”2″>
</asp:CheckBoxList>

Step4: write below code in Page_Load event

//Sample code

protected void Page_Load(object sender, EventArgs e)
{

//Data Source = Your server name or localĀ  sqlserver name

//Data Source=./SQLEXPRESS (for local Sqlserver)

//Data Source = santhu or 192.168.1.34 (for remote servers)

string ConnectionString = “Data Source=santhu;Initial Catalog=DataBaseName;User Id=sa;Password=abc123″;

SqlConnection objConn = new SqlConnection(ConnectionString);

string query = “select CategoryID,CategoryName from tblCategory”;
SqlCommand objCmd = new SqlCommand(query, objConn);
objCmd.CommandType = CommandType.Text;

DataSet objDs = new DataSet();
SqlDataAdapter objDa = new SqlDataAdapter(objCmd);

objConn.Open();
objDa.Fill(objDs);
objCmd.ExecuteNonQuery();

CheckBoxList1.DataTextField = “CategoryName”;
CheckBoxList1.DataValueField = “CategoryID”;

CheckBoxList1.DataSource = objDs;
CheckBoxList1.DataBind();

objConn.Close();
}

Happy coding…

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Leave a Reply

Alibi3col theme by Themocracy