输入字符串的错误格式不正确 [英] Getting error as input string was not in a correct format

查看:196
本文介绍了输入字符串的错误格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码



在textbox5 = 7.5 / 8.25 / 6.5中输入数字值时因输入字符串格式不正确而收到错误。< br $>


int hrsval = int.Parse(textBox5.Text);

string command =INSERT INTO Spldetails([Date],[EmpId] ,[EmpName],[指定],[记录],[Splwrkhrs],[团队],[Splwrk],[Splwrkdetails],[Allottedby])VALUES('this this.dateTimePicker1.Text +','+ this.textBox1.Text +','+ this.textBox2.Text +','+ this.textBox3.Text +','+ Convert.ToInt32(this.textBox4.Text)+', '+ hrsval +','+ this.textBox8.Text +','+ this.comboBox2.Text +','+ this.textBox6.Text +','+ this.comboBox1。文字+');



我尝试过:



int hrsval = int.Parse(textBox5.Text);

string command =INSERT INTO Spldetails([Date],[EmpId],[EmpName],[Designation],[Records],[ Splwrkhrs],[队],[Splwrk],[Splwrkdetails],[Allottedby] )VALUES('+ this.dateTimePicker1.Text +','+ this.textBox1.Text +','+ this.textBox2.Text +','+ this.textBox3.Text +' ,'+ Convert.ToInt32(this.textBox4.Text)+','+ hrsval +','+ this.textBox8.Text +','+ this.comboBox2.Text +',' + this.textBox6.Text +','+ this.comboBox1.Text +');

this is my code

while entering number value in textbox5 = 7.5/8.25/6.5 i am getting error as Input string was not in a correct format.

int hrsval = int.Parse(textBox5.Text);
string command = "INSERT INTO Spldetails([Date],[EmpId],[EmpName],[Designation],[Records],[Splwrkhrs],[Team],[Splwrk],[Splwrkdetails],[Allottedby]) VALUES('" + this.dateTimePicker1.Text + "','" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + Convert.ToInt32(this.textBox4.Text) + "','" + hrsval + "','" + this.textBox8.Text + "','"+this.comboBox2.Text+"','" + this.textBox6.Text + "','" + this.comboBox1.Text + "')";

What I have tried:

int hrsval = int.Parse(textBox5.Text);
string command = "INSERT INTO Spldetails([Date],[EmpId],[EmpName],[Designation],[Records],[Splwrkhrs],[Team],[Splwrk],[Splwrkdetails],[Allottedby]) VALUES('" + this.dateTimePicker1.Text + "','" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + Convert.ToInt32(this.textBox4.Text) + "','" + hrsval + "','" + this.textBox8.Text + "','"+this.comboBox2.Text+"','" + this.textBox6.Text + "','" + this.comboBox1.Text + "')";

推荐答案

首先,不要使用Parse将用户输入转换为数值:如果失败则抛出异常 - 并且用户会一直犯错。毕竟,他们只是人类。改为使用TryParse,并报告特定消息的问题,以便tehy可以解决问题:

First off, don't use Parse to convert user input to numeric values: it throws an exception if it fails - and it will, users make mistakes all the time. They are after all, only human. Use TryParse instead, and report a problem with a specific message instead so tehy can correct the problem:
int hrsval;
if (!int.TryParse(textBox5.Text, out hrsval))
   {
   MessageBox("Please enter a integer number between 0 and 99");
   return;
   }



其次,您不能将7.5之类的值解析为整数:它是一个浮点值,因此您需要解析它并将其存储在一个浮点数或双倍数:


Second, You can't parse a value like 7.5 as an integer: it's a floating point value so you need to parse it and store it in a float or double:

double hrsval;
if (!double.TryParse(textBox5.Text, out hrsval))
   {
   MessageBox("Please enter a integer number between 0.0 and 99.99");
   return;
   }



第三,不要那样做数据库访问!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:


Thirdly, don't do database access like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



BTW:帮自己一个忙,并停止使用Visual Studio默认名称 - 你可能还记得TextBox8 今天是手机号码,但是当你必须在三周内修改它时,你会这样吗?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键......

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...


这篇关于输入字符串的错误格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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