Solved

Failed to convert parameter value from a String to a Int32

Posted on 2008-06-25
4
1,000 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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