Input string was not in a correct format

Posted on 2009-02-20
Last Modified: 2013-12-17
I am reading data from a table and and determined that one of the fields is a blank.  My variable I am trying to store this data to is an integer.  I am doing a Convert.ToInt32 but it does not know how to handle a blank field.  I tried putting a .ToString at the end of the data but that did not work either.  Can someone tell me the syntax to hand blank fields?
MyGlobalVars.initGuid = Guid.NewGuid();

                myMPI.MPI1 = MyGlobalVars.initGuid;

                myMPI.NPI = Convert.ToInt32(patient.d.NPI.ToString());--------  Error Here

Open in new window

Question by:kwh3856
    LVL 25

    Accepted Solution

    Convert.ToInt32(patient.d.NPI ?? 0);
    LVL 3

    Expert Comment


    myMPI.NPI = Convert.ToInt32(IIf(String.IsNullOrEmpty(patient.d.NPI.ToString),0,patient.d.NPI.ToString)

    This will supply a default value (e.g., zero) if the field is blank.

    LVL 21

    Expert Comment

    reb's is the cleanest route, '??' means if the left side if it is not null, but if it is null, use the right side

    Author Closing Comment

    Thank you very much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now