Can anyone show me a sample code to create a connection to a sql database and how I would output that information into a form. I can't seem to get any connections to work.
this needs to be in the web.config file
--------------------------------------
<appSettings>
<add key="ConnectionString" value="SERVER=VKSQL01;Max Pool Size=5000;Connection Lifetime=60;Application Name=ProjectMaintenance;Database=t2dvktest4;uid=sa;pwd=blackhook;" />
<add key="Timeout" value="30" />
</appSettings>
.vb file
-------------------
Public objConn As SqlConnection = New SqlConnection()
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
lbxClientSearch.Items.Clear()
Dim str As String
Dim search As String
Dim dsClient1 As DataSet = New DataSet()
str = "Select clientID, case when firstname = '' or firstname is null then SearchName else searchname+', '+ firstname end as searchName from tblClient where searchname like '%" & search & "%'"
objConn.ConnectionString = ConfigurationSettings.AppSettings("ConnectionString"
Try
objConn.Open()
Dim sdaClient As SqlDataAdapter = New SqlDataAdapter(str, objConn)
sdaClient.Fill(dsClient1, "tblClient"
Dim dt As DataTable = dsClient1.Tables(0)
lbxClientSearch.DataSource = dt.DefaultView
lbxClientSearch.DataTextField = "searchName"
lbxClientSearch.DataValueField = "clientID"
lbxClientSearch.DataBind()
objConn.Close()
Catch objException As Exception
Response.Write("<h1>An error has occurred with the Database.</h1>"
Response.Write("<h3>Please Contact your Intranet administrator.</h3>"
Response.Write("<hr>"
Response.Write("<li><b>Message:</b> " & objException.Message)
Response.Write("<li><b>Source:</b> " & objException.Source)
Response.Write("<li><b>Stack Trace:</b> " & objException.StackTrace)
Response.End()
End Try
If lbxClientSearch.Items.Count = 1 Then
lbxClientSearch.SelectedIndex = 0
SetFocus(btnCharge)
ElseIf lbxClientSearch.Items.Count > 1 Then
SetFocus(lbxClientSearch)
Else
SetFocus(txtClientSearch)
End If
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.