Solved

Failed to convert parameter value from a String to a Int32

Posted on 2008-06-25
4
994 Views
Last Modified: 2013-12-16
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();
        }
0
Comment
Question by:kwh3856
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
JuckMan earned 250 total points
ID: 21870575
Covert.ToInt32(string) should work for you.

Here is a link that can give you more info
http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx
0
 
LVL 5

Expert Comment

by:JuckMan
ID: 21870583
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
0
 
LVL 7

Assisted Solution

by:photowhiz
photowhiz earned 250 total points
ID: 21870922
Why are you storing zip codes as integers? Are you going to add them or something? That makes no sense, people type zip codes as strings, you should store them as strings.

Also, some countries allow letters in zip or postal codes.
0
 

Author Closing Comment

by:kwh3856
ID: 31470800
Thank you very much for your reply. Both of you had the right answer.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

20 Experts available now in Live!

Get 1:1 Help Now