I have created a windiws forms program in visual studio. I am using a local mdf database file. I have also imported the same database inte my sql server. I hav altered the connectionstring in my.settings, But when I try to connect to the sql database the debugging just shuts down with noe errors. The codde I use to set the connectionstring is:

 DS2 = "Data Source=" + TextBox1.Text + ";Network Library=DBMSSOCN ;Initial Catalog=" + TextBox2.Text + ";User ID=" + TextBox3.Text + ";Password=" + TextBox4.Text + ""
            connetionString = DS2

In settings.vb I have entered this code:

Namespace My

    'This class allows you to handle specific events on the settings class:
    ' The SettingChanging event is raised before a setting's value is changed.
    ' The PropertyChanged event is raised after a setting's value is changed.
    ' The SettingsLoaded event is raised after the setting values are loaded.
    ' The SettingsSaving event is raised before the setting values are saved.
    Partial Friend NotInheritable Class MySettings


        Public WriteOnly Property RuntimeConnectionString() As String
            Set(ByVal value As String)
                My.Settings("QAConnectionString") = value

            End Set
        End Property

        Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded

        End Sub
    End Class
End Namespace

Then in my main form I use this code to set det connectionstring:

 My.Settings.RuntimeConnectionString = Start.DS2

I have done this before, but then I just used 2 databases in sql server and not one file and 1 server.

Anyone who has experienced this problem before?

Is your code wrapped in a try/catch block? You say there are no errors but I have yet to come across a database connection issue that didn't provide an error message to a try/catch block.
Alternatively, debug your code and step through line by line and find the exact point it fails at.

I changed my code to:

 Try
            My.Settings.RuntimeConnectionString = Start.DS2
        Catch ex As Exception
            MessageBox.Show("Dette fungerer ikke som det skal. det er noe feil." & vbNewLine _
             & "i Form1" & vbNewLine & vbNewLine _
            & "Reason for error:" & vbNewLine _
            & ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

But no exceptions was catched.

The start.DS2 it referes to is like this:

Imports System.Data.SqlClient

Public Class Start
    Inherits System.Windows.Forms.Form

    Dim connetionString As String
    Public cnn As SqlConnection
    Public DS As String
    Public DS2 As String
    Dim Servadr As String = "127.0.0.1,1433"
    Dim Servname As String = "SAMPLEDATABASE"
    Dim Username As String = "User1"
    Dim Passw As String = "user1"
    Dim tempAdr As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SampleDatabase.mdf;Integrated Security=True;Connect Timeout=30"

    Private Sub Start_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        TextBox1.Text = Servadr 'tempAdr
        TextBox2.Text = Servname
        TextBox3.Text = Username
        TextBox4.Text = Passw

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

            DS2 = "Data Source=" + TextBox1.Text + ";Network Library=DBMSSOCN ;Initial Catalog=" + TextBox2.Text + ";User ID=" + TextBox3.Text + ";Password=" + TextBox4.Text + ""
            connetionString = DS2

        cnn = New SqlConnection(connetionString)
        Dim MainF As New Form1
        Try
            cnn.Open()
            'MsgBox("Tilkoblingen er vellykket! ")
            cnn.Close()
            MainF.Show()
        Catch ex As Exception
            MsgBox("Tilkoblingen misslyktes vennligst sjekk påloggings informasjonen og prøv igjen! ")

        End Try

        'Dim VelgK As New VelgKunde
        'VelgK.Show()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class

The Messages in the MsgBoxes are in Norwegian.

The tempadr varable is the local database.
I have tested the connection properties and I have no problem with the connection to the database

Edited 2 Years Ago by johmolan

Connection string alaways delimits with a ";". You did not put ";" after ";Password=" + TextBox4.Text + "(put here a ';')". It could be solve your problem.

Thanks for trying Santanu but that did not helt either.
Even though I tell the form to load the data from the sql-server it continues to load tha data from the local databasefile.
I have med a second connection string in settings but it still does not use this even if I tell it to.

I do not know what happend but now it does not shut down anymore but it still refuses to get the data from the sql-database. Though I have set the connectionstring to get the data from the sql-database it only loads the data from the local databasefile. It is the first time I use a local file, but as I see it it still uses an connectionstring and it still should be possible to alter this programmaly.

I've had problems like this before myself.
This usually happens if you also use a strongly typed dataset, in which case you have to manully change the generated code for the dataset so that it uses the "correct" connectionstring.

The best solution would be to drop the local database and dataset and all connectionstring references and then redo it using the sql server.
It's the quickest way instead of spending hours of fiddling with the code.

This article has been dead for over six months. Start a new discussion instead.