当包含Convert.ToInt32(cmd.ExecuteScalar())时,我收到有关将nvarchar转换为int的错误
然而 - 我正在为它做的值被声明为INT,在我的存储过程中用作INT,并且是数据库中的INT。我不知道从
获取nvarchar的位置input
存储过程是......
Protected Sub btnCreateAudition_Click(sender As Object, e As EventArgs) Handles btnCreateAudition.Click
Protected Sub btnCreateAudition_Click(sender As Object, e As EventArgs) Handles btnCreateAudition.Click
Dim audition_ID As Integer = 0
Dim audition_Status As Integer = 0
Dim ins_ID As Integer = DropDownList1.SelectedValue
Dim ins_name As String = GetIns_Name(DropDownList1.SelectedValue)
Dim judge1 As Integer = DropDownList2.SelectedValue
Dim judge2 As Integer = DropDownList3.SelectedValue
Dim judge3 As Integer = DropDownList4.SelectedValue
Dim judge4 As Integer = DropDownList5.SelectedValue
Dim judge5 As Integer = DropDownList6.SelectedValue
Dim judgename1 As String = DropDownList2.SelectedValue
Dim judgename2 As String = DropDownList3.SelectedValue
Dim judgename3 As String = DropDownList4.SelectedValue
Dim judgename4 As String = DropDownList5.SelectedValue
Dim judgename5 As String = DropDownList6.SelectedValue
Dim auditionIDTest As Integer = 100001
Using con As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\BandDatabase.mdf;Integrated Security=True")
Using cmd As New SqlCommand("Create_Audition")
Using sda As New SqlClient.SqlDataAdapter()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@audition_date", Calendar1.SelectedDate)
cmd.Parameters.AddWithValue("@audition_location", txtauditionlocation.Text)
cmd.Parameters.AddWithValue("@audition_status", audition_Status)
cmd.Parameters.AddWithValue("@instrument_name", ins_name)
cmd.Parameters.AddWithValue("@ins_ID", ins_id)
cmd.Parameters.AddWithValue("@judge1", judge1)
cmd.Parameters.AddWithValue("@judge2", judge2)
cmd.Parameters.AddWithValue("@judge3", judge3)
cmd.Parameters.AddWithValue("@judge4", judge4)
cmd.Parameters.AddWithValue("@judge5", judge5)
cmd.Parameters.AddWithValue("@judgename1", judgename1)
cmd.Parameters.AddWithValue("@judgename2", judgename2)
cmd.Parameters.AddWithValue("@judgename3", judgename3)
cmd.Parameters.AddWithValue("@judgename4", judgename4)
cmd.Parameters.AddWithValue("@judgename5", judgename5)
cmd.Parameters.AddWithValue("@audition_IDTest", auditionIDTest)
cmd.Connection = con
con.Open()
'No error if below line is commented out, but DB isn't updated
audition_ID = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
End Using
End Using
End Sub
哪里可能将audition_ID指定为nvarchar类型?
答案 0 :(得分:0)
我看到你在这里声明instrument_name
参数为整数:
CREATE PROCEDURE [dbo].[Create_Audition]
...
@instrument_name INT,
...
但您使用以下代码行将值传递为字符串:
' declaration
Dim ins_name As String = GetIns_Name(DropDownList1.SelectedValue)
' in SqlCommand parameters
' this passing a string to an int SQL parameter, hence conversion failed
cmd.Parameters.AddWithValue("@instrument_name", ins_name)
由于instrument_name
预计为字符串,因此请将其数据类型更改为nvarchar
,然后您的查询应该有效:
CREATE PROCEDURE [dbo].[Create_Audition]
...
@instrument_name NVARCHAR,
...
...