Re: Retrieving a Guid from a DataTable

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hans, the column has a Uniqueidentifier data type. However, my table
does not accept nulls - so there are no null values.

Tom, the GUID is stored as a Uniqueidentifier in the database. But in
the application row["ID"].GetType() returns System.Guid

Ignacio, as specified above, the table has a column of uniqueidentifier
data type. I have a method with the following:

ArrayList ReadingRange = new ArrayList();
try
{
DataTable tblReadings = Access.GetAllReadings();// populates the
tblReadings dataTable
if (tblReadings == null)
{
MessageBox.Show("No data found!!!");
}
else
{


foreach(DataRow row in tblReadings.Rows)
{

//Reading class with parameter of type System.Guid
Reading reading = new Reading(
(Guid)row["ID"],
);
ReadingRange.Add(Reading);
}
return ReadingRange;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return ReadingRange;

/// ******* I am still getting the same error message!!!



AlveenX wrote:
Hi,
I am trying to pick a Guid from a data row using the following code:

foreach(DataRow row in MyDataTable.Rows)
{

(Guid)row["ID"]

}

However, this is the error I'm getting: System.InvalidCastException:
Specified Cast is not Valid. Do you know how I can cast the row entry
so that I end up retrieving the Guid?

Thanks,
Alveen

.



Relevant Pages

  • Re: Primary Key - string or integer
    ... but UNIQUEIDENTIFIER is basically a ... data you have upper and lowercase characters to contend with that you ... won't with a UNIQUEIDENTIFIER data type. ... If you have the need for a GUID, UNIQUEIDENTIFIER is the data type you ...
    (microsoft.public.sqlserver.server)
  • Re: SCOPE_INDENTITY() Not Returning
    ... @addr_id uniqueidentifier OUTPUT ... and NEWID(). ... NEWIDon the other hand generates a binary GUID value of data type ...
    (comp.databases.ms-sqlserver)
  • How do I define an XML schema to support a GUID primary key?
    ... I would like to model an XML schema to match a sql table that uses the GUID ... "uniqueidentifier" data type. ... There is no similar data type on the XML side. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Data Types
    ... >> Steve, ... >> Tibor Karaszi, SQL Server MVP ... >>> character varchar data type? ... A uniqueidentifier uses 16 bytes. ...
    (microsoft.public.sqlserver.programming)
  • Re: Data Types
    ... > moved to SQL Server and I don't know the best newsgroups yet. ... > Does a Unique Identifier data type take up less storage space than a 32 ... A uniqueidentifier uses 16 bytes. ... but I'm looking at the data types and think that the TimeStamp ...
    (microsoft.public.sqlserver.programming)