Incorrect date format and object reference problem

Taraoro

New member
Joined
Apr 14, 2010
Messages
2
Programming Experience
Beginner
Hi, I'm having a few problems with a project in VB and I was hoping you could help me figure them out.
The first problem I'm having is this:
I'm trying to update stock in a table using SQL through vb.net, the SQL code looks fine and I can't see any problems in it, but it keeps telling me that there was a problem with an incompatible date format - the table being written to is in a sql database and the dates are both set to 'datetime'.
This is what I'm using to write to the table:

VB.NET:
DataCommand.CommandText = "INSERT INTO [Rented DVDs](MemberID,DVDID,RentedDateRented,RentedDateDue,RentedOverdue) VALUES('" _
        & Trim(TextBox2.Text) & "','" & Trim(TextBox1.Text) & "','" & DateTime.Today.Date() & _
        "','" & DateTime.Today.Date.AddDays(Trim(TextBox3.Text)) & "','No')"
I can't figure out what's wrong with it...

The second problem is this:
I'm trying to use a textfile to record some simple stats and then use them to generate a bar graph but I get "Object reference not set to an instance of an object." error...I can't see where the problem is here, it's probably something obvious but this is the code I'm using to write to the file:
VB.NET:
Public Class RentADVD
    Dim Valid As Boolean = False
    Dim MemVal As Boolean = False
    Dim DVDVal As Boolean = False
    Dim DaysVal As Boolean = False
    Dim DVDChecker As String = ""
    Dim MemChecker As String = ""
    Dim InStock As Integer = 0
    Dim Genre As String
    Dim Reader As System.IO.StreamReader = System.IO.File.OpenText("Stats.txt")
    Dim Writer As System.IO.StreamWriter
    Dim StringFind As Integer
    Dim SaleString As String
    Dim Sales As Integer
    Public Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim DataConnect As SqlServerCe.SqlCeConnection
        Dim DataCommand As SqlServerCe.SqlCeCommand

        Dim Line As String = "0"

        Do
            Line = Reader.ReadLine()
'The line below is where I get the error.
            If Line.StartsWith(Genre) Then
                StringFind = Line.IndexOf(",")
                SaleString = Line.Substring(StringFind)
                Sales = CInt(SaleString + 1) + 1
                Writer.WriteLine(Genre, Sales)
            ElseIf EOF("Stats.txt") = True Then
                Writer.WriteLine(Genre, 1)
            End If

        Loop Until (Line = Nothing)
I thought it could be something to do with genre, but when I looked at it in debug mode it had the correct value for it, here's where genre is from:
VB.NET:
    Public Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim CheckDVD As SqlServerCe.SqlCeCommand
        Dim DataConnect As SqlServerCe.SqlCeConnection
        Dim DataR As SqlServerCe.SqlCeDataReader

        If TextBox1.Text <> DVDChecker Then
            DVDChecker = ""
        End If
        Try
            If TextBox1.TextLength = 4 Then
                DataConnect = Connect()
                DataConnect.Open()
                CheckDVD = DataConnect.CreateCommand
                CheckDVD.CommandText = "SELECT * FROM Rental WHERE DVDID=" & Trim(TextBox1.Text)
                DataR = CheckDVD.ExecuteReader
                While DataR.Read
                    InStock = DataR(4)
                    DVDChecker = DataR(0)
                    Genre = DataR(3)
                End While
                If DVDChecker = Nothing Then
                    MessageBox.Show("This DVD ID does not exist in the database!")
                ElseIf InStock < 1 Then
                    MessageBox.Show("This DVD is not in stock!")
                End If
                DataConnect.Close()
            End If

        Catch ex As Exception
            MessageBox.Show("There was a problem connecting to the database!")
        End Try

They're both probably some stupid thing I've forgotten to do or check as I'm pretty new to programming, but I'd appreciate some help :)
 

oliverg

Active member
Joined
Apr 14, 2010
Messages
37
Programming Experience
1-3
in your initial declarations try:

Private genre as new string = ""

Is genre assigned to every time your textbox1 text is changed?
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
The problem is that you are passing it a date, in the format of a string. If you were to pass it as a date, it would be fine. Solution:

1. Turn Option Strict On.
2. See the link in my signature about Parameters, and change all your queries to use parameters.
 

Taraoro

New member
Joined
Apr 14, 2010
Messages
2
Programming Experience
Beginner
Using Private genre as new string = "" made no difference, and yes it's assigned a value every time textbox1 length = 4.

I'll try parameterizing, thanks :D
 
Top Bottom