Question

I have an application that connects to a database and retrieves information (from a table called "Transactions") for a given AccountNo(which is a field in the table). Then it puts the retrieved information into an instance of a class called "Transaction". From that instance of the class it then creates a new node into a pre created generic Doubly Linked List called "Transactions". (I apologise if I have worded anything wrong, I am very new to all of this)

Now my issue is that when I try to put the retrieved information into the instance of the class "Transaction" I get an "InvalidCastException" which says "The specified cast is invalid". All the datatypes are correct so I don't really know what the problem is.

Here is my code.

Transaction class:

public class Transaction
{
    private int AccountNumber;
    private DateTime Date;
    private string Description;
    private string DebitCredit;
    private float Amount;

    public Transaction(int accountNumber, DateTime date, string description, string debitCredit, float amount)
    {
        this.AccountNumber = accountNumber;
        this.Date = date;
        this.Description = description;
        this.DebitCredit = debitCredit;
        this.Amount = amount;
    }
}

The rest of my code is behind a button click event(The line that is throwing the cast error has ** on either side of it):

private void button1_Click(object sender, EventArgs e)
{
    LinkedList<Transaction> Transactions = new LinkedList<Transaction>(); //create the generic linked list
    SqlConnection con = new SqlConnection(@"Data Source=melss002; Initial Catalog=30001622; Integrated Security=True"); //Connection string
    int accNum = Int32.Parse(Microsoft.VisualBasic.Interaction.InputBox("Please enter account number", "Account Number")); //Prompt the user for account number
    SqlCommand cmd = new SqlCommand("Select * From Transactions where AccountNo = " + accNum, con); //command to execute
    con.Open();  //open the connection to the database           
    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.HasRows)//Check if the table has records
    {
        while (reader.Read()) //read all records with the given AccountNo
        {
            **Transaction Transaction001 = new Transaction(reader.GetInt32(0), reader.GetDateTime(1), reader.GetString(2), reader.GetString(3), reader.GetFloat(4));** //New Transaction node
            Transactions.AddFirst(Transaction001);// add the node to the Doubly Linked List (Transactions)
        }
    }
    else
    {
        MessageBox.Show("No records found");
    }

    PrintNodes(Transactions);

    reader.Close();
    con.Close();
}
Was it helpful?

Solution

If all the data types were correct you wouldn't get this error message, they are not:

If you look at this line, you explicitly tell the reader to return fields of a given data type:

Transaction Transaction001 = new Transaction(reader.GetInt32(0), reader.GetDateTime(1), reader.GetString(2), reader.GetString(3), reader.GetFloat(4));

Please make sure that field 0 is actually an Int32, not an Int16 and that field 4 is actually a float, not a double or even a decimal. Decimal types are in most cases the source of errors like that and I bet your problem can be solved by changing from GetFloat to GetDouble.

OTHER TIPS

That could happen if there null values in table. For example if amout is null it will fail to convert to float. Or maybe it's double in database, not float.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top