locked
Adding aparameters issue RRS feed

  • Question

  • User-240320001 posted

    I am trying to add a parameter to a SQL query with ASP.NET

    but I got the following error message every time, I would appreciate if someone can help me 

    The error message is Object reference not set to an instance of an object. and it makes reference to the following line of code

     cmd.Parameters.AddWithValue("@DLName", SqlDbType.Int).Value = 1

    Any help will be greatly appreciated

     Dim cmd As SqlCommand = Nothing
    
            Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
            Dim queryString As String = "SELECT DLEmail FROM [EDList] WHERE DLName = @DLName"
            cmd.Parameters.AddWithValue("@DLName", SqlDbType.Int).Value = 1
    
            Using connection As SqlConnection = New SqlConnection(connectionString)
                Dim command As SqlCommand = New SqlCommand(queryString, connection)
                connection.Open()
                cmd = New SqlCommand(queryString)
                cmd.Connection = connection
                Dim reader As SqlDataReader
                reader = cmd.ExecuteReader()
                While reader.Read()
                    Dim [to] = New MailAddress(reader("DLEmail").ToString())
                    message.Bcc.Add([to])
                End While
    
                smtp.Send(message)
                reader.Close()
                connection.Close()
            End Using
        End Sub

    Thursday, September 6, 2018 9:14 PM

Answers