Parameter Error after Catching Exception

tpra21

Active member
Joined
Oct 21, 2006
Messages
26
Programming Experience
1-3
The code below will add a record to my Reminders table. The DueDate is a date type in mm/dd/yyyy format. When I run this and enter an invalid date in the txtDate textbox, it throws and catches a format exception and displays a message box.

The problem is after the user clicks ok on the error message box, enters another date, and clicks the button that the below code is in, the program throws the following exception:

Parameter ? _ 1 has no default value.

It seems like the error message box is clearing my dataset or something. I don't understand why the DueDate parameter would be empty if the user types in a valid date after getting the error.

Any help would be great.

Thanks, Adam

VB.NET:
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbInsertCommand1.CommandText = "INSERT INTO Reminders(DueDate, Message) VALUES (?, ?)"[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbInsertCommand1.Connection = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbConnection2[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbInsertCommand1.Parameters.Add("@DueDate", System.Data.OleDb.OleDbType.DBDate, 0, "DueDate").Value = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2](txtDate.Text)[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbInsertCommand1.Parameters.Add("@Message", System.Data.OleDb.OleDbType.VarWChar, 255, "Message").Value = txtMessage.Text[/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2]OleDbConnection2.Open()[/SIZE]
[SIZE=2]OleDbInsertCommand1.ExecuteNonQuery()[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]MessageBox.Show(ex.Message)[/SIZE]
[SIZE=2][COLOR=#0000ff]Finally[/COLOR][/SIZE]
[SIZE=2]OleDbConnection2.Close()[/SIZE]
[SIZE=2]MessageBox.Show("The Record Has Been Added", "Reminder Added", MessageBoxButtons.OK, MessageBoxIcon.Information)[/SIZE]
[SIZE=2]Reminders1.Reminders.Clear()[/SIZE]
[SIZE=2]OleDbDataAdapter1.Fill(Reminders1)[/SIZE]
[SIZE=2]btnAdd.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]btnUpdate.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]btnDelete.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]ComboBox1.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]btnConfirm.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]btnConfirm.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]btnConfirm.Text = ""[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] InvalidCastException [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]MessageBox.Show("Invalid format. Please enter in mm/dd/yyyy format", _[/SIZE]
[SIZE=2]"Invalid Format", _[/SIZE]
[SIZE=2]MessageBoxButtons.OK, MessageBoxIcon.Exclamation)[/SIZE]
[SIZE=2]Reminders1.Reminders.Clear()[/SIZE]
[SIZE=2]OleDbDataAdapter1.Fill(Reminders1)[/SIZE]
[SIZE=2]txtDate.Text = ""[/SIZE]
[SIZE=2]txtDate.Focus()[/SIZE]
[SIZE=2][COLOR=#0000ff]Finally[/COLOR][/SIZE]
[SIZE=2]ComboBox1.Focus()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
 
The way you are going about this is a bit wrong, if you don't mind my saying so. You shouldn't be using exceptions as a form of validation. Instead handle the textbox Validating event you can, from there cancel the operation if the date isn't in the correct format. Also regular expressions are a usful way of determining if a date is in the correct format. One example would be.

VB.NET:
 [/B]
[B](0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d[/B]
[B]

Yea, i know what you are thinking... What the hell is that. Well basically it will determine valid dates including making sure that the dates can actually be a date. Example, you wouldn't be able to enter

VB.NET:
41/13/0001

To use it check out the regex class. You can add the following pattern in the constructor and then in the validating event determine if there is a match, if there is then continue, otherwise cancel the event (e.cancel = true) and prompt the user to re-enter the date. Much cleaner that catching exceptions.
 
I can't see why you wouldn't use a DateTimePicker for the date input. Then the user can't enter an invalid value. You simply assign the Value property of the DTP to your parameter's Value property without any thought about format whatsoever. A Date is a Date and format doesn't enter into it if no conversion to ro from a string is involved.
 
You are the first person I have ever seen, who is using VS2002 / NET 1.0

Is that for real?
 
First, thanks for the expression. I didn't know those even existed! And as for me not doing it right by using exception handling, that is the criticism I need to become better at programming VB (I am fairly new to VB.Net), so thanks. Secondly, I did realize that it would be better to use a dateTimePicker, so I did that right after I made the original post. Lastly, I am using VisualBasic.Net Standard; Version 2002, so maybe I listed it wrong at signup.

Thanks everyone, Adam
 
You have selected the correct option in your profile. It's just that not many people use VS.NET 2002 anymore. If you're happy with it or you must use it for some reason then no problem, but given that VB 2005 Express is free and can do most of what most non-professionals need, it makes sense for most people to either get VS 2005 or, if they don't want to pay for that, get the free VB Express. .NET 2.0 and VS 2005 offer many more features over previous versions. The IDE is a bit flaky but SP1 was released in beta recently so should go gold very soon.
 
Back
Top