Solved

Failed to convert parameter value from a String to a Int32

Posted on 2008-06-25
4
996 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 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