Click here to Skip to main content
15,947,958 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, every one !

I would like to ask about about an article that mister Heath Stewart @heaths posted "Role-based Security with Forms Authentication"
I followed the exact same steps he mentioned. but when I try to log in it shows this error "Unable to cast an object of type 'System.Int32' to type 'System.String'." at this line
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtLogin.Text, DateTime.Now, DateTime.Now.AddMinutes(30), true, reader.GetString(0), FormsAuthentication.FormsCookiePath); 

I really don't know why :( :(
Please help, I need to get this done by this afternoon :'(
E.F. Nijboer 20-Jun-14 7:35am    
You should go to the article and ask on the bottom of the page. You can post remarks on the article page which will notify the writer of the article.
CHill60 20-Jun-14 8:14am    
When you debug, what is the value of reader.GetString(0)? Is it null by any chance?
Member 10889990 20-Jun-14 9:44am    
it doesnt say any reader.GetStrin{0} but it's showing me that ticket value is null !!
Member 10889990 20-Jun-14 12:01pm    
I did a and it gave me that error, so I replaced it with reader.GetValue(0).toString() and it showed me the RoleId. But when I replaced it in my formsAuthenticationTicket nothing happens, no error, no redirection to the default page

1 solution

Just adding this as the solution for future visitors:

I'm gonna guess the GetString() method is throwing the exception. Take a look at the documentation for this method on MSDN. The data you are reading must already be a string. The datatype of the column at index "0" in your case is likely of the int datatype.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900