locked
print the students list based on gender RRS feed

  • Question

  • User34732094 posted

    Hi 

    I am trying to print student form based on gender.

      Actually i have a dropdownlist with Male and female.I am using sqldatasourcecontrol. When the form loads gridview displays all the students. but based on dropdownlist selection it should display the students. Is it possible to do that? Right now i am able to diplay the students in the different gridview?

     

    Thank you

    Wednesday, May 12, 2021 11:44 PM

Answers

  • User409696431 posted

    If you are using a RadioButtonList you can use that control, similar to the dropdownlist by changing it to

     <asp:ControlParameter ControlID="RadioButtonList1"
                Name="Gender"
                PropertyName="SelectedValue" />

    And making sure you have an initial selected value in your RadioButtonList if you want the gridview to load initially with All data:

                <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                    <asp:ListItem Selected="True" Value="All" Text="All" />
                    <asp:ListItem Value="Female" Text="Female" />
                    <asp:ListItem Value="Male" Text="Male" />
                </asp:RadioButtonList>

    Your requirement for a date range was not in your original post, and I have no idea what the real requirement is or how you are implementing it.  Multiple date ranges as additional selections, and an additional parameter for the datasource?  If so, how are they selected?

    Next, what has a picture to do with any of this?  Getting images stored in bytes in the database sounds like it would be best handled by posting it as a separate question.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 23, 2021 4:55 AM

All replies

  • User-1545767719 posted

    based on dropdownlist selection it should display the students in the same gridview. Is it possible to do that?

    Yes it is possible. Consider something like below:

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
        <asp:ListItem>ALL</asp:ListItem>
        <asp:ListItem>Male</asp:ListItem>
        <asp:ListItem>Female</asp:ListItem>
    </asp:DropDownList>
    
    <asp:SqlDataSource ID="SqlDataSource1"runat="server"
        ConnectionString="your connection string" 
        SelectCommand= "SELECT * FROM Students WHERE (@Gender='ALL' OR [Gender]=@Gender)"
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1"
                Name="Gender"
                PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
    
    <asp:GridView ID="GridView1" runat="server"
        ...
        DataSourceID="SqlDataSource1"
        ...>
    
        ...
    
    </asp:GridView>

    Thursday, May 13, 2021 2:57 AM
  • User34732094 posted

    Thank you for the reply. When you select 'All' It won't display data because table doesn't have any column which holds data as 'All'. I was trying to do with radiobuttons but nothing is working. select * from student where gender= @gender. but how to specify @gender value is coming from radiobutton?

    Thanks

    Tuesday, May 18, 2021 9:04 PM
  • User409696431 posted

    The SelectCommand you were shown does not assume there is a value of 'ALL' in the database Gender column.  It is doing a logical test to see if the dropdownlist selectedvalue is 'ALL'.  If it is, it will be true, and it will Select * from Students.  It it is not true, then has an OR  clause for the column Gender to equal the dropdownlist value.  If the two gender values are in the database, it will select data for the appropriate one, male or female.

    Now, while the example shows a dropdownlist with a value of ALL that will show up until one of the other two options are selected, it does not mean that that ALL is selected on pageload.  It will display as the first option, but it won't be technically selected.  If you look at the HTML for the page, you'll see:

    <select name="ctl00$MainContent$DropDownList1" onchange="javascript:setTimeout('__doPostBack(\'ctl00$MainContent$DropDownList1\',\'\')', 0)" id="MainContent_DropDownList1">
    	<option value="ALL">ALL</option>
    	<option value="Male">Male</option>
    	<option value="Female">Female</option>
    </select>

    There is no "selected" item.  To fix that, change the markup of the dropdownlist to explicitly specify the initial selected value.

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
        <asp:ListItem Selected="True">ALL</asp:ListItem>
        <asp:ListItem>Male</asp:ListItem>
        <asp:ListItem>Female</asp:ListItem>
    </asp:DropDownList>

    See if that solves the problem you were having with the dropdownlist.

    Tuesday, May 18, 2021 10:42 PM
  • User34732094 posted

    Hi Kathy,

     Thank you for the reply.

    But now the requirement is " To print the student data based on the gender and i have print student who is born between the two dates". I have radio buttons Male,Female,All. Since i have to use the date range i have to write sqlqueries. But  the only problem is i am unable to get the picture from the database to gridview. picture is stored in the databse in bytes.

    Thank you

    Sunday, May 23, 2021 1:28 AM
  • User409696431 posted

    If you are using a RadioButtonList you can use that control, similar to the dropdownlist by changing it to

     <asp:ControlParameter ControlID="RadioButtonList1"
                Name="Gender"
                PropertyName="SelectedValue" />

    And making sure you have an initial selected value in your RadioButtonList if you want the gridview to load initially with All data:

                <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                    <asp:ListItem Selected="True" Value="All" Text="All" />
                    <asp:ListItem Value="Female" Text="Female" />
                    <asp:ListItem Value="Male" Text="Male" />
                </asp:RadioButtonList>

    Your requirement for a date range was not in your original post, and I have no idea what the real requirement is or how you are implementing it.  Multiple date ranges as additional selections, and an additional parameter for the datasource?  If so, how are they selected?

    Next, what has a picture to do with any of this?  Getting images stored in bytes in the database sounds like it would be best handled by posting it as a separate question.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 23, 2021 4:55 AM
  • User34732094 posted

    Ok.Thanks Kathy. I will make it separate question.

    Sunday, May 23, 2021 2:09 PM
  • User-939850651 posted

    Hi entrylevel,

    Do you mean that your current problem is solved?

    If so, you can mark the answers that are useful to you. This will also help others in the community.

    Best regards,

    Xudong Peng

    Monday, May 24, 2021 8:38 AM
  • User34732094 posted

    Hi

     I can not use "All" at all because i am getting the values for dropdownlist from the table which has only Male and Female. Right now it is working for Male and Female. If i add "All" manually to the dropdownlist i may need to write the code.

    Thanks All

    Wednesday, May 26, 2021 6:04 PM
  • User409696431 posted

    Since you are using a RadioButtonList, this is for information only:  I already explained that you can use All in the dropdownlist.  The code you were shown for that does NOT look for "All" in the database.  It's a logical test to see if the dropdown value is "All".

    Thursday, May 27, 2021 4:46 AM