1

hey guys m having this wierd exception of cast though my datatypes are correct in db:

string sql =
            string.Format(
                @"select aim_network_id,aim_network_name,oxinetwork_id,pack_id,pack_name,p_face_value,pm_prefix from Operator where aim_network_id='{0}'",
                gridbackOffice["aim_network_id", gridbackOffice.CurrentCell.RowIndex].Value);
        OleDbCommand getSelectedGridDatecmd = new OleDbCommand(sql, conn);
        OleDbDataReader reader = getSelectedGridDatecmd.ExecuteReader();
        while (reader.Read())
        {
            txtAimNetworkID.Text = reader.GetString(0);
            txtAimNetworkName.Text = reader.GetString(1);
            txtPARNetworkID.Text = reader.GetString(2);
            txtPARFaceValue.Text = reader["p_face_value"].ToString();
           //in above line if i'm doing this `reader.GetString(5)` then i'm getting specified cast exception and that to randomly i.e some time it works fine and suddenly sometime gives this exception
            txtPARPackID.Text = reader.GetString(3);
            txtPARPackName.Text = reader.GetString(4);
            txtPARPMPrefix.Text = reader["pm_prefix"].ToString();
        }

I'm little bit confused if m using this reader["p_face_value"].ToString() then my code is running very smoothly but whats the issue with using this reader.GetString(5) , according to me both method return string, nebody had faced this error b4 ? ....Error is at 4th and 7th line in while loop.

Exception:Specified cast is not valid (InvalidCastException unhandled)

1
  • Post complete Exception details. Commented Dec 6, 2010 at 12:20

4 Answers 4

3

According to MSDN, OleDbDataReader.GetString() does not perform any conversions before attempting to cast to a string - therefore the data retrieved must already be a string.

If there is a chance that the value in that column could be null, the docs suggest that you should check if the value is null first:

if (  !reader.IsDBNull(5) ) {
    txtPARFaceValue.Text = reader.GetString(5); 
}

Calling reader["p_face_value"] on a null value returns DBNull - and when you call ToString() on DBNull, you get an empty string.

Sign up to request clarification or add additional context in comments.

Comments

2

From MSDN:

No conversions are performed; therefore the data retrieved must already be a string.

If the column is not a string type, you'll need to use the .ToString() method to convert it.

3 Comments

but how come it is running on first time, i mean it does works for a while but then suddenly that exception appears
It fails when it encounters a null value in your data, @FosterZ - see my answer for how to stop it from happening.
@Dexter: yes man, this db was containing some null values in p_face_value.. my laziness i nvr look'd into the db...thnx
0

What is the datatype of p_face_value in your database?

Based on the error description given it seems that this is not a string type, so when you call:

reader.GetString(5)

the code errors out as it cannot convert whatever type it is to a string. The .ToString() method will work as this does not use a cast.

1 Comment

p_face_value is String i.e Text in ms access
0

You should use GetString only when the column is a string-equivalent type in the database (like varchar), in your case "p_face_value" seems to be a numeric type, therefore it cannot simply convert it to a string.

The way you're doing it right now is the right way.

1 Comment

but my p_face_value is string in db...i said it is working fine while fetchin' data...but sometime suddenly it gets error..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.