1

Coming from a web based background, I don't have much experience developing and deploying .NET desktop applications and would like some help understanding the process of connecting a deployed win form app on a client machine to a local instance of SQL server.

I have created a test form application where I am able to configure my connection string at run time via a win form with textbox's for DBname, Server, UserID, and Password.. and it works fine (as in I can pull data from whichever server/db I set) when I run in debug. To test this out on a client machine (my other PC), I used InstallShield with VS13 and created an install package. I created a backup of the database that was running locally on my development machine, and recreated it on my client machine that has SQL server installed. I installed the the application, and ran the configuration form..Unfortunately though when I run on the client machine, I get an error once the code executes the 'Change connection string' function - "Object ref not set to an instance.." I am curious as to what the possible issues could be, and would like advice on how to troubleshoot, or suggestions if their is a better and more preferred way to go about installing an application on a client machine and get it to talk to the clients local SQL server.

Many thanks in advance!

------Code------ ** Please note all the message box's are for me to isolate where the code is breaking when I run it as a packaged exe.

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    Try

        Dim Con As New StringBuilder("Data Source=")
        Con.Append(txtServer.Text)
        Con.Append(";Initial Catalog=")
        Con.Append(txtDBName.Text)
        Con.Append(";User ID=")
        Con.Append(txtUserID.Text)
        Con.Append(";Password=")
        Con.Append(txtPass.Text)
        Con.Append(";Integrated Security=SSPI;")
        Dim strCon As String = Con.ToString()
        MessageBox.Show("About to call the update xml method. Server: " & txtServer.Text & " DB: " & txtDBName.Text & " User: " & txtUserID.Text & "Password: " & txtPass.Text)
        updateConfigFile(strCon)
        MessageBox.Show("Made it out of updateConfigFile Method")

        Dim Db As New SqlConnection()


        ConfigurationManager.RefreshSection("connectionStrings")
        Db.ConnectionString = ConfigurationManager.ConnectionStrings("connex").ToString()

        Dim da As New SqlDataAdapter("select * from TestTable", Db)
        Dim dt As New DataTable()
        da.Fill(dt)
        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "TestColumn"
    Catch ex As Exception
        MessageBox.Show(ConfigurationManager.ConnectionStrings("con").ToString() + ".This is invalid connection", "Incorrect server/Database")
    End Try




End Sub


Public Sub updateConfigFile(con As String)
    Try
        MessageBox.Show("We are in updateConfigFileMethod")
        Dim XmlDoc As New XmlDocument()
        MessageBox.Show("About to load config file")
        XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        For Each xElement As XmlElement In XmlDoc.DocumentElement
            If xElement.Name = "connectionStrings" Then
                'setting the coonection string
                xElement.FirstChild.Attributes(2).Value = con
            End If
        Next
        MessageBox.Show("XML Created, about to save config file. Connnection string: " & con)
        XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try

End Sub
user3288174
  • 13
  • 1
  • 3
  • you might also consider line breaks and paragraphs in the future – Ňɏssa Pøngjǣrdenlarp Jan 07 '15 at 14:40
  • It depends, it could be an issue with code or it could be the installation itself; we can't tell with little info you have provided... – Trevor Jan 07 '15 at 14:47
  • The code doesn't throw an error when I run in visual studio. It only breaks once its been packaged. That is why I didn't post the code in the first place. I'll post the code though – user3288174 Jan 07 '15 at 15:41
  • @Plutonix - The linked article you posted is for configuring connection on install. I have the limited version of install shield and am unable to have that as an option to the user. – user3288174 Jan 07 '15 at 15:50
  • 1
    Did you package your config file? As "yourApp.exe.config" or you packaged "app.config"? – T.S. Jan 07 '15 at 15:54
  • @user3288174 can you post the full stack trace of the error instead of just the error message? – Bill Dinger Jan 07 '15 at 15:54
  • On another note - it really hurts my eye to see how you build your conn string. What about doing it this way: `string connStr = String.Format("Data Source={0};Initial Catalog={1};User ID=;Password=;Integrated Security=SSPI;", txtServer.Text, txtDBName.Text, txtUserID.Text, txtPass.Text);` – T.S. Jan 07 '15 at 15:58
  • It was packaged as 'yourapp.exe.config' . I guess maybe that could be the problem? Maybe looking in the wrong place to set? Also, I agree, using string.format is much better. – user3288174 Jan 07 '15 at 16:32
  • The definition is there, just scroll down. – user3288174 Jan 07 '15 at 20:43
  • I am not entirely sure but I think, This is the line with issues `xElement.FirstChild.Attributes(2).Value = con`. Now, why don't you use config namespace for deal with connection strings as shown here: http://stackoverflow.com/a/1312739/1704458 , only use `ConfigurationManager.OpenExeConfiguration`. Don't do this XML stuff – T.S. Jan 07 '15 at 21:15

0 Answers0