Parameter Query - Error handling?

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
In my current queries I am running I have two parameters - @CustomerID and Status=2

The form runs fine and as long as a customer has any rows with Status of 2 then they show, but when selecting a customer from the list that doesn't have any rows with status of 2, I get error;

An unhandled exception of type 'System.FormatException' occurred in system.data.dll
Additional information: Input string was not in a correct format.

I'm assuming this is because it can't find any rows for that Customer ID.

On my query for finding RowID, I have error handling that if no RowID is entered, the user is given a "Enter ID" message, and then if the user enters an ID that doesn't exist, that also gives an error message, using

If Ds1.Table1.rows.count = 0 Then
...
...

Is there a way of putting this into my other query? I think it's falling over as there are two parameters, but I don't know where to put the error handling code.

My code for the search (using parameter @CustomerID, status=2 is hard built into dataAdapter) - user clicks a button, clears the dataTables and then repopulates them with the new rows.

VB.NET:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

Ds1.table3.Clear() 

Ds1.table2.Clear()

Ds1.table1.Clear()

ds2.table2.Clear()

da1.SelectCommand.Parameters("@CustomerID").Value = txtCustomerID.Text

da1.Fill(Ds1)

FillChild()

End Sub

Private Sub cboCustomer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCustomer.SelectedIndexChanged

txtCustomerID.Text = cboCustomer.SelectedValue

End Sub

Private Sub FillChild()

da2.SelectCommand.Parameters("@RowID").Value = lblID.Text

da2.Fill(Ds1)

da3.SelectCommand.Parameters("@RowID").Value = lblID.Text

da3.Fill(Ds1)

End Sub
 
that would help wouldn't it!!!

Here's the code - the daDWR also has a parameter of Status = 2, but I've hardcoded this when creating the select query (WHERE CustomerID = @CustomerID AND Status = 2)

VB.NET:
'SqlSelectCommand2 
'
 
Me.SqlSelectCommand2.CommandText = "SELECT DWRNumber, CustomerID, CustomerContact, EndCustomer, DateRequested, DateRe" & _
 
"quiredBy, NPDContact, AccountManager, DWRObjective, FurtherInfo, Documentation, " & _
 
"CreatedBy, CreatedDate, StatusID, DateCompleted, SecondaryAccountManager, Commen" & _
 
"ts FROM DWR WHERE (CustomerID = @CustomerID) AND (StatusID = 2)"
 
Me.SqlSelectCommand2.Connection = Me.conDWR
 
Me.SqlSelectCommand2.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.Int, 4, "CustomerID"))

Thanks
Luke
 
da1.SelectCommand.Parameters("@CustomerID").Value = txtCustomerID.Text
da2.SelectCommand.Parameters("@RowID").Value = lblID.Text

Are the id fields strings or are they integers.
If they are integers, I think you would want to assign integer values to the parameter values.
Convert the .text values to integers and assign to a variable -

intVariable1 = Convert.ToInt32( txtCustomerID.Text)
da1.SelectCommand.Parameters("@CustomerID").Value = intVariable1

Use BindingContext to verify if any records are returned from the query.

Dim c As Integer = Me.BindingContext(DsDWR1, "DWR Name").Count
If c > 0 Then

else

end if

Hope this helps,
 
Back
Top