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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GUID to string

Status
Not open for further replies.

lance59

IS-IT--Management
Mar 6, 2007
50
US
Anyone know how to convert a GUID to string so I can display it in a textbox or use it in a sql query?

I am pulling in a GUID from a db into a listbox. I need to then use that guid to do another query on the db, but need to convert it to a string to use in the query.

I have searched and only can find string to GUID examples.

TIA
 
How are you getting it. some code please. Can't you do a ToString on the object?

Christiaan Baes
Belgium

"My old site" - Me
 
Once that data is pulled from the DB and in the DataGridView I pull it from there using:

Dim Person_ID_Guid = Me.DataGridView1.SelectedCells(3).Value

Now I have to convert Person_ID_Guid to a string to use in a SQL query such as:

Dim SQL2 As String = "select ENC_NBR from Encounter where Person_ID = ' " + Person_ID_String + " '

and to use it in a TextBox:

TextBox4.Text = Person_ID_String

The column property is "person_id(uniqueidentifier,not null)"

TIA
 
Me.DataGridView1.SelectedCells(3).Value.ToString

This should give you the string value.

This however is very wrong.

Dim SQL2 As String = "select ENC_NBR from Encounter where Person_ID = ' " + Person_ID_String + " '

Use the parameters from the commandobject to insert it.

If you show us the code you use to execute the sql then I will show you how to change it to work with parameters.


Christiaan Baes
Belgium

"My old site" - Me
 
Ok, here is the code:

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
'Get the GUID from the DataGridView cell
Dim Person_ID_Guid = Me.DataGridView1.SelectedCells(3).Value
' database connection string
Dim DBConnection2 As String = "Driver={SQL Server};" & "server=GEN2;" & "UID=sa;" & _
"Pwd=password;" & ";database=test"
' sql statment
Dim SQL2 As String = "select ENC_NBR from Encounter where Person_ID = ' " + Person_ID_String + " '"
'create ADODB Connection object
Dim Conn2 As New ADODB.Connection
'create ADODB Recordset object
Dim rs2 As New ADODB.Recordset
'create OleDb Adapter object
Dim daPerson As New OleDbDataAdapter
' finally Dataset to store returned recordset
Dim dsPerson As New DataSet("Person")
'open connection with the string as above
Conn2.Open(DBConnection2, "", "", -1)
'execute the query specifying static sursor, batch optimistic locking
rs2.Open(SQL2, DBConnection2, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockBatchOptimistic, 1)
'use the overloaded version of Fill method which takes recordset as parameter
daPerson.Fill(dsPerson, rs2, "Names")
'Setup DatGridView
SetUpDataGridView()
'bind datagrid to dataset and make visable
DataGridView2.DataSource = dsPerson.Tables("Names").DefaultView
DataGridView2.Visible = True
'close the connection
Conn2.Close()
End Sub
 
A few small points to look at for the future.
Get the code out of the click event and in a separate class in a separate project. There you should gather all the datarelated things.
Trust me you will thank me later.
Set Option Strict On!!!!

You are mixing ADO and ADO.Net. Not a good idea.


Code:
[Blue]Private[/Blue] [Blue]Sub[/Blue] DataGridView1_CellClick([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.Windows.Forms.DataGridViewCellEventArgs) [Blue]Handles[/Blue] DataGridView1.CellClick
            [Green]'Get the GUID from the DataGridView cell[/Green]
            [Blue]Dim[/Blue] Person_ID_Guid [Blue]As[/Blue] Object = [Blue]Me[/Blue].DataGridView1.SelectedCells(3).Value
            [Green]' database connection string[/Green]
            [Blue]Dim[/Blue] DBConnection2 [Blue]As[/Blue] [Blue]String[/Blue] = [Red]"Driver={SQL Server};"[/Red] & [Red]"server=GEN2;"[/Red] & [Red]"UID=sa;"[/Red] & _
            [Red]"Pwd=password;"[/Red] & [Red]";database=test"[/Red]
            [Green]' sql statment[/Green]
            [Blue]Dim[/Blue] SQL2 [Blue]As[/Blue] [Blue]String[/Blue] = [Red]"select ENC_NBR from Encounter where Person_ID = @Person_Id"[/Red]
            [Green]'create ADODB Connection object[/Green]
            [Blue]Dim[/Blue] Conn2 [Blue]As[/Blue] [Blue]New[/Blue] System.Data.OleDb.OleDbConnection
            Conn2.ConnectionString = DBConnection2
            [Green]'create OleDb Adapter object[/Green]
            [Blue]Dim[/Blue] daPerson [Blue]As[/Blue] [Blue]New[/Blue] System.Data.OleDb.OleDbDataAdapter
            [Green]' finally Dataset to store returned recordset[/Green]
            [Blue]Dim[/Blue] dsPerson [Blue]As[/Blue] [Blue]New[/Blue] DataSet([Red]"Person"[/Red])
            [Green]'open connection with the string as above[/Green]
            Conn2.Open()
            [Blue]Dim[/Blue] cmd2 [Blue]As[/Blue] System.Data.OleDb.OleDbCommand
            cmd2.Parameters.Add([Red]"@Person_Id"[/Red], OleDb.OleDbType.Guid)
            cmd2.Parameters(0).Value = Person_ID_Guid
            daPerson.SelectCommand = cmd2
            [Green]'use the overloaded version of Fill method which takes recordset as parameter[/Green]
            daPerson.Fill(dsPerson, [Red]"Names"[/Red])
            [Green]'Setup DatGridView[/Green]
            SetUpDataGridView()
            [Green]'bind datagrid to dataset and make visable[/Green]
            DataGridView2.DataSource = dsPerson.Tables([Red]"Names"[/Red]).DefaultView
            DataGridView2.Visible = [Blue]True[/Blue]
            [Green]'close the connection[/Green]
            Conn2.Close()
        [Blue]End[/Blue] [Blue]Sub[/Blue]

Christiaan Baes
Belgium

"My old site" - Me
 
This line

cmd2.Parameters.Add("@Person_Id", OleDb.OleDbType.Guid)

gives me his error.

Message="Object reference not set to an instance of an object."

FYI - I just finished B5 Season 3 working on Season 4.

Thanks





 
change this

Dim cmd2 As System.Data.OleDb.OleDbCommand

to this

Dim cmd2 As new System.Data.OleDb.OleDbCommand



I haven't watched B5 in years which reminds me I should start again one of these days.



Christiaan Baes
Belgium

"My old site" - Me
 
OK, that fixed that error.

I now get:

Fill: SelectCommand.Connection property has not been initialized.

On this line:

daPerson.Fill(dsPerson, "Names")

But is that not initialized by this line:

Dim daPerson As New System.Data.OleDb.OleDbDataAdapter

TIA
 
Also where does the SQL query (sql2) get sent to the DB to pull the data?
 
you need to add cmd2.connection = conn2

the adaapter will get it from the selectcommand you have given it.

Christiaan Baes
Belgium

"My old site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top