locked
Not declared error RRS feed

  • Question

  • User-1793215261 posted

    Hello

    Part of the code I have in my register.aspx.vb file which relates to salting/hashing looks like this:

    Imports Microsoft.AspNet.Membership.OpenAuth
    Imports System
    Imports System.Linq
    Imports System.Web
    Imports System.Web.UI
    Imports System.Data.OleDb
    Imports System.Security.Cryptography
    Imports System.Text
    Imports System.Web.Helpers
    
    Private Sub CreateAccount(ByVal username As String, ByVal password As String, ByVal email As String)
            Dim hashedPassword As String = Crypto.HashPassword(password)
    
            Using connection As New OleDbConnection("connectionString")
    
                Using cmd As New OleDbCommand("INSERT INTO university (username, strEmail, hashed) VALUES (@username, @strEmail, @hashed)", connection)
                    cmd.Parameters.AddWithValue("@username", username)
                    cmd.Parameters.AddWithValue("@strEmail", email)
                    cmd.Parameters.AddWithValue("@hashed", hashedPassword)
    
                    connection.Open()
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        End Sub

    I am getting an error on the following line:

    Dim hashedPassword As String = Crypto.HashPassword(password)

    with 'Crypto' underlined (not declared) because 'hashedPassword' is not a String. What should that line of code be, please?

    Thank you.

    Friday, November 16, 2018 11:40 AM

All replies

  • User753101303 posted

    Hi,

    From a quick look it seems System.Web.Helpers.Crypto.HashPassword is expected to return a string ands hashedPassword is a string. So I'm not sure to get which error message you have.

    You tried to compile the code in case it would still be about an error in older code ? If you have an error give the exact error message or you have also a compiler error number. It would allow to make 100% sure about the error message you see.

    You can also have a look at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/ for C# (but you should have the same for VB or I would expect the same error numbers anyway) which may give some more explanation so that you can understand what is causing the error.

    Friday, November 16, 2018 2:10 PM
  • User-1793215261 posted

    Thanks for your reply, Patrice.

    The blue line under 'Crypto' in register.aspx.vb has now disappeared without making any code changes after I restarted PC and relaunched VS2017. However, when I look at the home page (index.asp) in my browser and click the 'Register' link, this is what I get:

    Compilation Error 
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

     Compiler Error Message: BC30451: 'Crypto' is not declared. It may be inaccessible due to its protection level.

    This line:

    Line 73:         Dim hashedPassword As String = Crypto.HashPassword(password)

    is highlighted in red.

    When it refers to 'compilation error', I don't really understand that, because I would normally compile it in Command Prompt or PowerShell and I haven't done that yet (I would only do that when I thought there were no errors). Either way, there seems to be something wrong with that line.

    Thanks again

    Friday, November 16, 2018 3:10 PM
  • User-1793215261 posted

    When I compile my project in PowerShell, this is what I get:

    C:\Users\Steve\Documents\Visual Studio 2017\DimaFinal\register.aspx.vb(9): warning BC40056: Namespace or type specified in the Imports 'System.Web.Helpers' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.

    C:\Users\Steve\Documents\Visual Studio 2017\DimaFinal\register.aspx.vb(73): error BC30451: 'Crypto' is not declared. It may be inaccessible due to its protection level.
    error ASPPARSE: C:\Users\Steve\Documents\Visual Studio 2017\DimaFinal\register.aspx.vb(73): error BC30451: 'Crypto' is not declared. It may be inaccessible due to its protection level.
    PS C:\>

    Thanks for any help.

    Friday, November 16, 2018 3:21 PM
  • User475983607 posted

    The System.Web.Helpers namespace is used in Web Matrix/ASP.NET Web Pages.  

    Imports System.Web.Helpers

    Are you building a Web Matrix site?   

    Anyway, the compiler error is telling you that the project does contain the System.Web.Helper assembly.  Perhaps, use NuGet to install the necessary packages.  

    https://www.nuget.org/packages/Microsoft.AspNet.WebHelpers/

    Friday, November 16, 2018 3:38 PM
  • User-1793215261 posted

    Thanks, mgebhard,for replying.

    No, I am just using a normal Web Forms design. Do you mean that I need to install certain NuGet packages if I use Imports System.Web.Helpers\

    Thanks

    Friday, November 16, 2018 3:51 PM
  • User475983607 posted

    Bluenose

    No, I am just using a normal Web Forms design. Do you mean that I need to install certain NuGet packages if I use Imports System.Web.Helpers\

     System.Web.Helpers is part of ASP.NET Web Pages 3.2 as documented in the link.  

    My best guess is you found a code snippet somewhere on the internet and copied code to your project without taking the time to understand the code or if the code is even compatible with your project.

    The error message is telling you that the compiler could not find Crypto; is not declared.   

    C:\Users\Steve\Documents\Visual Studio 2017\DimaFinal\register.aspx.vb(73): error BC30451: 'Crypto' is not declared. It may be inaccessible due to its protection level.
    error ASPPARSE: C:\Users\Steve\Documents\Visual Studio 2017\DimaFinal\register.aspx.vb(73): error BC30451: 'Crypto' is not declared. It may be inaccessible due to its protection level.

    This is where reading the reference docs is very helpful.   Crypto is a class in the System.Web.Helpers namespace.   Logically, the project is missing the System.Web.Helpers library.  Since NuGet is how we install libraries in VS, you can try installing the library and see if that solves the error.  I assume it will work but it might not.  Again refer to the documentation.  Keep in mind that you might need other libraries as well.

    Friday, November 16, 2018 4:17 PM
  • User-1793215261 posted

    I will take a look at the link you kindly posted and install the Web Helpers package - hopefully that will do the trick.

    Thanks again.

    Friday, November 16, 2018 4:31 PM
  • User-1793215261 posted

    I haven't been able to install the NuGet packages yet - I am getting an error saying 'Default' not found, so I am still working on that.

    In the meantime, when I remove the line that was generating an error, that is

    Dim hashedPassword As String = Crypto.HashPassword(password)

    and replace it with:

    Dim Crypto As String
    
            Crypto = Crypto.HashPassword(password)

    I still get an error on this line:

    Dim hashedPassword As String = Crypto.HashPassword(password)

    with 'Crypto' not declared. But I have deleted that line (not only commented it out).

    Thanks for any guidance.

    Sunday, November 18, 2018 6:21 PM
  • User753101303 posted

    If you want to use Crypto you could try :

    Dim hashedPassword As String = System.Web.Helpers.Crypto.HashPassword(password)

    You still may miss a reference and it seems the package doesn't install? The exact error message could help.

    As you are using Web Forms another option could be to use what ASP.NET offers out of the box. You could use https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project which provide an API to handle that for you.

    Monday, November 19, 2018 7:49 AM
  • User-1793215261 posted

    Hello Patrice

    Thank you again.

    I will try your suggested

    Dim hashedPassword As String = System.Web.Helpers.Crypto.HashPassword(password)

    later this morning and post back and open a new thread concerning NuGet.

    Cheers

    Monday, November 19, 2018 10:08 AM
  • User-1793215261 posted

    Hello Patrice

    When I use your suggested line of code

    Dim hashedPassword As String = System.Web.Helpers.Crypto.HashPassword(password)

    I get the following Compilation Error:

    BC30456: 'Helpers' is not a member of 'Web'.

    At the top of my code, I have:

    Imports System.Web.Helpers

    I am not sure if you are able to shed any light on this problem, but at least the 'Crypto' is not declared error has gone.

    Regards

    Tuesday, November 20, 2018 6:03 PM
  • User753101303 posted

    The first step would be to install the package (works just fine here). It seems that for some reason on your side, the project file can't be changed (for me I can reproduce your error if I unload the project file first, or do you have some other project whose project file is unloaded or maybe somewhere and old proj file which is not used any more ?).

    BTW what if you try to create a new project and try to install the package ? This is to find if this is something wide or just a problem on this particular project.

    Tuesday, November 20, 2018 6:39 PM
  • User-893317190 posted

    Hi Bluenose,

    System.Web.Helper is in the dll Microsoft.AspNet.WebPages, you could install it using nuget.

    If you couldn't fix your nuget, you could also find it in a MVC application.

    You could create a c# or vb mvc application and add reference to the newly created mvc application's  Microsoft.AspNet.WebPages dll.

    In your newly created vb or c# mvc application,click its reference, find System.Web.Helper, find its path property at the bottom of visual studio and then  your old project could make reference to it.

     

    Best regards,

    Ackerly Xu

    Wednesday, November 21, 2018 6:04 AM
  • User-1793215261 posted

    Thanks for your replies.

    I have temporarily lost connection, but will try your suggestions as soon as it is r4stored and post back.

    Thanks

    Wednesday, November 21, 2018 10:44 AM