Solved

Error: Type 'ListItem' is not defined.

Posted on 2004-08-04
11
1,288 Views
Last Modified: 2007-12-19
Hi There,

I have 2 listboxes that are populates from as DB when the page is loaded, I am trying to add a default item " lstStates.Items.Insert(0, new ListItem("By State","0"))" and I get the error Type 'ListItem' is not defined." The dropdownlists work fine I just can't seem to add a default value into it when the page loads.

What am I doing wrong?

Sean

Protected WithEvents lstStates As System.Web.UI.WebControls.ListBox
    Protected WithEvents lstCategorie As System.Web.UI.WebControls.ListBox

Dim CategoryID as Integer  


  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
   
      CategoryID = Cint(request.querystring("CategoryID"))
    If Not Page.IsPostBack() Then
        lstStates.DataSource = BindStates
        lstStates.DataValueField = "LocationID"
        lstStates.DataTextField = "Location"
        lstStates.Items.Insert(0, new ListItem("By State","0"))
        lstStates.SelectedIndex = 0
        lstStates.DataBind()
           
        lstCategorie.DataSource = BindCategories
        lstCategorie.DataValueField = "CategoryID"
        lstCategorie.DataTextField = "CategoryDesc"
       ' lstCategorie.Items.Insert(0, new ListItem("By Category","0"))
         lstCategorie.SelectedIndex = 0
        lstCategorie.DataBind()
       
      BindTheData(CategoryID)
    End If
  End Sub
0
Comment
Question by:bullrout
  • 5
  • 4
  • 2
11 Comments
 
LVL 7

Expert Comment

by:mdamico
Comment Utility
Try adding

Imports System.Web.UI.WebControls
0
 
LVL 7

Expert Comment

by:mdamico
Comment Utility
Or just do it inline:

lstStates.Items.Insert(0, new System.Web.UI.WebControls.ListItem("By State","0"))
0
 
LVL 5

Author Comment

by:bullrout
Comment Utility
Hi There,

I already had the import statement, I tried the code that you posted and nothing happens not even an error? The menu seems to still load no problem.

Any other ideas?

imports system
imports system.web
imports System.Configuration
imports system.web.ui
Imports System.Web.UI.WebControls
imports system.data
imports system.data.sqlclient
imports System.IO
imports system.data.sqlclient.sqldatareader

Namespace Irrigation  
Public Class CommonFunctions
Inherits System.web.ui.page
'Inherits System.Web.UI.UserControl
Protected WithEvents lstStates As ListBox
Protected WithEvents lstCategorie As Listbox
public txtlocationselection as TextBox

'***************************************************
'load the page and then populate the listmenus
'***************************************************

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
     If Not Page.IsPostBack() Then
        lstStates.DataSource = BindStates
        lstStates.DataValueField = "LocationID"
        lstStates.DataTextField = "Location"
        lstStates.Items.Insert(0, new System.Web.UI.WebControls.ListItem("By State","0"))
        lstStates.SelectedIndex = 0
        lstStates.DataBind()
           
        lstCategorie.DataSource = BindCategories
        lstCategorie.DataValueField = "CategoryID"
        lstCategorie.DataTextField = "CategoryDesc"
        lstCategorie.Items.Insert(0, new ListItem("By Category","0"))
         lstCategorie.SelectedIndex = 0
        lstCategorie.DataBind()
    End If

  End Sub
0
 
LVL 7

Expert Comment

by:mdamico
Comment Utility
Have the BindStates function insert the ("By State","0") row to the datatable , then

        lstStates.DataSource = BindStates
        lstStates.DataValueField = "LocationID"
        lstStates.DataTextField = "Location"
        lstStates.DataBind()
        lstStates.SelectedIndex = 0
0
 
LVL 5

Author Comment

by:bullrout
Comment Utility
Hi There,

I actually used some code that you posted previously and I am not sure where to place the code? I am still only learning so you think you could help me out with some code?

Sean



Public Shared Function BindCategories() as SqlDataReader

    Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
      Const strSQL as String = "SELECT CategoryID,CategoryDesc AS CategoryDesc " & _
                                          "FROM tblProductCategories ORDER BY CategoryDesc"
    Dim myCommand as New SqlCommand(strSQL, myConnection)
    myConnection.Open()
    Dim objDR1 as SqlDataReader
    objDR1 = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    Return objDr1
End Function
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Expert Comment

by:mdamico
Comment Utility
Using a datatable instead of sqldatareader, so row can be added more easily...


  Public Shared Function BindCategories() As DataTable

    Dim myConnection As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("connectionString"))

    Const strSQL As String = "SELECT CategoryID,CategoryDesc AS CategoryDesc " & _
                                  "FROM tblProductCategories ORDER BY CategoryDesc"
    Dim da As SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection)
    myConnection.Open()
    Dim dt As DataTable
    da.Fill(dt)
    myConnection.Close()

    Dim row As DataRow
    row = dt.NewRow
    row("CategoryID") = 0
    row("CategoryDesc") = "By State"
    dt.Rows.InsertAt(row, 0)

    Return dt
  End Function
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
Comment Utility
Hi,

you have to use
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
     If Not Page.IsPostBack() Then
        lstStates.DataSource = BindStates
        lstStates.DataValueField = "LocationID"
        lstStates.DataTextField = "Location"
        lstStates.Items.Insert(0, new System.Web.UI.WebControls.ListItem("By State","0"))
        lstStates.SelectedIndex = 0
        lstStates.DataBind()
           
        lstCategorie.DataSource = BindCategories
        lstCategorie.DataValueField = "CategoryID"
        lstCategorie.DataTextField = "CategoryDesc"
        lstCategorie.DataBind()
        lstCategorie.Items.Insert(0, new ListItem("By Category","0"))
        lstCategorie.SelectedIndex = 0
    End If

  End Sub

if you see i insert the default (0 position) item after i bind the listbox

Regards,
B..M
0
 
LVL 5

Author Comment

by:bullrout
Comment Utility
Hi There,


I tried mdamico's function and it retuned the error "Value cannot be null. Parameter name: dataTable ", I have already tried the function above from mmarinov and it just keeps returning the error "Error: Type 'ListItem' is not defined.".

I'm not sure what else I need to check?

Sean


0
 
LVL 28

Expert Comment

by:mmarinov
Comment Utility
So obviously you don't import the System.Web.UI.Webcontrols

change
lstCategorie.Items.Insert(0, new ListItem("By Category","0"))

to
lstCategorie.Items.Insert(0, new System.Web.UI.WebControls.ListItem("By Category","0"))


Regards,
B..M
0
 
LVL 5

Author Comment

by:bullrout
Comment Utility
Hi There,

I did try that and while it doesn't return any errors, I do not get the items added to the list. Should I re-boot or something like that ?

Sean


Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports Microsoft.VisualBasic
Imports System.Web.UI.WebControls

Namespace Irrigation

Public Class PagingDataList : Inherits System.Web.UI.Page

  Protected lblCurrentIndex As System.Web.UI.WebControls.Label
  Protected lblRecordCount As System.Web.UI.WebControls.Label
  Protected dlistcat As System.Web.UI.WebControls.DataList
  Protected lblPageSize As System.Web.UI.WebControls.Label
  Protected lblCounts As System.Web.UI.WebControls.Label
   
    Protected WithEvents lstStates As System.Web.UI.WebControls.ListBox
    Protected WithEvents lstCategorie As System.Web.UI.WebControls.ListBox

Dim CategoryID as Integer  


  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
   
      CategoryID = Cint(request.querystring("CategoryID"))
    If Not Page.IsPostBack() Then
        lstStates.DataSource = BindStates
        lstStates.DataValueField = "LocationID"
        lstStates.DataTextField = "Location"
        lstStates.Items.Insert(0, new System.Web.UI.WebControls.ListItem("By State","0"))
        lstStates.SelectedIndex = 0
        lstStates.DataBind()
           
        lstCategorie.DataSource = BindCategories
        lstCategorie.DataValueField = "CategoryID"
        lstCategorie.DataTextField = "CategoryDesc"
        lstCategorie.Items.Insert(0, new System.Web.UI.WebControls.ListItem("By Category","0"))
         lstCategorie.SelectedIndex = 0
        lstCategorie.DataBind()
       
      BindTheData(CategoryID)
    End If
  End Sub
0
 
LVL 5

Author Comment

by:bullrout
Comment Utility
ok my mistake!! Sorry marinov, I didn't listen properly to your comment regarding the adding of the item after binding the list.


 lstStates.DataBind()
        lstStates.Items.Insert(0, new System.Web.UI.WebControls.ListItem("By State","0"))
        lstStates.SelectedIndex = 0

I appreciate your help - Sean
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now