NullReferenceExecption was unhandled by User code

2019-09-03 16:52发布

问题:

I'm new to Visual studio 2013. i'm trying to develop a web application using vb.net. there is a scenario where i need to display data on webpage by fetching multiple data for different tables. i have a function where i'm trying to connect to my local database. below is the function

   Private Function GetData(query As String) As DataTable
Dim dt As New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("Data Source=  (LocalDB)\v11.0;AttachDbFilename=D:\Visual Studio Applications\TestDB\WebApplication1\App_Data\TestingDB.mdf;Integrated Security=True").ConnectionString
Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand(query)
            Using sda As New SqlDataAdapter()
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                sda.SelectCommand = cmd
                sda.Fill(dt)
            End Using
        End Using
        Return dt
    End Using
End Function

On line 3 in the function, in connection strings("") here i'm getting an error

"NullReferenceExecption was unhandled by User code An exception of type 'System.NullReferenceException' occurred in WebApplication1.dll but was not handled in user code"

when i check the properties of my project database, the connecting string was

Data Source=(LocalDB)\v11.0;AttachDbFilename="D:\Visual Studio Applications\TestDB\WebApplication1\App_Data\TestingDB.mdf";Integrated Security=True

If i use this connection string in the function, its is throwing syntax error because its has double quotes ("") in the connection string before and end of the Datadirectory ("D:\Visual Studio Applications\TestDB\WebApplication1\App_Data\TestingDB.mdf"). so due to that when i try placing this in my ConfigurationManager.ConnectionStrings ("connection string").ConnectionString its throwing syntax error (,')' expected).So i have removed those double quotes in my connection string but then its throwing NullReferenceExecption was unhandled by User code error.

In my web.config file the connection string is 'Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\TestingDB.mdf;Integrated Security=True' i tried using this as my connection string but no use still getting that NullReferenceExecption error.

I'm very confused and frustrated:( please help me with this.

回答1:

When you write

Dim constr As String = ConfigurationManager.ConnectionStrings("Data Source=  (LocalDB)\v11.0;AttachDbFilename=....").ConnectionString

you are asking to the Configuration Manager to find a connection string named in that way, of course this is the Value not the Name of the constring in your config.

So suppose that you have in your config a line like this

<connectionStrings>
    <add name="MyConString" connectionString="Data Source=...."  />
</connectionStrings>

then you call

Dim constr As String = ConfigurationManager.ConnectionStrings("MyConString").ConnectionString

to retrieve the value to pass to the SqlConnection constructor