Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have an issue
I have 2 Checkbox list populated the data from database. It is fine
One is Industry, Another one is Department

If i select multiple selection from checkbox list,
i need load data into grid view control based on selection.

Html tag
ASP.NET
<div class="col-lg-3">
    <asp:Label ID="Label1" runat="server" Text="Industry" ForeColor="Red" Font-Bold="true"></asp:Label>
    <div style="width:230px;height:300px; padding:1px; overflow:auto; border: 1px solid #ccc;">
        <asp:CheckBoxList ID="chkIndustry" runat="server" CssClass="form-control-lst" RepeatLayout="table"
                RepeatColumns="1" RepeatDirection="vertical"></asp:CheckBoxList>
    </div>
</div>

<div class="col-lg-3">
    <asp:Label ID="Label2" runat="server" Text="Department" ForeColor="Red" Font-Bold="true"></asp:Label>
    <div style="width:230px;height:300px; padding:1px; overflow:auto; border: 1px solid #ccc;">
        <asp:CheckBoxList ID="chkDepartment" runat="server" CssClass="form-control-lst" RepeatLayout="table"
                RepeatColumns="1" RepeatDirection="vertical"></asp:CheckBoxList>
    </div>
</div>

   <asp:GridView ID="GridView1" runat="server" CssClass="gridview" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
        BorderWidth="1px" CellPadding="3" PageSize="15">
        <Columns>
            <asp:CommandField ShowSelectButton="True">
                <ItemStyle Width="50px" />
            </asp:CommandField>

            <asp:TemplateField ShowHeader="False">

                <HeaderTemplate>
                    <asp:CheckBox ID="allchk"  runat="server" />
                </HeaderTemplate>

                <ItemTemplate><asp:CheckBox ID="chk" runat="server" /></ItemTemplate>
                <ItemStyle Width="10px" />
            </asp:TemplateField>
            <asp:BoundField DataField="Code" HeaderText="Code" />
            <asp:BoundField DataField="name" HeaderText="CoName"  />
            <asp:BoundField DataField="industry" HeaderText="Industry"  />
            <asp:BoundField DataField="NAMED" HeaderText="Delegate"  />
            <asp:BoundField DataField="status" HeaderText="Status"  />


        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>


VB
Public Function LoadParamDetails_CHK(chkList As CheckBoxList, strParamHead As String) As String
      LoadParamDetails_CHK = String.Empty
      Dim sConstr As String = ConfigurationManager.ConnectionStrings("ConnectString").ConnectionString
      Dim Conn As New SqlConnection(sConstr)
      Using Conn
          Dim command As New SqlCommand("Select * from Parameter where paramhead ='" & strParamHead & "' order by paramdetails", Conn)
          Dim da As New SqlDataAdapter()
          Dim dt As New DataTable()
          command.CommandType = CommandType.Text
          Conn.Open()
          da.SelectCommand = command
          da.Fill(dt)
          chkList.DataSource = dt
          chkList.DataValueField = "id"
          chkList.DataTextField = "paramdetails"
          chkList.DataBind()
      End Using
      Conn = Nothing
      Return LoadParamDetails_CHK
  End Function

Call Function
LoadParamDetails_CHK(Me.chkIndustry, "industry")
LoadParamDetails_CHK(Me.chkDepartment, "department")


I used try this code. It is populated only one industry even though i have selected Multiple industry and also I am blur how to add selected department into this code from Department checkbox list
Private Sub LoadSelectDelegate()
    For i As Integer = 0 To Me.chkIndustry.Items.Count - 1
        If Me.chkIndustry.Items(i).Selected = True Then
            Dim query As String = "select * from vw_Client_HD where industry = '" + Me.chkIndustry.Items(i).Text + "'"

            Dim dt As DataTable = GetData(query)
            Me.GridView1.DataSource = dt
            Me.GridView1.DataBind()
        End If

    Next

Pls advice me.

thank you maideen

What I have tried:

Private Sub LoadSelectDelegate()
    For i As Integer = 0 To Me.chkIndustry.Items.Count - 1
        If Me.chkIndustry.Items(i).Selected = True Then
            Dim query As String = "select * from vw_Client_HD where industry = '" + Me.chkIndustry.Items(i).Text + "'"

            Dim dt As DataTable = GetData(query)
            Me.GridView1.DataSource = dt
            Me.GridView1.DataBind()
        End If

    Next
Posted
Updated 25-Mar-18 5:00am
v2

1 solution

First of all, your code is Sql Injection[^] vulnerable!

How to avoid of Sql Injection? Please, read these:
How To: Protect From SQL Injection in ASP.NET[^]
SQL Injection Attack – Security Research & Defense[^]
SQL Injection and how to avoid it – ASP.NET Debugging[^]
Writing Secure Dynamic SQL in SQL Server | Microsoft Docs[^]

Tip: use stored procedures[^] to get your data!
How to call SQL Server stored procedures in ASP.NET by using Visual Basic .NET[^]
How to: Execute a Stored Procedure that Returns Rows[^]

As to this statement:
Quote:
I used try this code. It is populated only one industry even though i have selected Multiple industry and also I am blur how to add selected department into this code from Department checkbox list

VB
Private Sub LoadSelectDelegate()
    For i As Integer = 0 To Me.chkIndustry.Items.Count - 1
        If Me.chkIndustry.Items(i).Selected = True Then
            Dim query As String = "select * from vw_Client_HD where industry = '" + Me.chkIndustry.Items(i).Text + "'"

            Dim dt As DataTable = GetData(query)
            Me.GridView1.DataSource = dt
            Me.GridView1.DataBind()
        End If
    Next



Imagine, in every step of for... next loop, you're overriding previous result. ;)
If i understand you correctly, you want to get the list of industries which industry name corresponds to selected value (or values).
So, you need to use IN (Transact-SQL) | Microsoft Docs[^]
SQL
SELECT *
FROM vw_Client_HD
WHERE industry IN ('industry1', 'industry2', 'industry3')


To be able to achieve that you have to pass a list of industries as a delimited text to sql stored procedure. Inside this procedure you have to split delimited text into values. See: Converting comma separated data in a column to rows for selection[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900