I have list of rows stored in object IEnumerator.
I need to display the row items as paging so that On Next button click it would display next item from the object while on pressing Previous button it should display the previous row.
My failed attempted code below...
Dim en As IEnumerator
Dim row As DataRow
Dim dt As DataTable dt = loadTips() // return datatable
en = dt.Rows.GetEnumerator()
Protected Sub btn_nxt_Click(sender As Object, e As EventArgs) Handles btn_nxt.Click
While en.MoveNext
row = en.Current
Response.Write(row(0))
End While
End Sub
Protected Sub btn_pre_Click(sender As Object, e As EventArgs) Handles btn_pre.Click
' code to move previous row
End Sub
You need to store the current page and simplify the enumerator to just those items.
Skip and Take are helper methods to simplify this.
Related
What i'm trying to do is to generate data on a ListBox from a query and then i can select some values, press a button and move those values to another ListBox.
When i try to move the values from a ListBox by the selecting the values and pressing the button i get the following error message:
System.NullReferenceException: 'Object reference not set to an instance of an object.' i think this is because im making a reference to the value and i'm not creating an instance of the value (correct me if i am wrong)
To fix this i believe i might have to instantiate each query value in an array to add to the listbox.
If this is correct what is the correct way to implement that array?
How i populate the ListBox
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FillSitesListbox()
End Sub
Private Sub FillSitesListbox()
Try
dt = SQL.ExecQuery("SELECT s.Str_ID, s.Nm, d.str_grp_id, (Cast(s.Str_ID As varchar) + ' - ' + s.Nm + ' [ ' + Cast(d.str_grp_id As varchar) + ' ]' ) as IDDesc
FROM Retail_Store s left outer join store_group d on ( s.str_id = d.str_id )
ORDER BY s.Str_ID")
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
SitesListBox.DataTextField = "IDDesc"
SitesListBox.DataSource = dt
SitesListBox.DataBind()
End Sub
How i move values between ListBoxes
Protected Sub FromSiteButton_Click(sender As Object, e As EventArgs) Handles FromSiteButton.Click
SitesListBox.Items.Add(StoresListBox.SelectedItem)
SitesListBox.Items.Remove(SitesListBox.SelectedItem)
End Sub
Protected Sub FromStoreButton_Click(sender As Object, e As EventArgs) Handles FromSTOREButton.Click
StoresListBox.Items.Add(SitesListBox.SelectedItem)
StoresListBox.Items.Remove(SitesListBox.SelectedItem)
End Sub
How i execute a query
Public Function ExecQuery(query As String) As DataTable
Dim DBDT = New DataTable
Using DBCon As New SqlConnection(ConStr),
DBCmd As New SqlCommand(query, DBCon)
Params.ForEach(Sub(p) DBCmd.Parameters.Add(p))
Params.Clear()
DBCon.Open()
DBDT.Load(DBCmd.ExecuteReader)
End Using
Return DBDT
End Function
'Add Params
Public Sub AddParam(Name As String, Value As Object)
Dim NewParam As New SqlParameter(Name, Value)
Params.Add(NewParam)
End Sub
End Class
It seems that the code that moves the items is wrong.
In the first click (FromSiteButton_Click) you add the SelectedItem of the StoresListBox to the SiteListBox and then remove the SelectedItem from SitesListBox. I think you should add to the StoreListBox and remove from Sites selected item of that box. The same happens in the move FromStore
In any case, to avoid NRE you should always test if the reference variables that you are using are Nothing or not before using them
Protected Sub FromSiteButton_Click(sender As Object, e As EventArgs) Handles FromSiteButton.Click
If SitesListBox.SelectedItem IsNot Nothing Then
StoresListBox.Items.Add(SitesListBox.SelectedItem)
SitesListBox.Items.Remove(SitesListBox.SelectedItem)
End If
End Sub
Protected Sub FromStoreButton_Click(sender As Object, e As EventArgs) Handles FromSTOREButton.Click
If StoresListBox.SelectedItem IsNot Nothing Then
SitesListBox.Items.Add(StoresListBox.SelectedItem)
StoresListBox.Items.Remove(StoresListBox.SelectedItem)
End If
End Sub
When i am trying to edit a editable gridview by clicking on edit
button. (so without inserting data) when i navigate to next page of
the grid.i saw that grid row which i edited is being open to edit
without clicking edit button. in 1st page(1st image) of the grid u can
see that i am trying to edit 6th row
in 2nd page of the grid you can see that 6th row is changed to
editable mode without even clicking on edit button
vb.net code
Protected Sub BindGridSubMeter()
Dim conMRI As New ConnectionMRI()
Dim ds As DataSet = conMRI.NabersSubMetergetData(ddlBldgId.SelectedItem.Value, ddlRating.SelectedItem.Value)
TextExclusions.Text = ds.Tables(1).Rows(0).Item(0).ToString()
UpdatePanelExclusions.update()
With GridSubMeter
.DataSource = ds.Tables(0)
.DataBind()
End With
End Sub
Protected Sub GridSubMeter_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridSubMeter.RowEditing
GridSubMeter.EditIndex = e.NewEditIndex
Me.BindGridSubMeter()
End Sub
Protected Sub GridSubmeter_PageIndexChanging(sender As Object, e4 As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridSubMeter.PageIndexChanging
GridSubMeter.PageIndex = e4.NewPageIndex
Me.BindGridSubMeter()
End Sub
You do not reset the EditIndex on PageIndexChanging. The edit index is based on the visible items, not the total.
Protected Sub GridSubmeter_PageIndexChanging(sender As Object, e4 As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridSubMeter.PageIndexChanging
GridSubMeter.EditIndex = -1
GridSubMeter.PageIndex = e4.NewPageIndex
Me.BindGridSubMeter()
End Sub
My Global Variable Class:
Public Class GlobalVariable
Public Shared containsListBox2Item As ArrayList
End class
My Listbox2 Items are: What i am doing is adding listbox 2 items from listbox 1 using a > button which i need all items from listbox2 into an array list and then call it to do another thing. so i want to call each item. not sure how to do so.
1. X,Y Coordinate
2. Latitude, Longitude
3. Zip Code
4. State
5. City
Basically i want each listbox.text item to have its own value in the array list. so lets say like
If listbox1.text = "X,Y Coordinate" Then
GlobalVariable.containsListBox2Item.add("X,Y Coordinate").
Not sure if i coded that properly. Please help.
I would store the items not in the ListBox controls, but in Lists. In general, you want to keep your data somewhere not on the UI thread but just use the UI as a display. Ultimately, you will put the items from a List into the ArrayList
Make a winforms project, add a two ListBox and two Button controls.
Private list1 As New List(Of String)
Private list2 As New List(Of String)
Private arrayList As New ArrayList()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button1.Text = ">"
Button2.Text = "Finish"
list1.AddRange({"X, Y", "Lat", "Zip", "State", "City"})
updateListBoxes()
End Sub
Private Sub updateListBoxes()
ListBox1.Items.Clear()
ListBox1.Items.AddRange(list1.ToArray())
ListBox2.Items.Clear()
ListBox2.Items.AddRange(list2.ToArray())
End Sub
' > button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim item = ListBox1.Text
If item = "" Then Exit Sub
list1.Remove(item)
list2.Add(item)
updateListBoxes()
End Sub
' finish button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
arrayList.Clear()
arrayList.AddRange(list2.ToArray())
End Sub
Use the below code to add all item values from ListBox2 to your arraylist.
For Each item As ListItem In ListBox2.Items
GlobalVariable.containsListBox2Item.Add(item.ToString())
Next
Try this :
'This for adding listbox 2 items from listbox 1
For Each item As ListViewItem In ListView1.Items
ListView2.Items.Add(item.Text)
Next
'this for copingall items from listbox2 into an array list
For Each item As ListViewItem In ListView2.Items
GlobalVariable.containsListBox2Item.Add(item.Text)
Next
I have a listview and two data pagers. My listview is hooked up to a data source whose data is ordered randomly. ORDER BY NEWID()
As you can imagine, each time I select a page or click next/prev page all the data is randomized making the datapager quite useless.
I figured I could set the datasource when the page is not posted back and then programmatically set the datasource to the listview but now when selecting a page or clicking the next/prev buttons, the page just simply doesn't change..
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dv As DataView = sdsMembres.Select(DataSourceSelectArguments.Empty)
lvListMembres.DataSource = dv
lvListMembres.DataBind()
DataPager2.PagedControlID = "lvListMembres"
DataPager3.PagedControlID = "lvListMembres"
DataPager2.DataBind()
DataPager3.DataBind()
End If
End Sub
What am I missing / is there a better way to do this. The order by MUST be random and I MUST have the data pagers separate from the listview.
Thank you!
Take the code between the If statement and place it in a separate function.
Add a method of storing the data, in this particular case I'll store it as a DataTable in a session.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Clear datatable from session
Session("CourtierList_dt") = Nothing
' Set datasource to listview and save in a session variable
SetCourtierList()
End If
End Sub
Protected Sub SetCourtierList()
Dim dt As DataTable
If Session("CourtierList_dt") Is Nothing Then
Dim dv As DataView = sdsMembres.Select(DataSourceSelectArguments.Empty)
Session("CourtierList_dt") = dv.ToTable
End If
dt = Session("CourtierList_dt")
lvListMembres.DataSource = dt
lvListMembres.DataBind()
DataPager2.PagedControlID = "lvListMembres"
DataPager3.PagedControlID = "lvListMembres"
DataPager2.DataBind()
DataPager3.DataBind()
End Sub
Also, make sure to add the PagePropertiesChanging event to the listview
Protected Sub lvListMembres_PagePropertiesChanging(sender As Object, e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs) Handles lvListMembres.PagePropertiesChanging
DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
DataPager3.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
SetCourtierList()
End Sub
And there you have it, hope this is helpful for someone.
I am trying to use the code below to store the items from the list into a session. For some reason when I debug the code the count is returning 0 even though there are multiple items in the list box? Any ideas what I am doing wrong here?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
NameTextBox_AutoCompleteExtender.OnClientItemSelected = "getSelected"
End Sub
Protected Sub cmdNext_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles cmdNext.Click
Dim n As Integer = NPListbox.Items.Count
Dim arr As String() = New String(n - 1) {}
For i As Integer = 0 To arr.Length - 1
arr(i) = NPListbox.Items(i).ToString()
Next
Session("arr") = arr
Response.Redirect("~/frmDescription.aspx")
End Sub
<script language="javascript" type="text/javascript">
function getSelected(source, eventArgs) {
var s = $get("<%=NameTextBox.ClientID %>").value;
var opt = document.createElement("option");
opt.text = s.substring(s.length - 10);
opt.value = s.substring(s.length - 10);
document.getElementById('<%= NPListbox.ClientID %>').options.add(opt);
}
I am going to guess that you do not have any logic in your Page_Load to populate the listbox, based upon what it had when you finished the autocomplete extender logic. Since, you do not, then when the click event fires after the Page_Load your values are gone.
Put the logic that executes on selection of the autocomplete extender in a method and have your Page_Load call that, like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End Sub
Private Sub PopulateListBox()
' Go to whatever resource you are using to get the values for the list box
End Sub
UPDATE:
Since you are depending upon using a client-side function to grab the values from the autocomplete extender and populating the listbox that way, you need to mimic that logic in your Page_Load on the server-side, because it will be too late if you try to use the client-side one, since you need the data server-side and all of the server-side events happen before the client-side logic in a server post back.
You need to do something like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Only do this when page has posted back to the server, not the first load of the page
If IsPostBack Then
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End If
End Sub
Private Sub PopulateListBox()
' Get value from text box
Dim textBoxValue As String = Me.NameTextBox.Text
' Create new item to add to list box
Dim newItem As New ListItem(textBoxValue)
' Add item to list box and set selected index
NPListbox.Items.Add(newItem)
NPListbox.SelectedIndex = NPListbox.Items.Count - 1
End Sub