haroldjclements Posted August 2, 2004 Posted August 2, 2004 I have a problem with reading null value from my database. The connection works and does retrieve the data. However if there is an empty cell in the database it creates an exception �java.lang.classCastException: Specified cast is not valid� while (myDataReader.Read()) { description = myDataReader.GetString(0); //The error is here! custPN = System.Convert.ToString(myDataReader.GetString(2)); //custPN = myDataReader.GetString(2); sql = "SELECT Name FROM tblSupplier WHERE SupplierID = " + myDataReader.GetInt16(1); } I have tried to convert the output to a string, but that did not work. I think that the problem is with the .GetString() line as a blank cell is not a string. This cell is not always empty so .GetString() works fine when it's not. If anyone could help me I will be eternally grateful Quote
haroldjclements Posted October 12, 2004 Author Posted October 12, 2004 ok, I have worked it out. Using the try and catch you can tell your program to just ignore that error. Hope this helps someone. Harold Quote
samsmithnz Posted October 12, 2004 Posted October 12, 2004 Try checking to see if the value is null before assigning it to the string. You'll find this alot quicker than catching an exception. Quote Thanks Sam http://www.samsmith.co.nz
haroldjclements Posted January 19, 2005 Author Posted January 19, 2005 Hello, I know it�s been a long time since your post but I have come across the same problem again. How would you check to see if the item being dragged from the database is null? If I use the �myDataReader.GetString(x)� within an �if� statement then I still get the error, so presumably I need to test the object before the conversion. My question is how? Thanks in advance, Harold Clements Quote
haroldjclements Posted January 19, 2005 Author Posted January 19, 2005 Ok found it: if (myDataReader.IsDBNull(5)){textBox3.set_Text("");} else {textBox3.set_Text(myDataReader.GetString(5));} Cheers, Harold Clements Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.