2013-05-13 86 views
0

我将文本框中的值保存回数据库。如何将输入到文本框中的数字保存回数据库。我曾尝试以下,但得到一个错误说Input string was not in a correct formatINT到字符串 - 从文本框保存回数据库

newCarsRow.CustomerID = Convert.ToInt32(TextBox5.Text); 
//I have also tried 
newCarsRow.CustomerID = int.Parse(TextBox5.Text); 

我保存文本输入到文本框,像这样

newCarsRow.Surname = TextBox1.Text.ToString(); 
+0

customerid是数字字段吗? – matzone 2013-05-13 10:01:34

+0

你在TextBox5.Text中得到了什么,它不是数字 – Habib 2013-05-13 10:01:36

+0

你输入了什么数字?您还必须通过设置DBNull值分别处理空值。 – 2013-05-13 10:01:41

回答

2

而不是使用

newCarsRow.CustomerID = int.Parse(TextBox5.Text); 

,你应该尝试使用

int customerID = 0; 
if(int.TryParse(TextBox5.Text.Trim(), out customerID)) 
{ 
    newCarsRow.CustomerID = customerID; 
} 
else 
{ 
    // Customer id received from the text box is not a valid int. Do relevant error processing 
} 

现在你不会得到你以前所面临的异常,并且也将是能够执行相关的错误处理。

1

IF newCarsRow.CustomerID为int,可以是空间是有一些问题。

那就试试这个

newCarsRow.CustomerID = Convert.ToInt32(TextBox5.Text.Trim()); 
+0

我收到一个错误,说输入字符串不正确的格式。 – joshuahornby10 2013-05-13 10:08:53

+0

可以在执行Line之前调试Value TextBox5.Text,告诉我这是CustomerID的类型 – 2013-05-13 10:13:20

相关问题