I have a method that execute Store proc and retun a record that I need to access to its value in my C# code. I am using microsoft Enterprise Library fr accessing to database. This is my Method:
public Model.Customer GetCustomerInfo(string UserName, string Password)
{
Model.Customer model = null;
string myConnection = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ToString();
SqlDatabase db = new SqlDatabase(myConnection);
using (DbCommand command = db.GetStoredProcCommand("get_Customer"))
{
db.AddInParameter(command, "UserName", DbType.String, UserName);
db.AddInParameter(command, "Password", DbType.String, Password);
var result = db.ExecuteReader(command);
try
{
if (result.FieldCount == 0)
model = null;
else
{
result.Read();
model = new Model.Customer() {
Account_Number =Convert.ToInt32(result.GetString(0))
First_Name = result.GetString(1),
Middle_Name = result.GetString(2),
Last_Name = result.GetString(3),
Password = result.GetString(4),
isRegistered =Convert.ToBoolean(result.GetString(5)),
};
}
}
catch (Exception ex)
{
}
return model;
}
{System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.
This is my customer class:
[DataContract]
public class Customer
{
[DataMember]
public int Account_Number { get; set; }
[DataMember]
public string First_Name { get; set; }
[DataMember]
public string Middle_Name { get; set; }
[DataMember]
public string Last_Name { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public bool isRegistered { get; set; }
[DataMember]
public bool isActivated { get; set; }
[DataMember]
public bool isActive { get; set; }
[DataMember]
public bool isLocked { get; set; }
[DataMember]
public int Account_Payment_Status { get; set; }
[DataMember]
public int unsuccessful_login_count { get; set; }
[DataMember]
public string Email { get; set; }
}
and this is my store proc:
alter PROCEDURE [dbo].[get_Customer_P_in]
@UserName varchar(50)
,@Password varchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT
[Account_Number]
,[First_Name]
,[Middle_Name]
,[Last_Name]
,[Password]
,[isRegistered]
FROM [CustomerPortal].[dbo].[Customer] cc WITH (NOLOCK)
left join [CustomerPortal].[dbo].Customer_Email ce WITH (NOLOCK) on cc.Customer_ID = ce.Customer_ID
WHERE (CAST(cc.Account_Number AS varchar(50))= @UserName or ce.Email= @UserName) and cc.Password=@Password
END
Member
137 Points
441 Posts
Unable to cast object of type 'System.Int32' to type 'System.String'.
Dec 17, 2014 05:04 PM|nikoo56|LINK
I have a method that execute Store proc and retun a record that I need to access to its value in my C# code. I am using microsoft Enterprise Library fr accessing to database. This is my Method:
I am getting error right at this line:
{System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.
This is my customer class:
and this is my store proc:
All-Star
194009 Points
28028 Posts
Moderator
Re: Unable to cast object of type 'System.Int32' to type 'System.String'.
Dec 17, 2014 05:09 PM|Mikesdotnetting|LINK
It looks like the Account_Number field in the database is an int, so you need this:
Member
137 Points
441 Posts
Re: Unable to cast object of type 'System.Int32' to type 'System.String'.
Dec 17, 2014 05:29 PM|nikoo56|LINK
@Mikesdotnetting Thank you so much, I havn't noticed that.