Results 1 to 10 of 10

Thread: Produce error message if data searched for does not exist in a database

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    8

    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!

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    8

    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.

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    8

    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.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    8

    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!

  8. #8
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    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
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  9. #9
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,373

    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.

  10. #10
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: Produce error message if data searched for does not exist in a database

    just typed it off the top of my head.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width