错误:输入字符串格式不正确

时间:2010-07-01 06:28:09

标签: asp.net

我按以下格式创建存储过程。

ALTER procedure [spInsertEmp]
@empName varchar(50),
@Emp_Id int ,
@option varchar(10)
as
set nocount off


IF  @option='delete'

delete from emp where Emp_Id=@Emp_Id   

if @option='insert'

IF EXISTS(select EmpName from emp where EmpName=@empName)
return -1
ELSE
insert into emp (EmpName)values(@empName) 

if @option='update'
begin
   UPDATE emp set Empname=@empname where Emp_Id=@Emp_Id
End 

因为我使用c#,如下面的格式

给了代码隐藏
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("server = MYLAPTOP;uid=sa;pwd =hari_123; database =test1");

        //conn.Open();

        SqlCommand cmd = new SqlCommand("spInsertEmp", conn);

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@empName", SqlDbType.VarChar, 50).Value = txtAdditionalChargeType.Text;
        cmd.Parameters.Add("@Emp_Id", SqlDbType.Int).Value =Convert.ToInt32(txtAdditionalChargeType1.Text);
        cmd.Parameters.Add("@option", SqlDbType.VarChar, 10).Value = "update";

因为我收到了这行的错误

cmd.Parameters.Add("@Emp_Id", SqlDbType.Int).Value =Convert.ToInt32(txtAdditionalChargeType1.Text);

错误是

  

输入字符串的格式不正确

所以任何人都帮我提供更新按钮代码属于上面的存储过程。请帮帮我。

1 个答案:

答案 0 :(得分:0)

如果您的输入字符串无法转换为整数,则会发生这种情况(例如,“123”可以转换,但“123abc”显然不能转换)。您可以改用TryParse:

int additionalChargeType1;
if(int.TryParse(txtAdditionalChargeType1.Text, out additionalChargeType))
{
    //The conversion works, now do something with additionalChargeType
}
else
{
    //The conversion didn't work.
}