Sunny Bhattacharjee
Sunny Bhattacharjee

Reputation: 107

app.config connection string does not work

this piece of code works fine....

Private Sub save()
        Dim con As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Chuttu VB\Projects\LIC\LIC.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

        Dim sql As New SqlClient.SqlCommand("INSERT INTO ProposerDetails " & _
                                            "VALUES (" & Convert.ToInt32(PolicyNumberTextBox.Text) & ",'" & NameTextBox.Text & "','" & AgeTextBox.Text & "','" & PhoneTextBox.Text & "','" & AddressTextBox.Text & "','" _
                                            & NomineeTextBox.Text & "','" & NomineeRelationTextBox.Text & "'," & PlanID() & ",'" & PolicyTermTextBox.Text & "','" & PremiumAmountTextBox.Text & "','" _
                                            & PremiumTypeComboBox.Text & "','" & SumProposedTextBox.Text & "','Date' )", con)

        MsgBox(sql.CommandText)
        con.Open()

        MsgBox(con.State.ToString)
        Dim i As Integer = sql.ExecuteNonQuery
        MsgBox(i.ToString)
        con.Close()
        sql.Dispose()
        con.Dispose()
        ToolStripStatusLabelMessage.Text = "Saved"
    End Sub

as soon as i change the connection string to the connection string from app.config it stops working(adding data to DB)

Private Sub save()
        Dim con As New SqlClient.SqlConnection(LIC.My.Settings.LICConnectionString)

        Dim sql As New SqlClient.SqlCommand("INSERT INTO ProposerDetails " & _
                                            "VALUES (" & Convert.ToInt32(PolicyNumberTextBox.Text) & ",'" & NameTextBox.Text & "','" & AgeTextBox.Text & "','" & PhoneTextBox.Text & "','" & AddressTextBox.Text & "','" _
                                            & NomineeTextBox.Text & "','" & NomineeRelationTextBox.Text & "'," & PlanID() & ",'" & PolicyTermTextBox.Text & "','" & PremiumAmountTextBox.Text & "','" _
                                            & PremiumTypeComboBox.Text & "','" & SumProposedTextBox.Text & "','Date' )", con)

        MsgBox(sql.CommandText)
        con.Open()

        MsgBox(con.State.ToString)
        Dim i As Integer = sql.ExecuteNonQuery
        MsgBox(i.ToString)
        con.Close()
        sql.Dispose()
        con.Dispose()
        ToolStripStatusLabelMessage.Text = "Saved"
    End Sub

NOTE: I get no errors.

Upvotes: 0

Views: 1766

Answers (2)

Andy Stannard
Andy Stannard

Reputation: 1703

Try changing LIC.My.Settings.LICConnectionString to ConfigurationManager.ConnectionStrings["LICConnectionString"].ConnectionString

Upvotes: 0

Etch
Etch

Reputation: 3054

Here is a short deal explanation.

This is how you use a connection string from the config file.

Dim sqlConn as SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString)

Here is a link to how to do a parameterized queries

Upvotes: 1

Related Questions