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