Solved

Error: Type 'ListItem' is not defined.

Posted on 2004-08-04
11
1,357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 7

Expert Comment

by:mdamico
ID: 11723171
Try adding

Imports System.Web.UI.WebControls
0
 
LVL 7

Expert Comment

by:mdamico
ID: 11723182
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
ID: 11723240
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:mdamico
ID: 11723403
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
ID: 11723446
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
 
LVL 7

Expert Comment

by:mdamico
ID: 11723547
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
ID: 11723779
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
ID: 11723835
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
ID: 11723894
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
ID: 11723935
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
ID: 11724007
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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