HI i have this error null reference i don't know why

Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Configuration.ConfigurationSettings

Public Class Form1

Private ConName As New DataAccess


Private Sub cmdconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdconnect.Click
        Dim conn As String
        Dim m_server As String
        Dim m_db As String

        m_server = txtserver.Text
       m_db = txtdb.Text


       conn = "Datasource = & '" & m_server & "' &;Initial Catalog= '" & m_db & "';user id = sa;password = P@ssw0rd;"

      DataAccess.GetConnectionString(conn)






   End Sub

End Class

and i have this my app.config

 <connectionStrings>
  <add name ="Myconnname"
  connectionString="Persist Security Info = false;
  Data Source = jojo-pc;
  Initial Catalog= BOC_DB;
  User ID= sa;
  Password= P@ssw0rd;
 Integrated Security=SSPI;
 Trusted_Connection=TRUE;
 Application Name = TestDb"
 providerName="System.Data.SqlClient"/>
</connectionStrings>

and this is the function under the class called dataaccess

''' <summary>
    ''' Function to retrieve the connection from the app.config ''' </summary>
    ''' <param name="conName">Name of the connectionString to retrieve</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetConnectionString(ByVal conName As String) As String  
    'variable to hold our connection string for returning it        
    Dim strReturn As New String("")
         'check to see if the user provided a connection string name
         'this is for if your application has more than one connection string
         If Not String.IsNullOrEmpty(conName) Then
             'a connection string name was provided
             'get the connection string by the name provided
              strReturn = ConfigurationManager.ConnectionStrings(conName).ConnectionString
         Else
         'no connection string name was provided
             'get the default connection string
             strReturn = ConfigurationManager.ConnectionStrings("YourConnectionName").ConnectionString
         End If
         'return the connection string to the calling method
         Return strReturn
    End Function

Please guide im lost thanks

Recommended Answers

All 18 Replies

When you debug it what line is causing the null error? If is the line

strReturn = ConfigurationManager.ConnectionStrings("YourConnectionName").ConnectionString

the error will be because in the app.config the connection string is named "MyConnName" not "YourConnectionName".

i got the error on this line:

strReturn = ConfigurationManager.ConnectionStrings(conName).ConnectionString

it says that " use the new keyword to create an object instance.

: (

anyone here could help me?

Well On your config file you have a connection string called Myconname

Your function, GetConnectionString appears to take in the Name of the connection and look for it in your app config file..

Think about the code you are using - What is the String that you are passing in to that function?

I suspect you are modifying someone else's code with out understanding what it is you are doing...

DataAccess.GetConnectionString(conn)

Actually this was a tutorial from other site which i have read and i tried it unfortunately the person who post it is no longer connected to that site. i would not asked here if only that person is still connected. that's why i post it cause i don't know where i got wrong. and one thing that "DataAccess.GetConnectionString(conn)" i already used it. it's on my post at the very top. and one last thing people who joined here are looking for extra help,extra knowledge and guidance. if you think that every people here knows what they are posting then what is the used of forum?
forum = in which participants with common interests can exchange open messages.

Yes but instead of taking the time to analyse and understand the code you have just lashed it into your solution.

I gave you a rather large hint in my last two posts to what the issue is.

Let me Zero in further for you:

conn = "Datasource = & '" & m_server & "' &;Initial Catalog= '" & m_db & "';user id = sa;password = P@ssw0rd;" DataAccess.GetConnectionString(conn)

I will now spoon feed you what should be apparent if you bothered to analyse and understand the code before using it. Do you have a entry in your config app called Datasource="m_server";Initial Catalog="m_db";user id=sa; password=P@ssw0rd;? I think you'll find that you do not. Although you do have one called Myconname

People do not mind helping you but we are not here to program for you - It is through trial and error that we all have learned but only by understanding, analysing and following the code will you truely learn.. That's what debug mode is for.

I am not telling you to program it for me. i paste the code here in order to guide me or correct me. the app.config and the getconnectionstring is from tutorial site what i have added is the connect button i modify it by letting the user choose what the connection he/she like to connect. Let me Zero if for you too maybe YOU don't understand the code- as what the code trying to do on the app.config it is the default connection. the connectionstring function it is where if the user inputted a connection or not if the user didn't input any connection so the getconnectionstring will get the default connection that's why i am making a two textbox for the user input. m_server is a variable that is being assign to a txtserver.text and so with the m_db. Now, that's why i am posting it cause i need some enlightenment cause i got stuck on it. if you can look carefully on the app.config and connection string on the connect button are the same the only thing that they are different is the variable m_server and m_db for the user input.

if a person asked a question do you think he has the answer to his question?

If you think you know the answer then share it, if don't know anything then listen, all of this is a gift. if you think people who asked are dumbed then think of it,You were also just like that when you don't even know anything about it.

the yourconnectionstring there is the name of the connection name on my app.config name Myconname i have not change it when i post it. i will repost it for clarifications

Public Shared Function GetConnectionString(ByVal conName As String) As String

            Dim strReturn As New String("")



            If Not String.IsNullOrEmpty(conName) Then
                strReturn = ConfigurationManager.ConnectionStrings(conName).ConnectionString
            Else
                strReturn = ConfigurationManager.ConnectionStrings("MyconName").ConnectionString
            End If
            Return strReturn


End Function

no one?

Do you have the connectionstring you are trying to call in the .config file? Since you are providing a conneciton string name, the connection manager is trying to find the connectionstring with that name. The .config file you provided only has the "default" string, sort of speak. Have you tried calling the name of the default string to see if that returns null?

    DataAccess.GetConnectionString("Myconnname")

It looks like you are building a connection string as the name of the conection and then trying to find that in the config. Using the code above may help guide you a little bit :).

thanks maligui i will update you once i found something. thanks

hi maligui

at last i got it
DataAccess.GetConnectionString("txtserver.text")
I let the user input the connection name on the textbox and grab the data and pass it on my other class name called Server, this will hold the data for future reference especially during handling the connections.Now on my next step i will try to manipulate the
connection name by doing a loop and try to get the connection names at the app.config in order to display the connection names on the combo box so that the user will just choose the connections they want.

and by the way thanks a lot. cheers

If you go to MSDN's site: Storing and Retrieving Connection Strings, and go half way down the page to Example: Listing All Connection Strings. They show you how to get the result you are looking for.

thanks a lot i will update this thread and paste my code for corrections

thanks

Hi maligui

what i did is i make a loop of array to get the connectionstrings at the app.config
and it works fine.

by the way this is how i did it.

Me.cboserver.Items.Clear()

        Dim collect As ConnectionStringSettingsCollection

        Try
            collect = ConfigurationManager.ConnectionStrings
        Catch ex As Exception
            collect = Nothing
        End Try

        If collect IsNot Nothing Then

            For Each cs As ConnectionStringSettings In collect
                Me.cboserver.Items.Add(cs.Name) 'Getting all the names on the app.config
            Next cs
        End If
        collect = Nothing 
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.