SQL Connection Error

tekgod01

Member
Joined
Sep 7, 2003
Messages
23
Location
Indiana
Sadly, I dont get to mess with Visual Basic very much and so my brain likes to rust. So when I have to come back to it about once a year, I have to relearn everything. Yay.

Im trying to get a listbox to populate with some SQL data. When I run it, it always stops at "myConnection.Open" with an error. Heres what I got so far. Am I missing something?

Public Class fmMain
Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection = New SqlConnection("server=home;database=dbRawMaterials")
Dim myDataAdapter As New SqlDataAdapter
Dim myDataSet = New DataSet

---

Private Sub fmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myDataAdapter.SelectCommand = New SqlCommand
myDataAdapter.SelectCommand.Connection = myConnection
myDataAdapter.SelectCommand.CommandText = "SELECT PartNumber FROM tblRawMaterials"
myDataAdapter.SelectCommand.CommandType = CommandType.Text
myConnection.Open()
myDataAdapter.SelectCommand.ExecuteNonQuery()
myDataAdapter.Fill(myDataSet, "PartNumber")
myConnection.Close()
lstParts.DataSource = myDataSet
End Sub

---

Any help would be appreciated. Thank you.
 
This is all I get:

An unhandled exception of type System.Data.SqlClient.SqlException occurred in system.data.dll

Additional information: System error.
 
Here is an example from some working code

Code:
        Try
            oConn = New SqlClient.SqlConnection("Server=" & cmbSQLServer.SelectedItem & ";Database=" & _
            cmbDatabase.SelectedItem & ";uid=" & txtSQL.Text & ";pwd=" & txtSQLPass.Text & ";")
            oConn.Open()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "An error has occurred")
            Exit Sub
        End Try
        sSQL = "Select Top 1 * FROM " & lstTables.SelectedItem
        Try
            cmd = New SqlClient.SqlCommand
            cmd.CommandText = sSQL
            cmd.Connection = oConn
            da = New SqlClient.SqlDataAdapter(cmd)
            da.Fill(dt)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "An error has occurred!")
            oConn.Close()
            Exit Sub
        End Try

Let me know if that doesnt send you on the right path
 
Back
Top