Randomly generating number take 2

Posted on 2003-12-10
Last Modified: 2010-05-01
Hi i have managed to generate a three digit number randomly and populate it to a textbox. My application connects  to a DB and populates a listview according to whats in the database. What i need to do now is to make sure that the randomly generated number doesnt already exist in the database. So basically i have text fields that let users enter clientID, firstname, lastname etc, from text boxes and upon pressing the save button, saves it to the database. So when  i press the add button, my ClientID text field populates with a randomly generated 3 digit number, i just need to make sure it doesnt generate a number or client ID, already in the database....
Question by:intox1221
  • 5
  • 3
LVL 27

Expert Comment

ID: 9915274
Hi intox1221:
Sounds like an easy one to me.
To be able to answer, though, you should give more information about your database:
MSAccess? SQL?
Is the clientID field indexed? Is it the PrimaryKey?


Author Comment

ID: 9915528
it is the primary key. It is indexed to yes (no duplicates).

These r my connection details. this is the top level class variable in the form.
Dim cstrConnection As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" _
        & System.AppDomain.CurrentDomain.BaseDirectory() & "MailingList.mdb"

I have  Dbtier class set up which handles all the connections. A part of the code is as follows.

    Public Shared Function ReturnData(ByVal pstrConnection As String, ByVal pstrSQL As String) As DataSet
        ' This procedure returns a dataset containing
        ' all records from the passed-in query. The
        ' connection string is needed to connect to
        ' the database.

        'declare the DB connection, data adapter, and data set objects
        Dim conConnection As OleDbConnection
        Dim daAdapter As OleDbDataAdapter
        Dim dsData As DataSet


            'open the connection
            conConnection = New OleDbConnection(pstrConnection)

            'create the data adapter (this manages the data) based on the query
            daAdapter = New OleDbDataAdapter(pstrSQL, conConnection)

            'create the data set and fill it from the data adapter
            dsData = New DataSet

            'close the connection
LVL 27

Expert Comment

ID: 9916014
It does help to know that you are using .NET and an .mdb file

What you need to use is the "Find" function

Just type dsData.Tables(0).Find and press F1 for help.
If the help is not clear to you, then please let me know, and I will guide you through it.

LVL 27

Expert Comment

ID: 9916022
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


Author Comment

ID: 9916030
i will take a look at help like u suggest, it always helps. But i would rather u guide me where i need to go... thanks for ur time though....
LVL 27

Expert Comment

ID: 9916118
Something like this:

      Dim dt As DataTable = dsData.Tables(0)
      Dim dc(0) As DataColumn
      dc(0) = dt.Columns("clientId")
      dt.PrimaryKey = dc
      Dim keys(0) As String
      keys(0) = txtClientID.Text

Author Comment

ID: 9916239
what do i declare dsdata as and where. Also does it have to be local or class level variable.
LVL 27

Accepted Solution

Dabas earned 500 total points
ID: 9916292
I just took the variable as appears in your code!

Insert my code after the daAdapter.Fill(dsData) line

LVL 27

Expert Comment

ID: 10270351
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
No response from intox1221 from 12/10/2003 comment
Award points to Dabas is recommend.
Please leave any comments here within the next seven days.

EE Cleanup Volunteer

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now