vb.net 无法在 ExecuteNonQuery 上将参数值从字符串转换为 Int32

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16308561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:26:16  来源:igfitidea点击:

Failed to convert parameter value from a String to a Int32 on ExecuteNonQuery

asp.netsqlsql-servervb.net

提问by user2337472

Cant figure out the problem with this any help? the code stops at this line. I forgot to mention that I have a primary key which increments by one each time a new record is added. In the database it is also an integer. i didnt add it to this code as i presumed it would add itself when all of these records were added. if that makes sense.

无法弄清楚这个问题有什么帮助吗?代码停在这一行。我忘了提到我有一个主键,每次添加新记录时都会增加 1。在数据库中它也是一个整数。我没有将它添加到此代码中,因为我认为在添加所有这些记录时它会自行添加。如果这是有道理的。

recordsAffected = cmd.ExecuteNonQuery, 

I'm not sure what the problem is with this.

我不确定这有什么问题。

Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim recordsAffected As String
Dim cmdstring As String = "INSERT House (TypeofHouse, NumberofRooms, Location, Cost, Information, Picture) Values(@TYPEOFHOUSE, @NUMBEROFROOMS, @LOCATION, @COST, @INFORMATION, @PICTURE)"

conn = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\House.mdf;Integrated Security=True;User Instance=True")

cmd = New SqlCommand(cmdstring, conn)

cmd.Parameters.Add("@TYPEOFHOUSE", SqlDbType.NVarChar).Value = txtType.Text
cmd.Parameters.Add("@NUMBEROFROOMS", SqlDbType.Int).Value = txtNumOfRooms.Text
cmd.Parameters.Add("@LOCATION", SqlDbType.NVarChar).Value = txtLocation.Text
cmd.Parameters.Add("@COST", SqlDbType.Int).Value = txtCost.Text
cmd.Parameters.Add("@INFORMATION", SqlDbType.NVarChar).Value = txtInfo.Text
cmd.Parameters.Add("@PICTURE", SqlDbType.NVarChar).Value = txtLocation.Text

conn.Open()

recordsAffected = cmd.ExecuteNonQuery
conn.Close()

回答by Darren Wainwright

Have you tried converting your text to integer first.

您是否尝试先将文本转换为整数。

I.E change:

IE变化:

cmd.Parameters.Add("@NUMBEROFROOMS", SqlDbType.Int).Value = txtNumOfRooms.Text

to

cmd.Parameters.Add("@NUMBEROFROOMS", SqlDbType.Int).Value = CInt(txtNumOfRooms.Text)

The same would go for all your other Integers.

所有其他整数也是如此。

Basically your telling your parameter what data type to expect; SqlDbType.Intin this case, so you need to convert your input data; txtNumOfRooms.Textin this case, to the type expected.

基本上你告诉你的参数期望什么数据类型;SqlDbType.Int在这种情况下,您需要转换输入数据;txtNumOfRooms.Text在这种情况下,到预期的类型。

回答by Dennis Traub

You should convert the content of txtNumOfRoomsand txtCostto an intfirst.

应该转换的内容txtNumOfRooms,并txtCost以一个int第一。

Any one of the function calls below will probably work if the text in the respective input fields actually is a number:

如果相应输入字段中的文本实际上是数字,则下面的任何一个函数调用都可能起作用:

  • CInt(txtNumOfRooms.Text)
  • Int32.Parse(txtNumOfRooms.Text)
  • Convert.ToInt32(txtNumOfRooms.Text)
  • CInt(txtNumOfRooms.Text)
  • Int32.Parse(txtNumOfRooms.Text)
  • Convert.ToInt32(txtNumOfRooms.Text)

But since you are working with user input, you'd better check if the input for any numeric value actually is numeric, before trying to convert:

但是由于您正在处理用户输入,因此在尝试转换之前,您最好检查任何数值的输入是否实际上是数字:

If Not IsNumeric(txtNumOfRooms.Text) OrElse Not IsNumeric(txtCost.Text) Then
    Throw new InvalidOperationException("Trying to convert a string to an integer")  
End If

cmd.Parameters.Add("@NUMBEROFROOMS", SqlDbType.Int).Value = CInt(txtNumOfRooms.Text)
cmd.Parameters.Add("@COST", SqlDbType.Int).Value = CInt(txtCost.Text)

回答by Ty Petrice

Are you preforming validation on the textbox to verify that the string is a valid int?

您是否在文本框上执行验证以验证字符串是否为有效的 int?

回答by Sachin

You need to convert TextBox's Text value i.e. stringinto Integervalue for the column which has intdataType, like this

您需要将 TextBox 的 Text 值 iestring转换Integer为具有intdataType的列的值,如下所示

cmd.Parameters.Add("@NUMBEROFROOMS", SqlDbType.Int).Value =CInt(txtNumOfRooms.Text);

回答by Steve

You have two parameters of type Int, but you set a text string as value.

您有两个 Int 类型的参数,但您将文本字符串设置为值。

cmd.Parameters.Add("@TYPEOFHOUSE", SqlDbType.NVarChar).Value = txtType.Text
cmd.Parameters.Add("@NUMBEROFROOMS", SqlDbType.Int).Value = Convert.ToInt32(txtNumOfRooms.Text)
cmd.Parameters.Add("@LOCATION", SqlDbType.NVarChar).Value = txtLocation.Text
cmd.Parameters.Add("@COST", SqlDbType.Int).Value = Convert.ToInt32(txtCost.Text)
cmd.Parameters.Add("@INFORMATION", SqlDbType.NVarChar).Value = txtInfo.Text
cmd.Parameters.Add("@PICTURE", SqlDbType.NVarChar).Value = txtLocation.Text

also you could use

你也可以使用

cmd.Parameters.AddWithValue("@TYPEOFHOUSE", txtType.Text)
cmd.Parameters.AddWithValue("@NUMBEROFROOMS", Convert.ToInt32(txtNumOfRooms.Text))
cmd.Parameters.AddWithValue("@LOCATION", txtLocation.Text)
cmd.Parameters.AddWithValue("@COST", Convert.ToInt32(txtCost.Text))
cmd.Parameters.AddWithValue("@INFORMATION", txtInfo.Text)
cmd.Parameters.AddWithValue("@PICTURE", txtLocation.Text)

Of course you need to be sure that these two textboxes contains an actual string that could be converted to int. Otherwise using CInt, Parse or Convert.ToInt32 raises an exception.
You could introduce a bit of error checking before filling the parameters collection using Int32.TryParseas in the following example.

当然,您需要确保这两个文本框包含可以转换为 int 的实际字符串。否则使用 CInt、Parse 或 Convert.ToInt32 会引发异常。
您可以在使用Int32.TryParse填充参数集合之前引入一些错误检查,如下例所示。

Dim numOfRooms as Integer
if Not Int32.TryParse(txtNumOfRooms.Text, numOfRooms) Then
    MessageBox.Show("Type a valid number")
    return
End If

cmd.Parameters.AddWithValue("@NUMBEROFROOMS", numOfRooms)