Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 4, 2005 2:08 PM
I have spent 3 days on this and it is probably a very easy fix.
I am trying to retreive the userID which is a GUID during the login.
Does anyone have any ideas to make this work. I found a work around by adding a recordId as an integer, but this is a lousy fix.
If the recored is found there is no error. If not found I can not get around this error.
Here is my code that executes the stored procedure:
public static Guid GetProfileGuidLogin(string sLoginName, string sPassword)
{
string sSPName = "uspDoProfilesGuidTestLogin";
SqlConnection oConn = new SqlConnection((string)ConfigurationManager.AppSettings["ConnStr"]);
oConn.Open();
SqlCommand oCmd = new SqlCommand(sSPName, oConn);
oCmd.CommandType = CommandType.StoredProcedure;
oCmd.Parameters.AddWithValue("@loginId", sLoginName);
oCmd.Parameters.AddWithValue("@password", sPassword);
Guid oID = (Guid)(oCmd.ExecuteScalar());
if (oID.ToString() != "")
{
return oID;
}
else
{
return System.Guid.Empty;
}
}
Here is the error I keep getting if the data is not there.
I have tried every combination but the right one to avoid this error.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
|
Source File: D:\Inetpub\UltraSunSouthwest\App_Code\UltraSunData.cs ** Line:** 304
Stack Trace:
|
All replies (8)
Friday, August 5, 2005 6:31 AM âś…Answered
If the record is not found, then the object ExecuteScalar() returns will be of type System.DbNull.Value, which cannot be casted to a Guid, hence the casting error. Try something like this:
object oID = oCmd.ExecuteScalar();
if (oID is Guid)
{
return (Guid)oID;
}
else
{
return Guid.Empty;
}
Thursday, August 4, 2005 7:47 PM
The Sql type, I believe is called 'uniqueidentifier', which is in fact a GUID. I think you should take a look at what Books Online says about this:
uniqueidentifier
A globally unique identifier (GUID).
Remarks
A column or local variable of uniqueidentifier data type can be initialized to a value in two ways:
- Using the NEWID function.
- Converting from a string constant in the following form (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, in which each x is a hexadecimal digit in the range 0-9 or a-f). For example, 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid uniqueidentifier value.
Comparison operators can be used with uniqueidentifier values. However, ordering is not implemented by comparing the bit patterns of the two values. The only operations that are allowed against a uniqueidentifier value are comparisons (=, <>, <, >, <=, >=) and checking for NULL (IS NULL and IS NOT NULL). No other arithmetic operators are allowed. All column constraints and properties except IDENTITY are allowed on the uniqueidentifier data type.
Try treating your guid as a string and see whay happens.
Thursday, August 4, 2005 7:58 PM
SQL Server GUID is a 16 bytes binary datatype so you may need a convert instead. Run search for convert in SQL Server BOL(books online). Hope this helps.
Tuesday, October 18, 2005 4:11 PM
I was searching for a similar problem and ran across this ... the "object" fix worked wonders ... Just wanted to say thanks. You're a genious :)
Wednesday, October 19, 2005 7:23 AM
I'd look at the first overload of System.Guid's constructor. It takes (lo and behold) an array of 16 bytes!
Monday, November 2, 2009 1:49 AM
Please do the following steps,
1. Assign the ExecuteScalar() return value to a object variable
** Object** objValue = oCmd.ExecuteScalar();
2. Check the Object varaible for null
return objValue== null ? Guid.Empty : new Guid(objValue.ToString());
Hope this will solve the problem
Monday, November 2, 2009 2:01 AM
What I usualy go with this situation is. I use ExecureReader, then if rocord found, check
reader is not null and use .Read() to get the Guid of specified user.
Hope u understood.
REegards.
Wednesday, September 9, 2015 7:59 PM
I ran into this kind of message when my "GUID" was not actually saved in a "uniqueidentifier" column in the database. It was in a char(35) column, per the DBA's magnificent thought process. So I got this error when casting directly to a GUID. I had to use it as if it were a string (since it was saved as a string in the DB), instead.