Solved

Error: Type 'ListItem' is not defined.

Posted on 2004-08-04
11
1,373 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

624 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