无法将参数值从字符串转换为int32 [英] failed to convert parameter value from a string to a int32

查看:135
本文介绍了无法将参数值从字符串转换为int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是

my code is

protected void btnsubmit_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection vcon = new SqlConnection(ConfigurationManager.ConnectionStrings["surendra"].ToString());

            vcon.Open();
            SqlCommand vcmd = new SqlCommand();
            vcmd.Connection = vcon;
            vcmd.CommandType = CommandType.StoredProcedure;
            vcmd.CommandText = "register_sp ";
            SqlParameter sp1 = new SqlParameter("@emailid", SqlDbType.VarChar, 100);
            sp1.Direction = ParameterDirection.Input;
            sp1.Value = Txteid.Text;
            vcmd.Parameters.Add(sp1);
            SqlParameter sp2 = new SqlParameter("@password", SqlDbType.Int);
            sp2.Direction = ParameterDirection.InputOutput;
            sp2.Value = Txtpwd.Text;
            vcmd.Parameters.Add(sp2);
            SqlParameter sp3 = new SqlParameter("@firstname", SqlDbType.VarChar, 100);
            sp3.Direction = ParameterDirection.Input;
            sp3.Value = txtfname.Text;
            vcmd.Parameters.Add(sp3);
            SqlParameter sp4 = new SqlParameter("@lastname", SqlDbType.VarChar, 100);
            sp4.Direction = ParameterDirection.Input;
            sp4.Value = Txtlname.Text;
            vcmd.Parameters.Add(sp4);
            vcmd.ExecuteNonQuery();
            Response.Write("http://www.facebook.com");
            vcon.Close();
        }
       catch(Exception ex)
        {

            Response.Write(ex.Message);
       }



我的数据库是


my database is

ALTER procedure [dbo].[register_sp]
@emailid varchar(100),@password int,@firstname varchar(100),@lastname varchar(100)
as
insert into  Users(emailid,Password,firstname,lastname) values (@emailid,@password,@firstname,@lastname)
select * from users

推荐答案

为什么你认为所有密码都是数字?

And那个整数?

你把你的参数设置为一个,然后传递一个用户输入的字符串...所以即使它应该是数字,你也不要检查,所以任何用户输入错误会使应用程序崩溃...
Why do you seem to think that all passwords will be numbers?
And integers at that?
You set your parameter as an into, and then pass it a user entered string... So even if it was supposed to be numeric, you don't check, so any user input error will crash the app...


而不是整数try varchar for password field。
Instead of integer try varchar for password field.


您是否希望所有用户输入数字密码? ?即使它是这样,你应该使用



Convert.ToInt32(txtpwd.Text);
Do you want all users to enter numeric password?? even if it is so ,You should use

Convert.ToInt32(txtpwd.Text);


这篇关于无法将参数值从字符串转换为int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆