Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. search a range of data by date to display in datagrid view

search a range of data by date to display in datagrid view

Scheduled Pinned Locked Moved Visual Basic
cssdatabasesysadminsecuritytutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zaimah
    wrote on last edited by
    #1

    hi, i'm new in this. i want to display a list of data in my data grid view by the date that have been request. example i want a list from 1 feb 2008 untill 20 feb 2008. but nothing come out from the data grid except the title for each column in my datagrid. so this is the code. or is it something wrong with my datagrid properties [code] Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim startDate As String Dim endDate As String Dim ds As DataSet = Nothing startDate = y1.Text endDate = y2.Text Try Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Senarai_Log where Date >= '" & startDate & "' and Date <= '" & endDate & "'", New SqlConnection("Server=MAHANI;Database=SPGALatihan;Persist Security Info=True;User ID=sysadm;Password=sysadm")) cmd.Connection.Open() Dim sqlDR As SqlDataReader = cmd.ExecuteReader() DataGrid1.DataSource = sqlDR DataGrid1.Visible = True DataGrid1.DataBind() DataGrid1.DataSource = Nothing cmd.Connection.Close() DataGrid1.Visible = True Catch ex As Exception ds = Nothing Label8.Visible = True Label8.Text = "NO DOKUMEN INI TIADA DALAM REKOD. SILA SEMAK DI DALAM SENARAI LOG" End Try End Sub End Class [/code]

    D 1 Reply Last reply
    0
    • Z zaimah

      hi, i'm new in this. i want to display a list of data in my data grid view by the date that have been request. example i want a list from 1 feb 2008 untill 20 feb 2008. but nothing come out from the data grid except the title for each column in my datagrid. so this is the code. or is it something wrong with my datagrid properties [code] Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim startDate As String Dim endDate As String Dim ds As DataSet = Nothing startDate = y1.Text endDate = y2.Text Try Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Senarai_Log where Date >= '" & startDate & "' and Date <= '" & endDate & "'", New SqlConnection("Server=MAHANI;Database=SPGALatihan;Persist Security Info=True;User ID=sysadm;Password=sysadm")) cmd.Connection.Open() Dim sqlDR As SqlDataReader = cmd.ExecuteReader() DataGrid1.DataSource = sqlDR DataGrid1.Visible = True DataGrid1.DataBind() DataGrid1.DataSource = Nothing cmd.Connection.Close() DataGrid1.Visible = True Catch ex As Exception ds = Nothing Label8.Visible = True Label8.Text = "NO DOKUMEN INI TIADA DALAM REKOD. SILA SEMAK DI DALAM SENARAI LOG" End Try End Sub End Class [/code]

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      zaimah wrote:

      Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Senarai_Log where Date >= '" & startDate & "' and Date <= '" & endDate & "'",

      It's best that you learn parameterized queries. The reason it doesn't work is because the dates are in the wrong format and your SQL is improperly written. I highly suggest reading this[^] to understand parameterized queries and what your wrote it a huge security problem.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      Z 1 Reply Last reply
      0
      • D Dave Kreskowiak

        zaimah wrote:

        Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Senarai_Log where Date >= '" & startDate & "' and Date <= '" & endDate & "'",

        It's best that you learn parameterized queries. The reason it doesn't work is because the dates are in the wrong format and your SQL is improperly written. I highly suggest reading this[^] to understand parameterized queries and what your wrote it a huge security problem.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        Z Offline
        Z Offline
        zaimah
        wrote on last edited by
        #3

        thanks dave for giving me the article that r related. but as i said b4, i'm new in this, i didn't understand much about the article except 4 the error msg explaination. in my prgm the user suppose to enter date in this format "dd/mm/yyyy". so, by saying it's in the wrong format is it means that i should not declare it as string? sorry again for my english is not so good. in what way does my code can make injection attacks happen?

        D 1 Reply Last reply
        0
        • Z zaimah

          thanks dave for giving me the article that r related. but as i said b4, i'm new in this, i didn't understand much about the article except 4 the error msg explaination. in my prgm the user suppose to enter date in this format "dd/mm/yyyy". so, by saying it's in the wrong format is it means that i should not declare it as string? sorry again for my english is not so good. in what way does my code can make injection attacks happen?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          OK, it sounds like you really need to pickup a beginners book on VB.NET and work through it. Your database should not be storing dates and times as strings. This makes it very difficult to retrieve a set of records between two dates. They should be stored as SQL Date types instead. When you go to search the database for records between two entered dates, you have to convert the text entered by the user (this is a string) to a DateTime object. Look at the documentation for DateTime.TryParse[^] for this. When you get the two dates, you use them as parameters in a parameterized SQL query that does the searching. That's where that article link I posted comes in. That shows you how to setup and use parameterized queries. Concantenting strings together makes for some very bad, and easily breakable, SQL code that can be difficult to find bugs in.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          Z 1 Reply Last reply
          0
          • D Dave Kreskowiak

            OK, it sounds like you really need to pickup a beginners book on VB.NET and work through it. Your database should not be storing dates and times as strings. This makes it very difficult to retrieve a set of records between two dates. They should be stored as SQL Date types instead. When you go to search the database for records between two entered dates, you have to convert the text entered by the user (this is a string) to a DateTime object. Look at the documentation for DateTime.TryParse[^] for this. When you get the two dates, you use them as parameters in a parameterized SQL query that does the searching. That's where that article link I posted comes in. That shows you how to setup and use parameterized queries. Concantenting strings together makes for some very bad, and easily breakable, SQL code that can be difficult to find bugs in.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            Z Offline
            Z Offline
            zaimah
            wrote on last edited by
            #5

            thanks a lot for ur explaination. i'm really new in this, so many people that r expert in this usually don't entertain a beginner like me. thanks a lot. i will buy the book that u have suggested. one more thing, i'm confuse about one thing, what is the different between vb.net and asp.net? I'm using visual studio. if i see the html view, i can see the word , so i know it is an asp coding. but if see in the aspx.vb, which is the one that r asp? i'm still confuse in that.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups