Retrieving a Guid from a DataTable

  • Thread starter Thread starter AlveenX
  • Start date Start date
A

AlveenX

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
 
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

Is it really a guid (I think SqlServer will call it
"uniqueidentifier")?
When you get these casting errors for a column that *should* have the
correct type, the value is probably a System.DbNull.Value (to represent
a "null" in the database).
You can check for this with row.IsNull("ID")

Hans Kesting
 
AlveenX said:
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

Hi Alveen,

Is the GUID stored as a string? If so, you can't just cast away to a 'Guid'
struct.

Try this:
Guid g = new Guid( row["ID"] );

If your GUID is of the correct format, then the above code should generate a
valid Guid struct, in g.
 
Hi,

Without seeing the table struct or more code it's imposible to know the
error.

Do this, Assign it to an object instance then in the watch you can see the
type of the column.
Depending of it you could call one of the Convert method or do a cast
 
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 said:
Hans, the column has a Uniqueidentifier data type. However, my table
does not accept nulls - so there are no null values.

There can still be nulls in your DataTable.
If you stop in the debugger, what do you see in that column?
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!!!


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
 
AlveenX said:
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


Alveen,

could it be you need to cast the row["ID'"] to a string, then work w/
it to create a Guid.

perhaps,

Guid g = new Guid ( (row["ID"].ToString() ) );

OM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top