0

I want to Bind a checklistbox with Database in sqlserver2008. I am working in asp.net C# on a user control Module. I wrote a code. i want to know whether the code is perfact or not and also want to know that in which event i should place this code to get proper output.

{
  int Post_Id = int.Parse(ViewState["ID"].ToString());
    SqlConnection cn1 = new SqlConnection();
    cn1.ConnectionString=
    ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
    SqlDataAdapter da = new SqlDataAdapter("SelectTags", cn1);
    DataTable ds = new DataTable();
    SqlCommand cmnd1 = new SqlCommand("SelectTags", cn1);
    cmnd1.Parameters.AddWithValue("@Post_Id",Post_Id);
    cmnd1.CommandType = CommandType.StoredProcedure;
    cn1.Open();
    cmnd1.ExecuteNonQuery();
    da.Fill(ds);
    cn1.Close();
    foreach (DataRow dr in ds.Rows)
    {
        String field1 = dr["Tag_Name"].ToString();
        CheckBoxList2.Items.Add(field1);
        CheckBoxList2.DataBind();
    } 
}

SQL query for sql server 2008

GO
/****** Object:  StoredProcedure [dbo].[InsertPost2Tag]    Script Date: 04/02/2013 09:47:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter PROCEDURE [dbo].[SelectTags]
    -- Add the parameters for the stored procedure here
@Post_Id int
AS
BEGIN

    SELECT mst_Tag.Tag_Name FROM mst_Tag INNER JOIN Post2Tag ON mst_Tag.tagId = Post2Tag.Tag_Id Where Post2Tag.Post_Id=@Post_Id

END
GO

2 Answers 2

1

Do this in page load witin

 if(!ispostback){
    CheckBoxList2.DataSource = ds; //This is the dataset that you fill from your stored procedure;
    CheckBoxList2.DataTextField = "Tag_Name";
    CheckBoxList2.DataValueField = "Tag_Name_Id";
    CheckBoxList2.DataBind();
 }

and take one more parameter Tag_Name_Id in your sp query..

 SELECT mst_Tag.Tag_Name,Tag_Name_Id FROM mst_Tag INNER JOIN Post2Tag ON mst_Tag.tagId = Post2Tag.Tag_Id Where Post2Tag.Post_Id=@Post_Id

Remove this from your code

foreach (DataRow dr in ds.Rows)
{
    String field1 = dr["Tag_Name"].ToString();
    CheckBoxList2.Items.Add(field1);
    CheckBoxList2.DataBind();
} 

Hope this helps... If it is what you were asking for?

Sign up to request clarification or add additional context in comments.

1 Comment

Yagnesh@ I have used panels. in one panel there is a grid view. in another panel there is a form view. i have used this checkboxlist inside that form view. now on selected index changed event of my grid view control i want to invisible the panel containing grid view and make form view panel visible.
0

No need to do any thing Just choose data source for check list box and set the session variable with selected value of grid view in its selected index changed event.

It worked for me... and too easy to implement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.