Hi all, :)

I am creating a log in form and I connected to my database and when i run my form I keep geting this error

"NullRefrenceExeption was unhandled by user code"
Object reference not set to an instance of an object.

I'm getting the error in this line

Dim conStr As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\sony\Documents\Visual Studio 2010\Projects\SADA2\SADA2\App_Data\SADA2.mdf';Integrated Security=True;User Instance=True").ConnectionString)

Can any one help me with this plz

If you want to store your connection string in the app.config or web.config you normally place in the correct section and give it a name. You then use the ConfigurationManager to locate the connection based on the name. You haven't done that. In essence you have told it to look for an element named "Data Source=./SQL... etc" which doesn't exist.
Either declare it as a string in code or correctly place it in the config file and search for it by its name.

Dim conStr As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("your_name_goes_here").ConnectionString)

Hi hericles, Thanks for your replay

this is my code

Private Function ValidateLogin(ByVal username As String, ByVal password As String) As Boolean


        Dim boolReturn As Boolean = False
        Dim conStr As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\sony\Documents\Visual Studio 2010\Projects\SADA2\SADA2\App_Data\SADA2.mdf';Integrated Security=True;User Instance=True").ConnectionString)
        Dim cmdStr As String = "SELECT loginId, Password FROM [LoginTable]"
        Dim cmd As SqlCommand = New SqlCommand(cmdStr, conStr)
        Dim dr As SqlDataReader
        conStr.Open()
        dr = cmd.ExecuteReader()
        While dr.Read()


            If (username = dr("loginId").ToString()) And (password = dr("Password")) Then


                boolReturn = True
                Return boolReturn
            End If
        End While
        dr.Close()
        conStr.Close()
        Return boolReturn
    End Function

I'm still learning in ASP.NET so could you tell me plz how to do it?

thank,

Hi all,

I tried to declare my connection as hericles said , but i still have the same error could any one help me plz

Public Class LoginMain
    Inherits System.Web.UI.Page
    Public strErrorDescription As String
    Public constr2 As String

    Protected WithEvents LoginButton As Button

    Private Function ValidateLogin(ByVal username As String, ByVal password As String) As Boolean

        constr2 = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\sony\Documents\Visual Studio 2010\Projects\SADA2\SADA2\App_Data\SADA2.mdf';Integrated Security=True;User Instance=True"
        Dim boolReturn As Boolean = False
        Dim conStr As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr2").ConnectionString)
        Dim cmdStr As String = "SELECT loginId, Password FROM [LoginTable]"
        Dim cmd As SqlCommand = New SqlCommand(cmdStr, conStr)
        Dim dr As SqlDataReader
        conStr.Open()
        dr = cmd.ExecuteReader()
        While dr.Read()


            If (username = dr("loginId").ToString()) And (password = dr("Password")) Then


                boolReturn = True
                Return boolReturn
            End If
        End While
        dr.Close()
        conStr.Close()
        Return boolReturn
    End Function
    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
        If (ValidateLogin(Login1.UserName, Login1.Password)) Then


            e.Authenticated = True
            Response.Redirect("~/SLP.aspx")
        Else
            e.Authenticated = False
        End If
    End Sub


End Class

OK, if you have declared your connection string as a string you would access like this:

Dim connectionString As String()
    connectionString = 'your connection string'
    Dim conn As SqlConnection = New SqlConnection(connectionString)

If you have entered your connection string into the code then you don't need to use the configuration manager as the connection isn't located in the app.config file.

If you have added the connection string to th app.config file you will have a section that look like this:

<?xml version='1.0' encoding='utf-8'?>
    <configuration>
    <connectionStrings>
      <clear ></clear>
      <add name="Name" 
       providerName="System.Data.ProviderName" 
       connectionString="Valid Connection String;" ></add>
    </connectionStrings>
    </configuration>

Now you can use the configuration manager to locate the element with the name 'Name' (for example).
If you are having trouble with the idea of app.config files and the storing of connection strings I would suggest typing '.net app confg connection string' into google and reading the MSDN articles that come up.

Hope that helps,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.