Parameterized Query which was not supplied.

Hi Experts,
I have a gridview that is bound to an objectdatasource. The gridview has 3 columns: ID, Title and SubTitle.

When I edit a row, I will get the following error if I leave the SubTitle textbox blank.
Parameterized Query '(@Title nvarchar(9),@SubTitle nvarchar(4000),@ID nvarchar(3))UPD' expects parameter @SubTitle, which was not supplied.

I will get a similar error if I leave the Title textbox blank.

How do I fix it?
Thanks


Imports System.Data.SqlClient
Imports System.Configuration
 
Public Class Books
    Public Shared Sub UpdateBooks(ByVal Title As String, ByVal SubTitle As String, ByVal ID As String)
        Dim connection As SqlConnection = StartConnection()
        Dim sSqlQuery As String = ""
        sSqlQuery = "UPDATE dxBooks SET Title = @Title, SubTitle = @SubTitle WHERE (ID = @ID)"
        Dim Cmd As New SqlCommand(sSqlQuery, connection)
        Cmd.Parameters.AddWithValue("@Title", Title)
        Cmd.Parameters.AddWithValue("@SubTitle", SubTitle)
        Cmd.Parameters.AddWithValue("@ID", ID)
        Cmd.ExecuteNonQuery()
        connection.Close()
    End Sub
End Class

Open in new window

noobe1Asked:
Who is Participating?
 
noobe1Connect With a Mentor Author Commented:
I solved the problem. All that is needed is to test if the value passes is an empty string. If yes, set the parameter's value to DBNull.Value

        If (SubTitle = String.Empty) Then
            Cmd.Parameters.AddWithValue("@SubTitle", DBNull.Value)
        Else
            Cmd.Parameters.AddWithValue("@SubTitle", SubTitle)
        End If


Imports System.Data.SqlClient
Imports System.Configuration
 
Public Class Books
    Public Shared Sub UpdateBooks(ByVal Title As String, ByVal SubTitle As String, ByVal ID As String)
        Dim connection As SqlConnection = StartConnection()
        Dim sSqlQuery As String = ""
        sSqlQuery = "UPDATE dxBooks SET Title = @Title, SubTitle = @SubTitle WHERE (ID = @ID)"
        Dim Cmd As New SqlCommand(sSqlQuery, connection)
        Cmd.Parameters.AddWithValue("@Title", Title)
                If (SubTitle = String.Empty) Then
            Cmd.Parameters.AddWithValue("@SubTitle", DBNull.Value)
        Else
            Cmd.Parameters.AddWithValue("@SubTitle", SubTitle)
        End If
        Cmd.Parameters.AddWithValue("@ID", ID)
        Cmd.ExecuteNonQuery()
        connection.Close()
    End Sub
End Class

Open in new window

0
 
prairiedogCommented:
How do you call UpdateBooks in your code?
0
 
GreymanMSCCommented:
You don't include the @ with the parameter names in the methods for System.Data.SqlClient.SqlParameters objects.
Imports System.Data.SqlClient
Imports System.Configuration
 
Public Class Books
    Public Shared Sub UpdateBooks(ByVal Title As String, ByVal SubTitle As String, ByVal ID As String)
        Dim connection As SqlConnection = StartConnection()
        Dim sSqlQuery As String = ""
        sSqlQuery = "UPDATE dxBooks SET Title = @Title, SubTitle = @SubTitle WHERE (ID = @ID)"
        Dim Cmd As New SqlCommand(sSqlQuery, connection)
        Cmd.Parameters.AddWithValue("Title", Title)
        Cmd.Parameters.AddWithValue("SubTitle", SubTitle)
        Cmd.Parameters.AddWithValue("ID", ID)
        Cmd.ExecuteNonQuery()
        connection.Close()
    End Sub
End Class

Open in new window

0
 
noobe1Author Commented:
prairiedog,
I specified UpdateBooks in the objectdatasource's UpdateMethod.

GreymanMSC,
I'm still getting the same errors.

I think the problem lies in the values that is passed to UpdateBooks. If I enter "ABC" for Title and "EFG" for SubTitle in the gridview, the code runs fine. If I enter "ABC" for Title and "" for SubTitle, I get the error.

Maybe the gridview does not pass any value for SubTitle when it is an empty string. Just a guess.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.