How to compare values from database with a current date

Hi,

I have a form with a button that when the user clicks on a button it should check - if a report already exist or not.
If it exists it should show a message telling the user that report already exist or display another message telling the user to enter data for a new report and then inserting default values like 0.

Could someone please looks at my code because I am not sure what I am doing wrong.
 
Thanks!

Public Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click

        Dim strConnString As String = "server=***;database=***;uid=***;pwd=***"
        Dim objConn As New SqlConnection(strConnString)
        Dim strInsert As New SqlCommand
        Dim TodayDate As DateTime = DateTime.Now

        Dim strSQL As String
        strSQL = "SELECT * FROM StudentsTable WHERE ReportDate >= DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)"

        Dim cmd As New SqlCommand(strSQL, objConn)
        Dim reader As SqlDataReader
        objConn.Open()
        reader = cmd.ExecuteReader

        While (reader.Read())
            If (String.Compare(reader.Itme("ReportDate").ToString(), TodayDate)  = 0) Then  


                MessageBox("Report for this date already exists." & _
                Microsoft.VisualBasic.vbNewLine)

                'objConn.Close()

            ElseIf (String.Compare(reader.Item("ReportDate").ToString, TodayDate) < 0) Then

                strInsert.CommandText = "Insert StudentsTable(ReportName, ReportDate, ClassA, ClassB, ClassC, CFlag) " & _
                 "Values('Report_'+REPLACE(CONVERT(varchar(10),GETDATE(),101),'/',''),GETDATE(),0,0,0,0)"

                strInsert.Connection = objConn
             
                objConn.Open()
           
                strInsert.ExecuteNonQuery()

                MessageBox("Enter data for a new report." & _
                Microsoft.VisualBasic.vbNewLine)

            End If
          End While
   
        objConn.Close()

    End Sub
avi4Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

williamcampbellCommented:
If (String.Compare(reader.Itme("ReportDate").ToString(), TodayDate)  = 0) Then  


Typo  ''' Itme "" 
0
Edwin_CCommented:
It seems to me that the StudentsTable contains only one record (or none) record for each ReportDate.  If that is true, your loop may be doing the job right.

Because you first selected all records with ReportDate >= current date, i.e. records with current ReportDate and future ReportDate

In the first If, you checked if the ReportDate equals the current date.  If it does, you pop a message but the loop continues.  That means any records with a future ReportDate will get executed in the ElseIf part and you insert a new record for each row with a future ReportDate.

Perphaps you can try a better way of doing this.

        Dim strSQL As String
        strSQL = String.Format("SELECT ReportName FROM StudentsTable WHERE ReportDate = '{0:dd MMM yyyy}'", DateTime.Today)

        Dim cmd As New System.Data.SqlClient.SqlCommand(strSQL, objConn)
        objConn.Open()
        Dim obj As Object = cmd.ExecuteScalar()
         If (obj = vbNull) Then
            'insert new report here
        Else
            'report alreay exist, show your message
        End If
        objConn.Close()

Hope that helps
Edwin

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
avi4Author Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.