Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEW to .NET

Status
Not open for further replies.

Wes98765

Programmer
Jul 31, 2002
135
US
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.

Thanks for any help


Wes
 
This is a sample search page that we use...

this needs to be in the web.config file
--------------------------------------
<appSettings>
<add key=&quot;ConnectionString&quot; value=&quot;SERVER=VKSQL01;Max Pool Size=5000;Connection Lifetime=60;Application Name=ProjectMaintenance;Database=t2dvktest4;uid=sa;pwd=blackhook;&quot; />
<add key=&quot;Timeout&quot; value=&quot;30&quot; />
</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()

search = Trim(txtClientSearch.Text)
search = Replace(search, &quot;'&quot;, &quot;''&quot;)

str = &quot;Select clientID, case when firstname = '' or firstname is null then SearchName else searchname+', '+ firstname end as searchName from tblClient where searchname like '%&quot; & search & &quot;%'&quot;
objConn.ConnectionString = ConfigurationSettings.AppSettings(&quot;ConnectionString&quot;)
Try
objConn.Open()

Dim sdaClient As SqlDataAdapter = New SqlDataAdapter(str, objConn)
sdaClient.Fill(dsClient1, &quot;tblClient&quot;)
Dim dt As DataTable = dsClient1.Tables(0)
lbxClientSearch.DataSource = dt.DefaultView
lbxClientSearch.DataTextField = &quot;searchName&quot;
lbxClientSearch.DataValueField = &quot;clientID&quot;
lbxClientSearch.DataBind()
objConn.Close()
Catch objException As Exception
Response.Write(&quot;<h1>An error has occurred with the Database.</h1>&quot;)
Response.Write(&quot;<h3>Please Contact your Intranet administrator.</h3>&quot;)
Response.Write(&quot;<hr>&quot;)
Response.Write(&quot;<li><b>Message:</b> &quot; & objException.Message)
Response.Write(&quot;<li><b>Source:</b> &quot; & objException.Source)
Response.Write(&quot;<li><b>Stack Trace:</b> &quot; & 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

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top