早安載,錯誤:wtih參數名稱@myparm一個SQLParamenter不受此SqlParameter的收藏
我工作的一個ASP.NET 3.5 Web表單應用程序,已經寫了下面的代碼:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim connectionString As String = WebConfigurationManager.ConnectionStrings("Diel_inventoryConnectionString").ConnectionString
Dim con As New SqlConnection(connectionString)
Dim adapter1 As New SqlDataAdapter
adapter1.SelectCommand = New SqlCommand
adapter1.SelectCommand.CommandType = CommandType.StoredProcedure
adapter1.SelectCommand.CommandText = "PartSproc"
Dim parmNSN As New SqlParameter("@NSN", SqlDbType.NVarChar)
Dim parmName As New SqlParameter("@PartName", SqlDbType.NVarChar)
txtNSN.Text = adapter1.SelectCommand.Parameters("@NSN").Value
txtSearch.Text = adapter1.SelectCommand.Parameters("@PartName").Value
Dim dt As New DataTable()
adapter1.Fill(dt)
MySearch.DataSource = dt
MySearch.DataBind()
End Sub
當我運行該頁面時,我收到錯誤此SQLParameter集合不包含帶有@NSN的SQLParameter。我嘗試在@NSN和@PartName周圍使用撇號,但是這也不起作用,並提出表達式期望的錯誤。
我該如何糾正上述代碼,以便正確引用@NSN和@PartName參數?
編輯:下面
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim connectionString As String = WebConfigurationManager.ConnectionStrings("Diel_inventoryConnectionString").ConnectionString
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand("PartSproc", con)
cmd.CommandType = CommandType.StoredProcedure
Dim adapter1 As New SqlDataAdapter
Dim parmNSN As New SqlParameter("@NSN", SqlDbType.NVarChar)
Dim parmName As New SqlParameter("@PartName", SqlDbType.NVarChar)
adapter1.SelectCommand.Parameters.Add(parmNSN)
adapter1.SelectCommand.Parameters.Add(parmName)
adapter1.SelectCommand.Parameters("@NSN").Value = txtNSN.Text
adapter1.SelectCommand.Parameters("@PartName").Value = txtSearch.Text
Using con
Dim dt As New DataTable()
adapter1.SelectCommand = cmd
adapter1.Fill(dt)
MySearch.DataSource = dt
MySearch.DataBind()
End Using
End Sub
新代碼我現在收到沒有設置爲對象引用附加參數parmNSN的實例錯誤對象引用。我是否真的需要這些添加paraameter語句,因爲我已經將它們設置爲等於下面的文本框?
感謝, 希德
電話之前我需要使用除這些指令安德魯提供的那些或者是他們互換? – SidC