Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2008, 09:28 AM
Authorized User
 
Join Date: Feb 2007
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:
        
Code:
Protected Sub Button1_Click(ByVal sender as Object, ByVal e as System.EventArgs) Handles Button1.Click
Code:
   Dim obj as Object
   Dim name as String
  obj=lstIPC.SelectedValue
  name = Convert.ToString(obj)
  moduleinteger = 2
  DisplayTable(name)
 
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:
       
Code:
    Dim SQL2 As String = "SELECT IPCName FROM IPCNAMES"
Code:
            Dim PubsConn2 As New SqlConnection(ConnStr2)
            Dim TitlesCmd2 As New SqlCommand(SQL2, PubsConn2)
            Dim Titles2 As SqlDataReader

            PubsConn2.Open()
            Titles2=TitlesCmd2.ExecuteReader()
            lstIPC.DataSource = Titles2
            lstIPC.DataBind()
            lstIPC.Items.Insert(0, new ListItem ("-- Choose an IPC --"))
            PubsConn2.Close()

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

 
Old January 23rd, 2008, 09:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there.. try with selecteditem, or selectedtext.. selectedvalue is used when you have values asociated to every item (like item, code)...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old January 23rd, 2008, 10:06 AM
Authorized User
 
Join Date: Feb 2007
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

Hi Gonzalo,
    Thanks for responding. I tried to place selecteditem in my code and received the same error and when I attempted to place selectedtext in it gave me the following error:
         'SelectedText' is not a member of 'System.Web.UI.WebControls.ListBox'.
    Could it have something to do with my declarations? Here they are just in case:
                          <%@ Page Language="VB" Debug="True" Strict="False" Explicit="True" Buffer="True"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data.sqlClient" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.Text" %>

    Also, could it have something to do with my conversion technique? Is there something better than
name = Convert.ToString(obj)? Thanks again.

 
Old January 23rd, 2008, 10:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi.. for a quick check, can you just go back to your original code (the one with the from error), and print the resulting sql string? (the one that has the error..)

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old January 23rd, 2008, 10:17 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You should use:

lstIPC.SelectedItem.Text

there is no SelectedText property.

Also, I would change this:
Dim obj as Object
Dim name as String
obj=lstIPC.SelectedValue
name = Convert.ToString(obj)

to

Dim name as String = lstIPC.SelectedValue or
Dim name as String = lstIPC.SelectedItem.Text

since, in either case, the return type is string you do not need to convert it.

The reason for your error, I imagine, is as Gonzalo orginally said. By Binding the list as you have you have probably only populated the string portion of the listitems and not the values.

To change that do something like:
lstIPC.DataTextField = "somecolumninyourdatasource"
lstIPC.DataValueField = "somecolumninyourdatasource"
lstIPC.DataSource = ds
lstIPC.DataBind()

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old January 23rd, 2008, 10:52 AM
Authorized User
 
Join Date: Feb 2007
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

Hi dparsons,
   I did as Gonzalo said which was bringing out a JS message box on the client side and my SQL statement is not receiving anything from the parameters, it is inputting "Select Col001 From " and that's it. So with my original code there is nothing being passed to the receiving method.
   I tried to do what you said in your message, which i already had the DataTextField and DataValueField declared as attributes within the asp.net section of my code but it was a good idea and safer to place them where I bind the data. But everything else is exactly as you said. Here is my new code:
          Protected Sub Button1_Click(ByVal sender as Object, ByVal e as System.EventArgs) Handles Button1.Click
 moduleinteger = 2
  Dim name as String = lstIPC.SelectedItem.Text
  DisplayTable(name)

End Sub
    However, when I run this I get the following error:
      System.NullReferenceException: Object reference not set to an instance of an object.
   Which points to the line:
       Dim name as String = lstIPC.SelectedItem.Text

   It's seems as though it should work. Any thoughts? TIA


 
Old January 23rd, 2008, 11:03 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

That leads me to believe that lstIPC.SelectedItem returns null.

This will depend on your program flow but if your page load looks like this:

Protected Sub Page_Load(sender as Object, e as EventArgs)
   DataBindList()
End Sub

It needs to be
Protected Sub Page_Load(sender as Object, e as EventArgs)
   If Not Page.IsPostBack Then
       DataBindList()
   End If
End Sub

Otherwise you will rebind your list on postback and all of the selections you have made in the list box will be gone, resulting in the Null Reference.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old January 23rd, 2008, 11:11 AM
Authorized User
 
Join Date: Feb 2007
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

That was it!! Thank you so much. I used to use that most times as a default but for some reason I strayed. I think I need to set up a few templates again. Thanks again. :-)

 
Old January 23rd, 2008, 11:12 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

=] Glad it worked out for you.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
convert a SQL Statement from MS Access to a SQL Corey Access 6 March 28th, 2007 12:33 PM
select statement with listbox item(where qnsID =" Astro VB How-To 2 August 31st, 2005 02:12 AM
populate listbox from SQL server Stanny Access 1 May 30th, 2005 08:11 AM
listbox/Textbox/sql Rudner VB How-To 0 March 9th, 2005 02:44 AM
How to use use ListBox in SQL Report abhiinbwir ADO.NET 0 July 13th, 2004 06:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.