View Single Post
  #1 (permalink)  
Old January 23rd, 2008, 09:28 AM
toddw607 toddw607 is offline
Authorized User
Join Date: Feb 2007
Location: Binghamton, NY, USA.
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to toddw607 Send a message via Yahoo to toddw607
Default Using listbox element for SQL Statement

I'm trying to take a selected element from a listbox and add it into a SQL Statement. Here is the code:
Protected Sub Button1_Click(ByVal sender as Object, ByVal e as System.EventArgs) Handles Button1.Click
   Dim obj as Object
   Dim name as String
  name = Convert.ToString(obj)
  moduleinteger = 2
End Sub

Public Sub DisplayTable(ByVal name as String)

   Dim SQL As String = "SELECT Col001 FROM " & name

     The listbox is loading fine but when I click the button it gives me the following error:
       Incorrect syntax near 'FROM'.

   I've tried to send a normal String through as a parameter and that works fine. The issue is that the parameter "name" is not being recognized as a string. The elements within the listbox are pulled from SQL Server DB table and the elements are defined as varchar(50) so I know that's not the problem. Also here is how I populated the listbox in the page_load method:
            Dim PubsConn2 As New SqlConnection(ConnStr2)
            Dim TitlesCmd2 As New SqlCommand(SQL2, PubsConn2)
            Dim Titles2 As SqlDataReader

            lstIPC.DataSource = Titles2
            lstIPC.Items.Insert(0, new ListItem ("-- Choose an IPC --"))

     Can anyone give me an idea of why this is giving me the error? TIA.

Reply With Quote