I am starting a new system and i have already stored the connection string in my web.config file. I now want to use that connection string in my web forms. How do i reference it.
This is what I have in my web.config file
<connectionStrings>
<add name="XX" connectionString="server=XX;UID=XX;PWD=XX;Database=XX" />
<add name="XXX" connectionString="server=XX;UID=XX;PWD=XX;Database=XX" />
</connectionStrings>
This a web form that i wish to use the connection string maybe in a using statement or something, without having to open and close the connection each time in each form.
Protected Sub btnSearchEmployee_Click(sender As Object, e As EventArgs) Handles btnSearchEmployee.Click
Dim conn As New SqlConnection("server=XX; Database= XX; Integrated Security = XX")
Dim cmd As New SqlCommand("SELECT * FROM EmployeeCodes WHERE (FirstName LIKE '%' + @firstname + '%') OR (Code = @code) ", conn)
cmd.Parameters.Add("@firstname", SqlDbType.VarChar).Value = txtSearchEmployee.Text
cmd.Parameters.Add("@code", SqlDbType.VarChar).Value = txtSearchEmployee.Text
Dim adapter As New SqlDataAdapter(cmd)
Dim tbl As New DataTable()
adapter.Fill(tbl)
txtName.Text = ""
txtSurname.Text = ""
txtIDNo.Text = ""
txtCostCentre.Text = ""
txtDepartment.Text = ""
txtClockNo.Text = ""
If tbl.Rows.Count() > 0 Then
txtName.Text = tbl.Rows(0)(5).ToString()
txtSurname.Text = tbl.Rows(0)(6).ToString()
txtIDNo.Text = tbl.Rows(0)(8).ToString()
txtCostCentre.Text = tbl.Rows(0)(8).ToString()
txtDepartment.Text = tbl.Rows(0)(8).ToString()
txtClockNo.Text = tbl.Rows(0)(1).ToString()
lblSearchEmployee.Visible = False
Else
lblSearchEmployee.Visible = True
End If
End Sub