kwh3856
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.Conne ctionStrin gs["123123 123"].Conn ectionStri ng;
SqlCommand cmd;
InsLookUp.Open();
cmd = new SqlCommand("InsCompLookup" , InsLookUp);
cmd.CommandType = CommandType.StoredProcedur e;
// Input Parameters
cmd.Parameters.Add("@CARRI ER_NAME", SqlDbType.VarChar, 36).Value = tbCarrierName.Text;
cmd.Parameters.Add("@CARRI ER_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;------- ---------- ---------- ------Prob lem LINE
cmd.Parameters.Add("@PHONE ", SqlDbType.VarChar, 11).Value = tbCarrierPhone.Text;
cmd.ExecuteNonQuery();
if (InsLookUp != null)
{
InsLookUp.Close();
}
Here is my current code:
SqlConnection InsLookUp;
InsLookUp = new SqlConnection();
InsLookUp.ConnectionString
SqlCommand cmd;
InsLookUp.Open();
cmd = new SqlCommand("InsCompLookup"
cmd.CommandType = CommandType.StoredProcedur
// Input Parameters
cmd.Parameters.Add("@CARRI
cmd.Parameters.Add("@CARRI
cmd.Parameters.Add("@CITY"
cmd.Parameters.Add("@STATE
cmd.Parameters.Add("@ZIP",
cmd.Parameters.Add("@PHONE
cmd.ExecuteNonQuery();
if (InsLookUp != null)
{
InsLookUp.Close();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much for your reply. Both of you had the right answer.
hope this helps