开发者

Error: input string was not in a correct format

开发者 https://www.devze.com 2023-01-05 20:36 出处:网络
I c开发者_如何转开发reate stored procedure in the following format. ALTER procedure [spInsertEmp]

I c开发者_如何转开发reate stored procedure in the following format.

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 

in that I gave codebehind using c# like below format

    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";

in that I got error for this line

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

The error was

input string was not in a correct format

So any one help me to give the update button code belongs this above stored procedure. Please help me.


This will occur if your input string could not be converted to an integer (for example, "123" can be converted, but "123abc" obviously can't). You can use TryParse instead:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号