locked
Failed to convert parameter value from a SqlParameter to a Int64. RRS feed

  • Question

  • User-543487465 posted

    Hi all,

    I am new to application block. 

    I am trying to insert data in database. Following is the code snap.

    
    
    Aspx.cs file : 
    protected void cmdCreate_Click(object sender, EventArgs e)
    {
    AccountBalance account_balance = new AccountBalance();
    account_balance.AccountNo = Convert.ToInt64(txtAcntNo.Text.Trim());
    account_balance.BalDate = Convert.ToDateTime(txtDate.Text.Trim().ToString());
    account_balance.Balance = Convert.ToDouble(txtBalance.Text.Trim().ToString());
    account_balance.create();
            }

    Account Balance Class:

    public  class AccountBalance : IAccountBal
    {
    #region "Private Properties"
    private Int64 _account_no;
    private DateTime _balance_date;
    private Double _balance;
    #endregion
    #region "Default Constructor"
    public AccountBalance()
    { }
    #endregion

    public AccountBalance(Int64 AccountNo)
    {
    _account_no = AccountNo;
    }

    #region "Public Properties"
    public Int64 AccountNo
    {
    get { return _account_no; }
    set { _account_no = value; }
    }
    public DateTime BalDate
    {
    get { return _balance_date; }
    set { _balance_date = value; }
    }

    public Double Balance
    {
    get { return _balance; }
    set { _balance = value; }
    }
    #endregion

    #region "Methods"
    public DataSet RetrieveList()
    {
    return AccountDac.RetriewList(this);
    }
    public Int32 create()
    {
    return TransactionDac.createNewBalance(this);

    }
    
    
    DAC :
    public  class TransactionDac : Configuration
    {
    public static Int32 createNewBalance(IAccountBal newAccount)
    {
    SqlParameter[] _parameters ={
    new SqlParameter("@_account_no",newAccount.AccountNo),
    new SqlParameter("@_balance_date",newAccount.BalDate),
    new SqlParameter("@_balance",newAccount.Balance)
    };
    return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "_Create_Account_Balance", _parameters));
    }
    }
    
    
    SQLHelper Class:
     public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    {
    //create a command and prepare it for execution
    SqlCommand cmd = new SqlCommand();
    PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters);

    //execute the command & return the results
    object retval = cmd.ExecuteScalar();

    // detach the SqlParameters from the command object, so they can be used again.
    cmd.Parameters.Clear();
    return retval;

    }
    
    
    I am getting error  in object retval = cmd.ExecuteScalar()  "Failed to convert parameter value from a SqlParameter to a Int64."
    I am unable to understand what is the problem.
    
    
    Please help me.
    
    
    Thanks.
    Monday, October 3, 2011 3:37 AM

Answers

  • User1613327603 posted

    try with int32.

    account_balance.AccountNo = Convert.ToInt32(txtAcntNo.Text.Trim());
    public Int32 AccountNo
    {
    get { return _account_no; }
    set { _account_no = value; }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2011 9:36 AM

All replies

  • User191633014 posted

    did you use "Default Constructor" to create the instance of AccountBalance that you pass to createNewBalance?

    Monday, October 3, 2011 5:28 AM
  • User-543487465 posted

    Yes

    did you use "Default Constructor" to create the instance of AccountBalance that you pass to createNewBalance?

    Monday, October 3, 2011 5:37 AM
  • User1613327603 posted

    try with int32.

    account_balance.AccountNo = Convert.ToInt32(txtAcntNo.Text.Trim());
    public Int32 AccountNo
    {
    get { return _account_no; }
    set { _account_no = value; }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2011 9:36 AM