数据类型转换错误 [英] Data type Convertion error

查看:220
本文介绍了数据类型转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,

Hi sir,

Input string was not in a correct format.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error: 

Line 29:             User_Registration urobj = new User_Registration();
Line 30:             urobj.Username = txt_username.Text;
Line 31:             urobj.Userid = Convert.ToInt32(txt_userid.Text);
Line 32:             urobj.Password = txt_passord.Text;
Line 33:             urobj.Conformpassword = txt_confrmpasswrd.Text;

Error At Line 31: Input string was not in a correct format.


即使我尝试了urObj.userid = int.Parse(txt_userid.Text).这也不起作用.

即使在数据库中,我也将useid声明为整数.

请帮助我....!!!!


even i tried urObj.userid=int.Parse(txt_userid.Text). This is also not working.

even in database also i declared useid as integer.

Please help me out....!!!!

推荐答案

txt_userid中的内容(文本)包含非法字符,这些字符将转换为整数.因此,请确保输入正确的数字.

此外,也可以使用 TryParse [
The contents (text) in txt_userid contains illegal characters to be translated to integer. So ensure that you''re inputting a proper number.

Also instead of using Parse, use TryParse[^] to check if the conversion can be done. If it cannot be done, inform the user to correct the data.


嗨普拉萨德,

即使您为文本框txt_userid传递空字符串,
也会给出相同的错误.
因此,请尝试使用具有一些数值的原始代码.
Hi Prasad,

Even if you are passing empty string for text box txt_userid,
then also it will give same error.
So try your original code with some numeric value.


尝试一下:

Try this:

User_Registration urobj = new User_Registration();
urobj.Username = txt_username.Text;
if (txt_userid.Text!= null)
urobj.Userid = Convert.ToInt32(txt_userid.Text);
else
urobj.Userid = 0;

urobj.Password = txt_passord.Text;
urobj.Conformpassword = txt_confrmpasswrd.Text;

Alternatively,
Debug the line 31 and check the value for  txt_userid.Text.
If it is "" (blank) then do the following:

User_Registration urobj = new User_Registration();
urobj.Username = txt_username.Text;
if (txt_userid.Text!= "")
urobj.Userid = Convert.ToInt32(txt_userid.Text);
else
urobj.Userid = 0;

urobj.Password = txt_passord.Text;
urobj.Conformpassword = txt_confrmpasswrd.Text;



希望对您有帮助!



Hope this helps!!!


这篇关于数据类型转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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