SELECT MONTH AND YEAR FROM DATE FIELD FOR SQL DATASOURCE FOR A GRID VIEW

Baiju EP 141 Reputation points
2023-02-03T09:24:00.44+00:00

I have a gridview with sqldata source in my aspx+Vb +sql web. There is a date field in my table

have on dropdownlist mthtxt.text and another dropdownlist yrtxt.text

Table name is Clinic_Med_Issue

Empno Name Medicine qty effdate

MM001 Baiju Citrizine 02 2021-09-16 20:12:53.000

MM002 Sanath Dolo 650mg 02 2021-09-16 18:12:53.000

MM004 Radha Cough Syrup 02 2021-09-14 20:12:53.000

MM003 Krishna Citrizine 02 2021-09-13 20:12:53.000

MM001 Baiju Dolo 650mg 02 2021-09-17 21:00:00.000

I am using sqldata source for a gridview

<asp:SqlDataSource ID="gridsource" runat="server" ConnectionString="<%$ ConnectionStrings:baijuep %>" SelectCommand="SELECT * FROM [Clinic_Med_Issue]">

I wanted Select * from Clinic_Med_Issue where Month(effedate) = mth.text and Year(effdate)=yrtxt.text

but its not working in sql data source

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,238 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,558 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,231 Reputation points Microsoft Vendor
    2023-02-06T05:20:21.3066667+00:00

    Hi @Baiju EP ,

    You can provide an example or error message that reproduces the problem.

    I wrote an example based on your description, I hope it will help you.

     <div>
                <asp:DropDownList ID="mth" runat="server">
                    <asp:ListItem>Select mouth</asp:ListItem>
                    <asp:ListItem>1</asp:ListItem>
                    <asp:ListItem>2</asp:ListItem>
                    <asp:ListItem>9</asp:ListItem>
                    <asp:ListItem>8</asp:ListItem>
                </asp:DropDownList>
                <asp:DropDownList ID="yrtxt" runat="server">
                    <asp:ListItem>Select year</asp:ListItem>
                    <asp:ListItem>2021</asp:ListItem>
                    <asp:ListItem>2022</asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DBCS %>"
                   ></asp:SqlDataSource>
            </div>
    
      Protected Sub Button1_Click(sender As Object, e As EventArgs)
            SqlDataSource1.SelectCommand = "SELECT  Empno, Name ,Medicine ,qty ,effdate from Clinic_Med_Issue where Month(effdate)  ='" & mth.SelectedItem.ToString() & "' and Year(effdate)  ='" & yrtxt.SelectedItem.Value.ToString() & "'"
            GridView1.DataSource = SqlDataSource1
            GridView1.DataBind()
        End Sub
    

    DEMO

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful