文本框不接受null [英] Textbox doesn't accept null

查看:109
本文介绍了文本框不接受null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET



我有一个文本框应该接受null(文本框为空)。但是null值没有插入数据库(sql server)。错误是输入字符串格式不正确。编码是

ASP.NET

I've a textbox which should accept null(textbox is empty). But the null value is not inserting in database(sql server). The error is "Input string was not in a correct format". The coding is

int BusinessNumber = Convert.ToInt32(txtBusinessNumber.Text);



如果 .ToString()无效。

推荐答案

这可以通过多种方式解决:

http://msdn.microsoft.com/en-us/library/f02979c7.aspx [ ^ ]



或者你的情况可能更好:

http://msdn.microsoft.com/en-us/library/system.string。 isnullorempty.aspx [ ^ ]
This could be solved in a number of ways:
http://msdn.microsoft.com/en-us/library/f02979c7.aspx[^]

or perhaps better in your case:
http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx[^]


您必须检查文本框中是否有实际值,以及它是否可以转换为Int32。这样:

You have to check whether there is an actual value in your textbox, and if it is convertible to Int32. This way:
int BusinessNumber;
if (!string.IsNullOrEmpty(txtBusinessNumber.Text) && int.TryParse(txtBusinessNumber.Text, out BusinessNumber)) {
   // TextBox contains a valid Int32 value
}
else {
   // TextBox is empty or does not contain a valid Int32 value
}


这篇关于文本框不接受null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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