如何将字符串转换为int32 c#?

时间:2015-10-27 10:04:08

标签: c# sql-server winforms ado.net

我正在尝试从C #Windows窗体上的某个文本框和一个标签插入SQL Server中的Transaction表,其代码如下:

AppProcess abc = new AppProcess();
SqlConnection sqlconn1 = abc.GetConn();

SqlCommand sqlinsert = new SqlCommand("insert into Payment values
(@TID,@EID,@CustID,@CFName,@CLName,@BID,@BName,@Price,@Qty,@TransDate)",
sqlconn1);

DataTable dtCustomer = new DataTable();
sqlconn1.Open();

sqlinsert.Parameters.Add(new SqlParameter("@TID", SqlDbType.VarChar, 10));
sqlinsert.Parameters.Add(new SqlParameter("@EID", SqlDbType.VarChar, 10));
sqlinsert.Parameters.Add(new SqlParameter("@CustID", SqlDbType.VarChar, 10));
sqlinsert.Parameters.Add(new SqlParameter("@CFName", SqlDbType.VarChar, 100));
sqlinsert.Parameters.Add(new SqlParameter("@CLName", SqlDbType.VarChar, 100));
sqlinsert.Parameters.Add(new SqlParameter("@BID", SqlDbType.VarChar, 10));
sqlinsert.Parameters.Add(new SqlParameter("@BName", SqlDbType.VarChar, 100));
sqlinsert.Parameters.Add(new SqlParameter("@Price", SqlDbType.Int));
sqlinsert.Parameters.Add(new SqlParameter("@Qty", SqlDbType.Int));
sqlinsert.Parameters.Add(new SqlParameter("@Transdate", SqlDbType.Date));

sqlinsert.Parameters["@TID"].Value = tidTxt.Text;
sqlinsert.Parameters["@EID"].Value = eidTxt.Text;
sqlinsert.Parameters["@CustID"].Value = cidTxt.Text;
sqlinsert.Parameters["@CFName"].Value = cfnameTxt.Text;
sqlinsert.Parameters["@CLName"].Value = clnameTxt.Text;
sqlinsert.Parameters["@BID"].Value = bidTxt.Text;
sqlinsert.Parameters["@BName"].Value = bnameTxt.Text;
sqlinsert.Parameters["@Price"].Value = label17.Text;
sqlinsert.Parameters["@Qty"].Value = qtychoiceTxt.Text;
sqlinsert.Parameters["@Transdate"].Value = TransDateTxt.Text;

sqlinsert.ExecuteNonQuery();

但是当我运行应用程序时,它会生成此错误: click here to see what the error looks like

我很困惑哪条线会触发错误。以及如何将字符串转换为Int32?

2 个答案:

答案 0 :(得分:6)

使用应该像这个代码一样用于转换:

command.Parameters.Add("@Qty", SqlDbType.Int).Value = Convert.ToInt32(qtychoiceTxt.Text);

答案 1 :(得分:0)

您必须使用Convert.ToInt32(“string”)将“label17.Text”和“qtychoiceTxt.Text”转换为整数