10

I keep getting this error :

The parameterized query '(@AdminEmail nvarchar(4000),@AdminPassword nvarchar(4000))SELECT' expects the parameter '@AdminEmail', which was not supplied.

Code:

Public Function AuthenticateAdmin() As Boolean
    Dim Success As Boolean

    Dim strConn As String
    strConn = ConfigurationManager.ConnectionStrings("HMVDb").ToString
    Dim conn As New SqlConnection(strConn.ToString())

    Dim cmd As New SqlCommand("SELECT * FROM Admin WHERE AdminEmail=@AdminEmail AND Adminpassword=@Adminpassword", conn)
    cmd.Parameters.AddWithValue("@AdminEmail", EMail)
    cmd.Parameters.AddWithValue("@AdminPassword", Password)

    Dim da As New SqlDataAdapter(cmd)

    Dim ds As New DataSet

    conn.Open()
    da.Fill(ds, "Admin")
    conn.Close()

    If ds.Tables("Admin").Rows.Count > 0 Then

        Dim aemail As String = ds.Tables("Admin").Rows(0).Item("AdminEmail")
        Dim apass As String = ds.Tables("Admin").Rows(0).Item("AdminPassword")
        Dim aid As Integer = ds.Tables("Admin").Rows(0).Item("AdminID")
        Dim aname As String = ds.Tables("Admin").Rows(0).Item("AdminName")

        If EMail = aemail And Password = apass Then
            ID = aid ' Shopper ID that identify Ecader
            Name = aname
            Success = True 'Shopper is authenticated
        Else
            Success = False 'Authentication fail
        End If
    End If


    'Return the authentication result to calling program
    Return Success
End Function
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
saravanan
  • 101
  • 1
  • 1
  • 3
  • 5
    Please don't add noise to your question such as *Please help, it's urgent*. It doesn't bring much value to it. – Darin Dimitrov Jul 21 '11 at 15:22
  • possible duplicate of [The parameterized query expects the parameter which was not supplied](http://stackoverflow.com/questions/3865982/the-parameterized-query-expects-the-parameter-which-was-not-supplied) – Gopi Jun 01 '15 at 17:16

3 Answers3

42

Your @AdminEmail variable EMail is null. You cannot pass a null on a required parameter. Use DBNull.Value.

When using null, you are informing Sql Server that you are omitting the parameter. This can be useful for an optional parameter with a default value, but causes an error for a required parameter.

I recommend that you use always use a utility function when passing a value to a command parameter.

For example:

public static object GetDataValue(object value)
{
   if(value == null)
   {
       return DBNull.Value;
   }

   return value;
}

and then use

cmd.Parameters.AddWithValue("@AdminEmail", GetDataValue(EMail))
Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101
1

Is it possible that the EMail property is null (Email is Nothing)? I think you might get that error in that case. Be sure that EMail = String.Empty or EMail = "" before you set your parameter value.

Edit: Or as another answer suggests, you can send DBNull.Value instead if you actually want nulls in your database.

TKTS
  • 1,261
  • 1
  • 11
  • 17
1

Step through your code and see what the value of Email and Password are. Chances are they are null.

Jack
  • 8,851
  • 3
  • 21
  • 26