Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

Failed to convert parameter value from a String to a Int32

I am creating a code and sql script to lookup data in a table.  One of the fields I use is a Zip code.  If the user inputs a zip code there is no error.  If the field is left blank I get the Failed to Convert parameter value from a String to a Int32.  Can someone show me the proper way to hand this problem.

Here is my current code:

SqlConnection InsLookUp;
        InsLookUp = new SqlConnection();
        InsLookUp.ConnectionString = ConfigurationManager.ConnectionStrings["123123123"].ConnectionString;

        SqlCommand cmd;

        InsLookUp.Open();

        cmd = new SqlCommand("InsCompLookup", InsLookUp);
        cmd.CommandType = CommandType.StoredProcedure;

       
        // Input Parameters
        cmd.Parameters.Add("@CARRIER_NAME", SqlDbType.VarChar, 36).Value = tbCarrierName.Text;
        cmd.Parameters.Add("@CARRIER_ID", SqlDbType.VarChar, 15).Value = tbCarrierId.Text;
        cmd.Parameters.Add("@CITY", SqlDbType.VarChar, 20).Value = tbCarrierCity.Text;
        cmd.Parameters.Add("@STATE", SqlDbType.VarChar, 2).Value = tbCarrierState.Text;




        cmd.Parameters.Add("@ZIP", SqlDbType.Int).Value = tbCarrierZip.Value;---------------------------------Problem LINE
        cmd.Parameters.Add("@PHONE", SqlDbType.VarChar, 11).Value = tbCarrierPhone.Text;


       
        cmd.ExecuteNonQuery();

        if (InsLookUp != null)
        {
            InsLookUp.Close();
        }
ASKER CERTIFIED SOLUTION
Avatar of JuckMan
JuckMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JuckMan
JuckMan

Also for some reason if the converter fails, then obviously the value you are trying to convert is not a number may have some alpha characters in them.

hope this helps
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kwh3856

ASKER

Thank you very much for your reply. Both of you had the right answer.