Answered by:
GUID Problem in C#...

Question
-
helloo,
i am having sum problem in the function below...this function is use to get UID from UserName in which i m firng select query to get UID from DB........
But SqlReader is not able retireve the Value of tht...it is giving sum Type casting error...
The Field UID DataType is GUID ...
public Guid GetUIDFromUserName(string p_UserName){
guid RetInt = new guid(); SqlConnection SqlConn = new SqlConnection(); SqlCommand SqlCmd = new SqlCommand(); SqlDataReader SqlRead; Guid RetInt = new Guid();SqlConn.ConnectionString = m_DM.GetConnectionString(
"PIMS_DB");SqlConn.Open();
SqlCmd.CommandText =
"Select UID from UserTran1 where UserName Like " + m_DM.MakeQryFieldStr(p_UserName);SqlCmd.Connection = SqlConn;
SqlRead = SqlCmd.ExecuteReader();
if (SqlRead.HasRows == true){
SqlRead.Read();
RetInt = (
Guid)SqlRead.GetGuid(0); //// Error generates Here.....}
return RetInt;}
It generates Sum Error
'Specified cast is not valid.'
Kindly help me.. Plz..........................Monday, July 28, 2008 8:03 PM
Answers
-
It is not your cast that is failing, you don't need a cast because GetGuid already returns a Guid. It is a cast inside of GetGuid() that fails. Perhaps because it is casting a DBNull to a Guid. Use something like this to see what is going on:
object value = SqlRead[0];
And inspect "value" in the debugger.
Hans Passant.- Marked as answer by nobugz Sunday, August 10, 2008 1:05 AM
Monday, July 28, 2008 8:18 PM
All replies
-
If I'm not mistaken, GetGuid() already returns a Guid. you dont have to explicity typecast the RHS.
anyways I think you are getting an error because the value returned is null (DbNull i.e.).
check for that and then do the assignment. Might resolve your issue.
- Proposed as answer by Praveen Rangarajan Monday, July 28, 2008 8:15 PM
Monday, July 28, 2008 8:15 PM -
It is not your cast that is failing, you don't need a cast because GetGuid already returns a Guid. It is a cast inside of GetGuid() that fails. Perhaps because it is casting a DBNull to a Guid. Use something like this to see what is going on:
object value = SqlRead[0];
And inspect "value" in the debugger.
Hans Passant.- Marked as answer by nobugz Sunday, August 10, 2008 1:05 AM
Monday, July 28, 2008 8:18 PM -
Hey Thanxxsss
Mr.Praveeen & Mr. Hans......
U made my Programming Night.....
Regards
TusharMonday, July 28, 2008 8:29 PM