Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Sadly, I don't 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.

 

I'm trying to get a listbox to populate with some SQL data. When I run it, it always stops at "myConnection.Open" with an error. Here's 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.

Posted

Here is an example from some working 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 doesn't send you on the right path

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...