Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error: Type 'ListItem' is not defined.

Posted on 2004-08-04
11
Medium Priority
?
1,407 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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