Solved

Failed to convert parameter value from a String to a Int32

Posted on 2008-06-25
4
1,001 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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