Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3460480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:14:05+00:00 2026-05-18T10:14:05+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:14:06+00:00Added an answer on May 18, 2026 at 10:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
Hey guys I am having a lot of trouble trying to understand this and
Hey Guys, having an issue with Linq-to-SQL, I am basically doing the following but
Hey guys I'm trying to work my way through this but am having an
Hey guys, i'm having some trouble with this: In the object consumer i need
Hey guys. I'm having this silly issue with inserting text with forwardslash into cart
Hey guys. I'm having this problem for 2 days now and I really don't
Hey guys, i'm having a problem with this little script. When i hover over
Hey guys, I'm new to jQuery/js and having a hard time figuring this out.
Hey guys am having a bit of problems with my jQuery, I have all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.