Skip to content

Comma delimited string to chkbList in gridview from databse?

Hassan Alhag

Asked Feb 24, 2017, 8:47 PM EST1 answer
Share
LinkedInX

Hi, All,

I have a problem to populate a comma delimited string from the database stored in Arabic like this (????? ????,???? ????? ?????,???? ???? ?????,???? ?????,) to a checkboxlist inside a gridview edit template?

 
here what my Code Look like:
 
<Columns>
<asp:BoundField DataField="TransactionName" HeaderText="Transaction Name" />
<asp:BoundField DataField="Category" HeaderText="Category" />
<asp:TemplateField HeaderText="Steps">
<ItemTemplate>
<asp:Label ID="lblBrand" runat="server" Text='<%#Eval("Steps") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
 
 
Java
 string checkBoxValues = "";
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBoxList chkBrand = (CheckBoxList) e.Row.FindControl("CheckBoxList1");
            String SQL = "SELECT * FROM TransactionDetail";


            string sConstr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(sConstr))
            {
                using (SqlCommand comm = new SqlCommand(SQL, conn))
                {
                    conn.Open();

                    SqlDataReader myReader = null;
                    myReader = comm.ExecuteReader();

                    while (myReader.Read())
                    {
                        checkBoxValues = (myReader["Steps"].ToString());

                    }
                    foreach (string value in checkBoxValues.Split(','))
                    {
                        ListItem item = new ListItem();
                        item.Text = value;
                        item.Value = value;
                        chkBrand.Items.Add(item);
                    }

                }

            }

        }
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1

    What is the issue you are getting. As per my understanding you are always binds the last record for all the row

     

     if (e.Row.RowType == DataControlRowType.DataRow)
    Java
            {
                CheckBoxList chkBrand = (CheckBoxList) e.Row.FindControl("CheckBoxList1");
                String SQL = "SELECT * FROM TransactionDetail";
    
    
                string sConstr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(sConstr))
                {
                    using (SqlCommand comm = new SqlCommand(SQL, conn))
                    {
                        conn.Open();
    
                        SqlDataReader myReader = null;
                        myReader = comm.ExecuteReader();
    
                        while (myReader.Read())
                        {
                            checkBoxValues = (myReader["Steps"].ToString());
    
                        }
                        foreach (string value in checkBoxValues.Split(','))
                        {
                            ListItem item = new ListItem();
                            item.Text = value;
                            item.Value = value;
                            chkBrand.Items.Add(item);
                        }
    
                    }
     
    +1