Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question on GUID (Globaly Unique Id's)

Status
Not open for further replies.

soldierB

Technical User
Dec 11, 2005
37
GB
Hi Guys,

Just covered a few chapters in my Wrox book on ADO.net.

Its all going just swell .........but Im not following one section to well.

Im sending Parimeter & Non Parimeter Queries to my database...

One of the database fields has an ID column and has a GUID as its value, in a lesson I covered I made a small application that generates a new GUID for me and when I need to add a record to the database I can paste it in.....No probs all works fine.

But in the code from the lesson the one section im not quite following is as follows:

In the "objCommand.Parameters.Add" im passing the parimeter "@ProjectID" and its of type "OleDbType.Guid" to a maximum size of 16--- Is that right so far?
then

New Guid(ComboBox1.SelectedValue.ToString), what the "New Guid" is im not sure?

Any help is appreciated.
Thanks again

Code:
 'Add the required parameter for the query
        objCommand.Parameters.Add("@ProjectID", OleDbType.Guid, 16).Value = _
            New Guid(ComboBox1.SelectedValue.ToString)
 
Sorry, but I would leave the Guid creation to the database and not the application if it was mssql.

now your problem

new Guid means that is creating a new object from the class Guid probably the one you made earlier in your little application.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Thanks Chrissie,

At the moment the lesson is usung an access database and will be switching to SQL soon.

Im still not following a little, if you can bear with me...

When I made the GUID in my little app it was very simple I had a text box and a button, whe the button is clicked the textbox is filled with the GUID as is the clipboard.

Code:
txtGuid.Text = Guid.NewGuid.ToString.ToUpper()

Private Sub txtGuid_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtGuid.TextChanged
        '---Copy theGUID to the clipboard
        Clipboard.SetText(txtGuid.Text)

    End Sub

So there I see I have accessed the GUID class and its NewGuid method, and for some reason put it to uppercase..

Im following that....

Its the next lesson as I mentioned in my first post that then creats a small app again that searches the database by using the GUID as my search parimeter.

But why when adding the parimeter to the objCommand am I saying thet the value held by the textbox is a NEW GUID?

As i see it, when the record was added to the database I generated a GUID for the record and added it, now im searching for the record using its GUID as a search parimeter...so why am I now saying its a NEW GUID again?

Thanks again.

Below is the code for the whole app im working on and its all fine, Im just not getting my head round this GUID section.....

Code:
Imports System.Data
Imports System.Data.OleDb

Public Class Form1
    '---Form level variables
    Private Const dbPath As String = "D:\ProjectTimeTracker\ProjectTimeTracker.mdb;"
    Private strConnectionString As String = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source =" & dbPath & ";"


    Private objConnection As OleDbConnection
    Private objCommand As OleDbCommand
    Private objDataAdaptor As OleDbDataAdapter
    Private objDataSet As DataSet

    Private Sub PopulateGrid()
        '--- Initialise a new instance of the OleDBDataAdaptor class
        objDataAdaptor = New OleDbDataAdapter
        '---Initialise a new instance of the DataSet class
        objDataSet = New DataSet
        '--- Set the SelectCommand for the OleDbDataAdaptor
        objDataAdaptor.SelectCommand = objCommand

        Try
            '---Populate the DataSet
            objDataAdaptor.Fill(objDataSet, "Projects")

            '---Bind the dataset to the datagrid
            grdResults.DataSource = objDataSet
            grdResults.DataMember = "Projects"

            '---Set the AlternatingRowsDefaultCellStyle.BackColor property
            grdResults.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke

            '---Set the CellBorderStyle property
            grdResults.CellBorderStyle = DataGridViewCellBorderStyle.None

            '---Set the SelctionMode property
            grdResults.SelectionMode = DataGridViewSelectionMode.FullRowSelect

            '--- Set the AutoSizeColumnsMode property
            grdResults.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

            '---Right align SequenceNumber Column
            grdResults.Columns("SequenceNumber").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight


        Catch OleDbException As OleDbException
            MessageBox.Show(OleDbException.Message, "Access Queries")
        End Try

        '---CleanUp
        objCommand.Dispose()
        objCommand = Nothing
        objDataAdaptor.Dispose()
        objDataAdaptor = Nothing
        objDataSet.Dispose()
        objDataSet = Nothing
        objConnection.Dispose()
        objConnection = Nothing
    End Sub

    Private Sub btnNonParimeterQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNonParimeterQuery.Click
        '---Initialise a new instance of the OleDbConnection class
        objConnection = New OleDbConnection(strConnectionString)

        '---Initialise a new instance of the OleDbCommand class
        objCommand = New OleDbCommand

        '---Set the objCommand object properties
        objCommand.CommandText = "usp_SelectProjects"
        objCommand.CommandType = CommandType.StoredProcedure
        objCommand.Connection = objConnection

        '---Populate the DataGridView
        Call PopulateGrid()

    End Sub

    Private Sub btnParimeterQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnParimeterQuery.Click
        '---Initialise a new instance of the OleDbConnection class
        objConnection = New OleDbConnection(strConnectionString)
        '---Initialise a new instance of the OleDbCommand class
        objCommand = New OleDbCommand
        '---Set the objCommand object properties
        objCommand.CommandText = "usp_SelectProject"
        objCommand.CommandType = CommandType.StoredProcedure
        objCommand.Connection = objConnection

        '--- Add the required parimeter for the query
        objCommand.Parameters.Add("@ProjectID", OleDbType.Guid, 16).Value = _
             New Guid(txtProjectID.Text)

        '---Populate the DataGridView
        Call PopulateGrid()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
 
Apparently (but to be sure I need the code for the Guid) the txtprojectid.text contains a value needed to make the Guid that will correspond with the one you had in the database and making a new object guid with that as a parameter will create a Guid that will work as a parameter.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top