Good Morning,
I'm working on an ASP.NET 3.5 webforms application and have written the following code:
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
When I run the page, I receive the error A SQLParameter with @NSN is not contained by this SQLParameter Collection. I tried using apostrophes around the @NSN and @PartName but that does not work either and presents expression expected error.
How might I rectify the above code so that it references the @NSN and @PartName parameters correctly?
EDIT: New Code below
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
I now receive an error Object reference not set to an instance of an object referencing the add parameter parmNSN. Do I really need those to add paraameter statements since I've already set them equal to the text boxes below?
Thanks, Sid