无法从字符串转换为十进制asp.net [英] cannot convert from string to decimal asp.net

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

问题描述

protected void btnClick_Register(object sender, EventArgs e)
    {
        RegisterdbDataContext ds = new RegisterdbDataContext();
        ds.StoreNewUsers(txtFirstName.Text, txtLastName.Text, txtEmailId.Text, txtPassword.Text, txtContactNumber.Text);
    }



我在txtContactNumber上收到错误。


I,m getting error on txtContactNumber.

推荐答案

StoreNewUsers 可能有十进制参数而你正在传递字符串,这就是你收到错误的原因。
StoreNewUsers might be having decimal parameter and you're passing string, that's why you're getting error.
ds.StoreNewUsers(txtFirstName.Text, txtLastName.Text, txtEmailId.Text, txtPassword.Text, decimal.Parse(txtContactNumber.Text));



试一试。



-KR


Give it a try.

-KR


如果你确定txtContactNumber只允许数字,它是必填字段然后使用decimal.Parse

If you are sure that txtContactNumber only allows numbers and it is mandatory field then use decimal.Parse
decimal contactNumber = decimal.Parse(txtContactNumber.Text);



其次,如果你不确定txtContactNumber值意味着用户可以提供字符或空值,那么为了安全使用十进制.TryParse


Secondly if you are not sure about txtContactNumber value means user can provide chars or blank value then for safety use decimal.TryParse

decimal contactNumber;
bool isSuccess = decimal.TryParse(txtContactNumber.Text, out contactNumber);


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

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