locked
sql date from database to mm/yyyy RRS feed

  • Question

  • User1717218719 posted

    I have the following code which works prefectly, but now I would like to add an extra condition for returning the data. At present I have two textboxes in which the user enters a value and presses the summit button and if those values are in the sql database they are returned in a gridview format onscreen.

    I would like to add another textbox which acts like a third criteria. The issue I have though is the column is a date. in the SQL database it is in thf form '2018-12-01 00:00:00'. however I would like the user to be able to enter the date in the form MM/YYY into the textbox.
    How would I go about doing this. Many thanks

            Dim conConn As SqlConnection = Nothing
            Dim comComm As SqlCommand = Nothing
            Dim adpAdap As SqlDataAdapter = Nothing
            Dim Dst As DataSet = New DataSet()
    
            comComm = New SqlCommand
            conConn = New SqlConnection(GblSqlCon)
            With comComm
                .Connection = conConn
                .CommandType = CommandType.Text
                .CommandText = "SELECT * FROM tbl_x WHERE Ttl >= 50 and (Cde= @Cde) AND (ID = @ID) " ' AND (dte = @dte)"
    
                .Parameters.AddWithValue("@Cde", argparam)
                .Parameters.AddWithValue("@ID", ID)
                '  .Parameters.AddWithValue("@dte", dte)
            End With
    
            adpAdap = New SqlDataAdapter(comComm)
            adpAdap.Fill(Dst, "tbl")
    
    
            '--Gridview
            GridView1.DataSource = Dst.Tables("tbl")
            GridView1.DataBind()
    
    
    
    
    
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Call GetDataBase1(txtbx1.Text, txtbx2.Text, txtbx3.Text)
    
        End Sub
    

    Tuesday, September 3, 2019 11:02 AM

Answers

  • User1717218719 posted

    Hi paindaasp,

    I just solved my probling using this in my sql statement

    cast('01-dec-2018' as smalldatetime)

    thankyou for your reply.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 3, 2019 12:19 PM

All replies

  • User3690988 posted

    Would converting your textbox into the first day of the month work?

        Dim mmYYYY() As String = txtDte.Text.Split("/")
        Dim dte As String = mmYYYY(0) & "/01/" & mmYYYY(1)

    Tuesday, September 3, 2019 12:16 PM
  • User1717218719 posted

    Hi paindaasp,

    I just solved my probling using this in my sql statement

    cast('01-dec-2018' as smalldatetime)

    thankyou for your reply.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 3, 2019 12:19 PM