Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error: Type 'ListItem' is not defined.

Posted on 2004-08-04
11
Medium Priority
?
1,435 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
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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

971 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