Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have two tables, Item group and Items, where in I need to publish the drop downs in such a way that all the group names should be listed in first drop down and items should be listed in second dropdown according to the selected value in first drop down.

I know using cascading dropdown of ajax would be the easy method and I tried it but I am unable to understand few terms they have mentioned in example.

So tried with regular drop down.

I am using datasource for first drop down and populating it .

My problem is I am getting the required result in first dropdown but in second dropdown I am getting duplicate values

please let me know where I am going wrong

XML
<td valign="top" width="100">
                            &nbsp;</td>
                        <td align="right" valign="middle" width="200">
                            <asp:Label ID="lblgroup" runat="server" Font-Bold="True"
                                Font-Names="Palatino Linotype" Font-Size="Small" Text="Item Group :"></asp:Label>
                        </td>
                        <td align="left" height="30" valign="middle" width="200">
                            <asp:DropDownList ID="ddlgroup" runat="server" DataSourceID="SqlDataSource1"
                                Font-Bold="False" Font-Names="Palatino Linotype" Font-Size="Small"
                                Width="150px" AutoPostBack="True" DataTextField="ITEMGROUPNAME"
                                DataValueField="ITEMGROUPNAME">
                            </asp:DropDownList>
                        </td>
                        <td width="250">
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td valign="top" width="100">
                            &nbsp;</td>
                        <td align="right" valign="middle" width="200">
                            <asp:Label ID="lblitem" runat="server" Font-Bold="True"
                                Font-Names="Palatino Linotype" Font-Size="Small" Text="Item :"></asp:Label>
                        </td>
                        <td align="left" height="30" valign="middle" width="200">
                            <asp:DropDownList ID="ddlitems" runat="server" Font-Names="Palatino Linotype"
                                Font-Size="Small" AutoPostBack="True" Width="150px">
                            </asp:DropDownList>

XML
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
                                ConnectionString="<%$ ConnectionStrings:conitemstring %>"
                                SelectCommand="SELECT [ITEMGROUPNAME] FROM [ITEMGROUP]"></asp:SqlDataSource>



.vb page
This below method is for displaying the second drop down based on first drop downs selected values

VB
Protected Sub ddlgroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlgroup.SelectedIndexChanged
    ddlitems.Items.Clear()
    sqlstr = "Select ITEMGROUPCODE FROM ITEMGROUP WHERE ITEMGROUPNAME='" + ddlgroup.SelectedValue.ToString + "'"
    sqlrdr = SqlHelper.ExecuteReader(sqlconn, CommandType.Text, sqlstr)
    sqlrdr.Read()
    sqlstr = "SELECT ITEMMASTER.ITEMCODE,ITEMMASTER.ITEMGROUPCODE,ITEMMASTER.ITEMNAME FROM ITEMMASTER,ITEMGROUP WHERE ITEMMASTER.ITEMGROUPCODE='" + sqlrdr(0).ToString + "'"
    sqlrdr.Close()
    sqlrdr = SqlHelper.ExecuteReader(sqlconn, CommandType.Text, sqlstr)
    While (sqlrdr.Read())
        ddlitems.Items.Add(sqlrdr(2))
    End While
    sqlrdr.Close()
End Sub


The below method is to display existing columns with the same item name as that of selected in second gridview

VB
Protected Sub ddlitems_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlitems.SelectedIndexChanged
      gdvitems.Dispose()
      sqlstr = "SELECT ITEMCODE FROM ITEMMASTER WHERE ITEMNAME='" + ddlitems.SelectedValue.ToString + "'"
      sqlrdr = SqlHelper.ExecuteReader(sqlconn, CommandType.Text, sqlstr)
      sqlrdr.Read()
      itemid = sqlrdr(0)
      sqlrdr.Close()
      sqlstr = "SELECT * FROM Item_loyalty_points_master WHERE ITEMCODE='" + itemid.ToString + "'"
      sqlcmd = New SqlCommand(sqlstr, sqlconn)
      sqladap = New SqlDataAdapter(sqlcmd)
      sqlcmd.ExecuteNonQuery()
      sqladap.Fill(ds)
      'sqldr = SqlHelper.ExecuteReader(sqlconn, CommandType.Text, sqlconn)
      'While sqldr.Read()
      gdvitems.DataSource = ds
      'End While
      gdvitems.DataBind()
  End Sub
Posted
Updated 19-Oct-10 21:41pm
v3
Comments
Dalek Dave 20-Oct-10 3:41am    
Edited for Grammar and Syntax.
William Winner 20-Oct-10 16:36pm    
I don't know how to answer your question, but I had some comments about your code. First, if you are only going to use one of the columns from your SELECT statement, why would you include more than one column in the SELECT to begin with? Second, in your SELECT where you select multiple columns, you say to select from two different tables, but you don't actually select anything from the second table, so there is no need to list ITEMPGROUP in your FROM statement. You could just as well write:
"SELECT ITEMNAME FROM ITEMMASTER WHERE ITEMGROUPCODE='" + sqlrdr(0).ToString + "'"

1 solution

If the question is just about duplicate values then you can add DISTINCT clause in you SQL query (And of course select just single column that you want to use).
 
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