Search code examples
asp.netsqlsql-server.net-4.0sqlmembershipprovider

Invalid Cast Exception calling SqlMembershipProvider GetUser


I am implementing authorisation on a web application using the ASP.NET SqlMembershipProvider, but it is randomly throwing an invalid cast exception when calling the GetUser method.

I can log in, and navigate the pages all fine, but than at random points, boom, it craps out, and throws the exception:

Unable to cast object of type 'System.Int32' to type 'System.String'

Looking at the stack trace you can clearly see it is being caused by the Membership Provider:

[InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.]
   System.Data.SqlClient.SqlBuffer.get_String() +5002910
   System.Data.SqlClient.SqlDataReader.GetString(Int32 i) +55
   System.Web.Security.SqlMembershipProvider.GetUser(String username, Boolean userIsOnline) +1169
   System.Web.Security.Membership.GetUser(String username, Boolean userIsOnline) +63
   System.Web.Security.Membership.GetUser() +19
   _Default.LoadLeadOverview() in \\file1\default.aspx.vb:169
   _Default.Page_Load(Object sender, EventArgs e) in \\file1\default.aspx.vb:30
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

I have had a look around and found other people with the same issue, but no solution. Any thoughts would be welcome.


Solution

  • Whilst I haven't done anything to specifically resolve this issue, I haven't seen it in a couple of weeks. Looking back, I was testing the site in multiple browsers at the same time, with the same account, logging in and out a lot, and that seemed to be when then problem came up. Now all the CSS and other cross-browser stuff has been settled, I'm not using multi browsers so heavily, and not at the same time and it seems to be fine.