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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
noobe1Author 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.