• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Randomly generating number take 2

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....
0
intox1221
Asked:
intox1221
  • 5
  • 3
1 Solution
 
DabasCommented:
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?

Dabas
0
 
intox1221Author Commented:
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

        Try

            '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
            daAdapter.Fill(dsData)

            'close the connection
            conConnection.Close()
0
 
DabasCommented:
intox1221:
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.

Dabas
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DabasCommented:
0
 
intox1221Author Commented:
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....
0
 
DabasCommented:
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
      dt.Rows.Find(keys)
0
 
intox1221Author Commented:
what do i declare dsdata as and where. Also does it have to be local or class level variable.
0
 
DabasCommented:
intox1221:
I just took the variable as appears in your code!

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

Dabas
0
 
planoczCommented:
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

planocz
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now