Hafiz Abdullah
Hafiz Abdullah

Reputation: 248

Unhandled exception issue vb.net

I created a simple form which call data from the database and it works very well. The problem is it does not work on some computers. I already tried running it on 5 different computers and only 3 of them execute as it should be. The other 2 computers shows Unhandled Exception errors pop up which is "String was not recognized as a valid Date Time". When I clicked "Details" button on the pop up, it shows that the error is in this lines of codes:-

 SQLConnection.Open()
 cmd = New MySqlCommand("SELECT * FROM jobimport WHERE no =('" & jobid & "')",     SQLConnection)

 read = cmd.ExecuteReader

    If read.HasRows = False Then
        '  MessageBox.Show("No record found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)


    Else
        Do While read.Read

            date1 = read("date")
            jobno = read("jobno")
            marking = read("marking")
            consignee = read("consignee")
            hblno = read("hblno")
            blno = read("blno")
            eta = read("eta")
            vessel = read("vessel")
            description = read("description")

        Loop

        DateTimePicker1.Value = DateTime.Parse(date1) ' <--- Error on this line
        TextBox1.Text = jobno
        TextBox2.Text = marking
        TextBox3.Text = consignee
        TextBox13.Text = hblno
        TextBox4.Text = blno
        TextBox6.Text = vessel
        TextBox7.Text = description

       End If

    SQLConnection.Close()
    SQLConnection.Dispose()

All the variables above are in String data type.

I tried to fix it by doing this

    DateTimePicker1.Text = date1

and this

    DateTimePicker1.Value = Convert.ToDateTime(date1)

but it still gives out the same error on those 2 computers while the other 3 computers works great without problem. Any idea?

Upvotes: 0

Views: 215

Answers (1)

har07
har07

Reputation: 89325

Try to change this line :

DateTimePicker1.Value = DateTime.Parse(date1)

To use ParseExact supplying relevant CultureInfo as third parameter. For example :

DateTimePicker1.Value = DateTime.ParseExact(date1, "d/m/yyyy", CultureInfo.InvariantCulture)

With ParseExact, application will parse your date string using supplied CultureInfo regardless of locale setting used in computer it run.

Upvotes: 2

Related Questions