2011-11-24 61 views
2

如何使用asp.net將電話號碼插入表中?在表中插入電話號碼

string insertquery = 
    "insert customer (
     id, name, address, gender, DOB, contactno, email, userid, password) 
    values 
     (@custid, @custname, @custaddress, @gender, @custdob, 
     @custcno, @custemail, @custuserid, @custpassword)"; 

這裏custcno是電話號碼。

sc.Parameters.AddWithValue("@custcno",Convert.ToInt32(txtcustcno.Text)); 

在運行時出現錯誤輸入字符串的格式不正確。

表contactno爲varchar(50)

+1

如果號碼不與計算,這是不是一個數字。電話號碼是字符串。 –

回答

1

的代碼試圖將電話號碼字符串轉換爲數字在這裏:

Convert.ToInt32(txtcustcno.Text) 

所以,如果它包含任何非數字字符,如括號的破折號,然後它會拋出異常。

要只需要插入字符串,不首先把它,這樣做:

sc.Parameters.AddWithValue("@custcno", txtcustcno.Text); 
+0

謝謝你;-)其工作..i試着當你說.. ;-) – raji

1

我不知道從這樣的問題:爲什麼Convert.ToInt32?許多電話號碼格式都有空格,破折號(&)。這些都會導致Convert.ToInt32使用ASP.NET驗證控件扔Input string was not in a correct format.

瓶坯輸入驗證(或其他),並做到這一點:

sc.Parameters.AddWithValue("@custcno",txtcustcno.Text); 
+0

非常感謝你;-)工作正常;-) – raji

1

的電話號碼,不要轉換爲int。你的參數更改爲:

sc.Parameters.AddWithValue("@custcno",txtcustcno.Text); 
+0

謝謝..你的工作.. – raji