-
Apr 22nd, 2014, 08:59 AM
#1
Thread Starter
New Member
Produce error message if data searched for does not exist in a database
Hi there, i have some code where the user enters the name of a customer and it displays their details. but if they enter a name that doesnt exist, i.e spell it wrong the whole program crashes. is there any way i can get a error message to show of they enter the wrong details, instead of the system crashing and having to restart it??
thanks!
-
Apr 22nd, 2014, 09:02 AM
#2
Thread Starter
New Member
Re: Produce error message if data searched for does not exist in a database
i should add that im using a database with all the customer details stored and being retrieved from there.
-
Apr 22nd, 2014, 09:29 AM
#3
Re: Produce error message if data searched for does not exist in a database
Hello KRJ, I think you should avoid Errors all around. What you want is not an error message but a validation message. The difference is that when you display errors it makes your application look a bit unstable, whereas a validation message points to the user.
Anyway, It is very difficult for us to propose a solution if we do not know how you are accessing the data.
Put some code where you are crashing or where you are getting the input from the user or where you are fetching the data from the database. (or all of them). And remember to put the code between code tags.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 22nd, 2014, 09:45 AM
#4
Re: Produce error message if data searched for does not exist in a database
You can certainly get an error message, but you probably shouldn't. It's always better to check whether an exception will occur rather than trapping them after they occur. This appears to be such a case, but we'd have to see how you are doing the search.
My usual boring signature: Nothing
-
Apr 24th, 2014, 06:21 AM
#5
Thread Starter
New Member
Re: Produce error message if data searched for does not exist in a database
'code that connects to the database
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\TheGarageDatabase.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
'sql code that stores only the values needed for the invoice into a dataset
sql = "SELECT FullName, Registration, BookingType, BookingDate FROM Bookings WHERE Fullname = '" & txtName.Text & "'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "TheGarageDatabase")
'fills the textboxes with the data from the database
txtName.Text = ds.Tables("TheGarageDatabase").Rows(0).Item(0)
txtReg.Text = ds.Tables("TheGarageDatabase").Rows(0).Item(1)
txtType.Text = ds.Tables("TheGarageDatabase").Rows(0).Item(2)
txtDate.Text = ds.Tables("TheGarageDatabase").Rows(0).Item(3)
con.Close()
maxRows = ds.Tables("TheGarageDatabase").Rows.Count
MsgBox(maxRows)
inc = 0
heres my code.
-
Apr 24th, 2014, 07:01 AM
#6
Re: Produce error message if data searched for does not exist in a database
OK... so before you do anything with the data, see if there is data... IF there aren't anyt rows THEN notify the user that there was no data, ELSE use the data. (END IF)
-tg
-
Apr 24th, 2014, 07:06 AM
#7
Thread Starter
New Member
Re: Produce error message if data searched for does not exist in a database
Ohh i see, so would i use sql code to do that or use VB code, im new to the sql code so its confusing me slightly.
Thanks for your help though!
-
Apr 24th, 2014, 07:55 AM
#8
Frenzied Member
Re: Produce error message if data searched for does not exist in a database
something like
Code:
IF maxRows = 0 THEN
MessageBox.Show("There are no records that match those criteria.")
else
txtName.Text = ds.Tables("TheGarageDatabase").Rows(0).Item(0)
txtReg.Text = ds.Tables("TheGarageDatabase").Rows(0).Item(1)
txtType.Text = ds.Tables("TheGarageDatabase").Rows(0).Item(2)
txtDate.Text = ds.Tables("TheGarageDatabase").Rows(0).Item(3)
END IF
Last edited by CoachBarker; Apr 25th, 2014 at 03:32 PM.
Reason: correct end if
-
Apr 25th, 2014, 11:59 AM
#9
Frenzied Member
Re: Produce error message if data searched for does not exist in a database
CoachBarker is spot on except for the typo with the "End If". If there are no rows returned from the query then it will crash when you try and reference "Rows(0)".
Another potential problem is that you are using inline SQL with single quotation marks. You are going to run into problems with names that contain a single quotation mark like "O'Brien". You must escape the single quotation marks when building your SQL statement.
Pass the username and password each to this function:
Code:
Public Function EscapeQuotes(ByVal str As String) As String
Return str.Replace("'", "''")
End Function
I'd also remove any trailing spaces with the Trim function or you risk returning zero rows when there are in fact matching rows.
-
Apr 25th, 2014, 03:32 PM
#10
Frenzied Member
Re: Produce error message if data searched for does not exist in a database
just typed it off the top of my head.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|