We help IT Professionals succeed at work.

Failed to convert parameter value from a String to a Guid

barnesco
barnesco asked
on
4,767 Views
Last Modified: 2012-05-05
I'm reading from a query string gid:

   http://localhost/Dot61/Default.aspx?tabid=58&gid=356a634d-6233-4555-a3fa-726d66a20f4a

And in the code, I do this with:

   Dim GID As String
   GID = Request.QueryString("gid")

When I do, I'm receiving the message:

   Failed to convert parameter value from a String to a Guid

All of my classes have the GID as string and the SQL query accepts a varchar, which works. What am I doing wrong?

Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
are you 100% sure the error occurs at that line?

Commented:
Could you post some more code ?
I think that the line
GID = Request.QueryString("gid")
is not where the problem lies ;)

Author

Commented:
Error code is posted below:
DotNetNuke.Services.Exceptions.ModuleLoadException: Failed to convert parameter value from a String to a Guid. 
---> System.InvalidCastException: Failed to convert parameter value from a String to a Guid. 
---> System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'. 
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) 
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider) 
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) 
at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) 
--- End of inner exception stack trace --- 
at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) 
at System.Data.SqlClient.SqlParameter.GetCoercedValue() 
at System.Data.SqlClient.SqlParameter.Validate(Int32 index) 
at System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters) 
at System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc) 
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) 
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) 
at Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, String commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership) 
at Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters) 
at Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(String connectionString, String spName, Object[] parameterValues) 
at KScoop.Modules.BClub.SqlDataProvider.GetBClubUser(Int32 PortalId, String GID) 
in c:\inetpub\wwwroot\Dot61\App_Code\BClub\SqlDataProvider.vb:line 130 
at KScoop.Modules.BClub.BClubController.GetBClubUser(Int32 PortalId, String GID) 
in c:\inetpub\wwwroot\Dot61\App_Code\BClub\BClubController.vb:line 61 
at KScoop.Modules.BClub.ViewBClub.Page_Load(Object sender, EventArgs e) 
in c:\inetpub\wwwroot\Dot61\DesktopModules\BClub\ViewBClub.ascx.vb:line 86 
--- End of inner exception stack trace ---

Open in new window

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
could you please post the codes around the following 3 lines:

at KScoop.Modules.BClub.SqlDataProvider.GetBClubUser(Int32 PortalId, String GID)
in c:\inetpub\wwwroot\Dot61\App_Code\BClub\SqlDataProvider.vb:line 130

at KScoop.Modules.BClub.BClubController.GetBClubUser(Int32 PortalId, String GID)
in c:\inetpub\wwwroot\Dot61\App_Code\BClub\BClubController.vb:line 61

at KScoop.Modules.BClub.ViewBClub.Page_Load(Object sender, EventArgs e)
in c:\inetpub\wwwroot\Dot61\DesktopModules\BClub\ViewBClub.ascx.vb:line 86
DBAduck - Ben MillerSQL Server Architect
CERTIFIED EXPERT

Commented:
If you look at the stack trace it is telling you that you are trying to use a string in a parameter that  accepts a guid and it is not in the right format.

So when you do GID = Request.QueryString("gid") the result is not formatted in a GUID format.  So you would need to catch that exception and find out what the value is in debug mode to ensure that it is indeed in the right format.  
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
to all that see GID -> GUID parameter type: I see no parameter type GUID anywhere so far...
DBAduck - Ben MillerSQL Server Architect
CERTIFIED EXPERT

Commented:
The part of the stack trace
---> System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)

Shows that there was a string given to the parameter and the type in SQL was a GUID.  That is where I was commenting on.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>Shows that there was a string given to the parameter and the type in SQL was a GUID.  That is where I was commenting on.
yes, you are right on that...

let me assume that the stored procedure you are calling (ie setting the parameter) is of type uniqueidentifier, and not of type varchar...

Author

Commented:
dbaduck, you're right in that the stack trace says I'm converting a string to a guid, but I don't see it where.

angellll, my parameter in the sproc is a varchar(100) and I made sure the table design was that way too. The query works. Attached are the lines of code (and a few more) that show where the parameter is being passed.

araim, I originally attempted to convert from a string to a guid, but couldn't do it they way I had coded it--I kept getting conversion errors. But your way works, and consequently, I'm able to pass the guidValue.

Thank you, everyone, for your enthusiasm. I really do appreciate it. Araim hit on the money.
BClub\SqlDataProvider.vb:line 130:
 
Public Overrides Function GetBClubUser(ByVal PortalId As Integer, ByVal GID As String) As IDataReader
	Return CType(SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GetBClubUser"), PortalId, GID), IDataReader)
End Function
 
 
BClubController.vb:line 61:
 
Public Function GetBClubUser(ByVal PortalId As Integer, ByVal GID As String) As List(Of BClubUserInfo)
	Return CBO.FillCollection(Of BClubUserInfo)(DataProvider.Instance().GetBClubUser(PortalId, GID))
End Function
 
 
ViewBClub.ascx.vb:line 86:
 
colBClubUser = objBClubUser.GetBClubUser(PortalId, GID)

Open in new window

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.