文本框不接受null

编程入门 行业动态 更新时间:2024-10-28 20:21:58
本文介绍了文本框不接受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

本文发布于:2023-11-08 18:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不接受   文本框   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!