Lucy_H

I created a card game in a windows form. This involved creating a class library and an extra class. The project was working fine. My computer lost my profile settings this morning but all my files were still found in the correct places. However, when I ran this card game project (after no changes were made and the project was run quite a few times yeterday with no trouble and the solution is built successfully) I all of a sudden have this error:

InvalidCastException was unahndled by user code.

Unable to cast object of type 'System.DBNull' to type 'TestingGameLibrary.Card'.

Can anyone give any advice

Lucy



Re: Visual C# General Unable to cast object of type 'System.DBNull

Manju Sandhu

Hi,

Can you plz provide the code in which you are getting this error.

Regards,

Manju Sandhu





Re: Visual C# General Unable to cast object of type 'System.DBNull

Florian Reischl

Hi Lucy

Is it possible, that you work with a database as back-end for any configurations If yes there are some NULL-Values.

Another possible reason could be that you work with data-binding. In default configuration missing objects will be interpreted as DBNull.Value.

Could you describe more specific the location where the exception will be thrown.

Regards

Flo





Re: Visual C# General Unable to cast object of type 'System.DBNull

Lucy_H

I have this code in the forms code:

for (int i = 0; i < players[currentPlayer].PlayHand.Count; i++)

{

listBox2.Items.Add(players[currentPlayer].PlayHandIdea);

}

This refers to a class called Player.cs and a class called Card.cs. When this code gets a value from the Card.cs class I get the error pointing to the following code in the Card.cs class:

public override bool Equals(object card)

{

return this == (Card)card;

}

Before the error is shown the code above is reached several times without throwing an exception.

Regards,

Lucy





Re: Visual C# General Unable to cast object of type 'System.DBNull

Manju Sandhu

Hi,

From where you are getting this value players[] array.

Regards,

Manju Sandhu





Re: Visual C# General Unable to cast object of type 'System.DBNull

Lucy_H

From the class Player.cs. This array is defined with this code:

Player[] players = new Player[numberOfPlayers];

for (int p = 0; p < players.Length; p++)

{

string playerName = listBox1.Items[p].ToString();

players[p] = new Player(playerName);

}

Regards,

Lucy





Re: Visual C# General Unable to cast object of type 'System.DBNull

Florian Reischl

Hi Lucy

I think the Exception occurs within your overridden Equals-Method. Please set a breakpoint into this method and check the type of the incoming object which you try to convert to "Card".

If the incoming value is "DBNull" there is the problem. Check the "call stack" to see where this method is called from.

Regards

Flo





Re: Visual C# General Unable to cast object of type 'System.DBNull

Hayder Marzouk

Hi,

I encountrer a similar problem when binding controls. When nothing is selected (for example no item selected in a combobox or empty text in a textbox) the .Net frameword tries to set DBNull.Value to the bound property.

Are using any king of binding in your code

HTH





Re: Visual C# General Unable to cast object of type 'System.DBNull

Lucy_H

Flo,

The incoming objects are from the class library, from the classes Rank.cs and Suit.cs. These values of these objects are correct. Under the object 'card' the value is set to {}.

Under the call stack the arrow is pointing to:

> TestingGameLibrary.dll!TestingGameLibrary.Card.Equals(object card = {}) Line 35 C#

I don't think this is correct. The object card should contain a value of rank and suit not {}.

HTH,

I do not think I am using any binding in my code.

Regards,

Lucy





Re: Visual C# General Unable to cast object of type 'System.DBNull

Hayder Marzouk

Hi again,

The main error message is due to a cast to System.DBNull. I don't think u set anywhere in your code a property or an object to DBNull.Value.

The DbNull class belongs to the System.Data namespace, two possibles reasons to this problem :

- You're using databiding

- You're using Rows (or tables) to store some data.

Try posting some code snipets (cpecially of the Equals function) and the full stack trace (using error.Tostring()).

HTH





Re: Visual C# General Unable to cast object of type 'System.DBNull

Lucy_H

I have been looking at my code in detail and I see no reason why the code causing the error should be accessed. I have tried commenting the following code out and the application runs successfully with no errors:

public override bool Equals(object card)

{

return this == (Card)card;

}

The application was built up in stages and different tasks were completed and I think that this was just left in by mistake. I still do not really understand why it was being accessed from this point:

for (int i = 0; i < players[currentPlayer].PlayHand.Count; i++)

{

listBox2.Items.Add(players[currentPlayer].PlayHandIdea);

}

And I still do not understand why it worked successfully yesterday and not today when no changes were made.

Thank you for all your help.

Regards,

Lucy





Re: Visual C# General Unable to cast object of type 'System.DBNull

Hayder Marzouk

Hi,

I think I have a small idea.

Can u Add this line of code at first line of Equals (object card) :

if (card == DBNull.Value)

{

MessageBox.Show ("My object equals DBNull.Value");

return false;

}

Here I am sure that somewhere the card object is set to DBNull.Value. So If u have the message, you have just to debug.

HTH





Re: Visual C# General Unable to cast object of type 'System.DBNull

Lucy_H

I cannot put the exact code example you gave into my class as it is just a class and not part of the form. Therefore a message box cannot be shown.

However I used this code instead:

if (card == DBNull.Value)

{

card.ToString();

return false;

}

else

{

return true;

}

Whilst debugging the code, card did equal DBNull.

Lucy





Re: Visual C# General Unable to cast object of type 'System.DBNull

Fadi Nassri

Code Block

If Not (dr("card").Equals(DBNull.Value)) Then

'not Null Code

Else

'Null Code:

card=new card

End If






Re: Visual C# General Unable to cast object of type 'System.DBNull

Lucy_H

My computer has restored its profile settings today. (When I haven't done anything). I have put the original troubling code back in:

public override bool Equals(object card)

{

return this == (Card)card;

}

The application works fine now with no errors appearing during run time. The object card is now not null. Does anyone know what has this has got to do with my profile settings

Lucy