C#Windows应用程序文本框输入事件问题 [英] C# windows application Text Box Enter event problem
本文介绍了C#Windows应用程序文本框输入事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在尝试在文本框中输入以下代码,输入事件
Hi All,
I m trying to execute the following code in text box enter event
int id;
id = Convert.ToInt32(txtRollNumber.Text);
SqlConnection Con = new SqlConnection(@"Data Source=HORTON-2A5CB16E\SQLEXPRESS;Initial Catalog=Temp;Integrated Security=SSPI;Max Pool Size=100;Connect Timeout=1000");
Con.Open();
但在运行时它显示id = Convert.ToInt32(txtRollNumber.Text); input string is not in correct format
请帮忙
谢谢&问候
英德拉吉特
[添加了可读性的代码块]
but in run time it is showing id = Convert.ToInt32(txtRollNumber.Text); input string is not in correct format
Please help
Thanks & Regards
Indrajit
[added code block for readability]
推荐答案
这就是它.txtRollNumber
文本框中的文本应为整数格式:所有数字,可选的减号,有效范围等.在转换之前,需要对其进行验证.代替Convert
,使用int.TryParse(txtRollNumber.Text, out id)
;这将进行验证,并在成功的情况下将值赋给id
并返回布尔值成功状态.
在转换出现问题之前,使用调试器检查txtRollNumber.Text
.
—SA
This is what it is. The text in the text boxtxtRollNumber
should be in integer format: all digits, optional minus, withing the valid range, etc. You need to validate it before converting. Instead ofConvert
, useint.TryParse(txtRollNumber.Text, out id)
; this will validate, assign right valued toid
in case of success and return Boolean success status.
Use debugger to inspecttxtRollNumber.Text
before conversion is something is going wrong.
—SA
请改用它,
Hi use this instead,
int id = 0;
Int32.TryParse(txtRollNumber.Text, out id);
-检查您在txtRollNumber.Text上获得了什么值,该值不能为空
-使用TRIM()忽略空格
- check what value u have get on txtRollNumber.Text, it should not be empty
- Use TRIM() to omit the spaces
这篇关于C#Windows应用程序文本框输入事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文