本文介绍了文本框不接受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()无效。
推荐答案这可以通过多种方式解决: msdn.microsoft/en-us/library/f02979c7.aspx [ ^ ] 或者你的情况可能更好: msdn.microsoft/en-us/library/system.string。 isnullorempty.aspx [ ^ ] This could be solved in a number of ways: msdn.microsoft/en-us/library/f02979c7.aspx[^] or perhaps better in your case: msdn.microsoft/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
发布评论