I have this array in ASP
CONST CARTPID = 0
CONST CARTPRICE = 1
CONST CARTPQUANTITY = 2
dim localCart(3,20)
I add items to this array dynamically like this
localCart(CARTPID,i) = productId
localCart(CARTPRICE,i) = productPrice
localCart(CARTPQUANTITY,i) = 1
The problem is, after 4 items, I can still add the items but UBound always return 3. Which causing my conditions failing.
I want to increase size of this array at run time so that UBOUND can return latest value.
Please let me know how can i do that. Here is my complete code
'Define constants
CONST CARTPID = 0
CONST CARTPRICE = 1
CONST CARTPQUANTITY = 2
'Get the shopping cart.
if not isArray(session("cart")) then
dim localCart(3,20)
else
localCart = session("cart")
end if
'Get product information
productID = trim(request.QueryString("productid"))
productPrice = trim(request.QueryString("price"))
'Add item to the cart
if productID <> "" then
foundIt = false
for i = 0 to ubound(localCart)
if localCart(CARTPID,i) = productId then
localCart(CARTPQUANTITY,i) = localCart(CARTPQUANTITY,i)+1
foundIt = true
exit for
end if
next
if not foundIt then
for i = 0 to 20
if localCart(CARTPID,i) = "" then
***ReDim Preserve localCart(UBound(localCart, 1) + 1,20)***
localCart(CARTPID,i) = productId
localCart(CARTPRICE,i) = productPrice
localCart(CARTPQUANTITY,i) = 1
exit for
end if
next
end if
end if
If your adding the items dynamically in a loop you'll want to use the Redim Preserve() statement. You'll want to use the Preserve part so you don't lose any of your existing data.
Otherwise if your using the array data and then redimming it for another set of data you can just the Redim() statement
Here is a good reference on using Redim() / Redim Prevserve() Statments: http://classicasp.aspfaq.com/general/can-i-create-an-array-s-size-dynamically.html
The first dimension is only 3 in length, while the second dimension is 20. If you want the UBound of the second dimension, do this:
UBound(localCart, 2)
Which returns 20. You should be able to combine this with ReDim Preserve.
I think redimentioning the array with the current UBound+1 after each addition of new item will make UBound gives you the latest value finally.
// New item addition code will go here
ReDim localCart(UBound(localCart, 1) + 1,20)
So it will update your array with the new size every time you will add the new item.
Related
I'm trying to combine the row cells when the campaign code and vehicle no are repeated as shown in below image. The result listed below is with gridview 20 page size
Problem
When the grid view page size is set with 2 for example, the row cell no longer combined. The result show each separated record.
If campaign code is sorted ascending/descending, the last record row cells will always not combine even though the campaign code and vehicle no are matched. Below image shown campaign code sorted in ascending. So when the campaign code is sorted descending, all the CMP002 are combined, while the last record of CMP001 will not be combined as shown in below image anymore.
Code Behind
Private Sub GV_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GV.RowDataBound
For rowIndex As Integer = GV.Rows.Count - 2 To 0 Step -1
Dim gvRow As GridViewRow = GV.Rows(rowIndex)
Dim gvPreviousRow As GridViewRow = GV.Rows(rowIndex + 1)
Dim sCurrCampaignCode As String = GV.DataKeys(rowIndex).Values("CAMPAIGN_CODE")
Dim sCurrVehicleNo As String = GV.DataKeys(rowIndex).Values("VEHICLE_NO")
Dim sPreviousCampaignCode As String = GV.DataKeys(rowIndex + 1).Values("CAMPAIG_CODE")
Dim sPreviousVehicleNo As String = GV.DataKeys(rowIndex + 1).Values("VEHICLE_NO")
If sCurrCampaignCode = sPreviousCampaignCode AndAlso sCurrVehicleNo = sPreviousVehicleNo Then
If sCurrCampaignCode = sPreviousCampaignCode Then
If gvPreviousRow.Cells(1).RowSpan < 2 Then
gvRow.Cells(1).RowSpan = 2
gvRow.Cells(2).RowSpan = 2
gvRow.Cells(3).RowSpan = 2
Else
gvRow.Cells(1).RowSpan = gvPreviousRow.Cells(1).RowSpan + 1
gvRow.Cells(2).RowSpan = gvPreviousRow.Cells(2).RowSpan + 1
gvRow.Cells(3).RowSpan = gvPreviousRow.Cells(3).RowSpan + 1
End If
gvPreviousRow.Cells(1).Visible = False
gvPreviousRow.Cells(2).Visible = False
gvPreviousRow.Cells(3).Visible = False
End If
End If
Next
End Sub
I just found a solution. Code have to be moved from RowDataBound to OnDataBound instead
I have a function, which is supposed to return zero, if the input cannot be converted to an integer.
But sometimes it fails, if the field from a resultset is not a proper value, whatever it is.
Function nulblank(str)
dim val
if IsNull(str) then
val=0
else
str = trim(str)
if isNumeric(str) then
val = cDbl(str)
else
val = 0
end if
end if
nulblank = val
end function
I get an error 0x80020009 on str = trim(str)
This function is only called on
set rs = conn.execute(sql)
i = nulblank(rs("somefield"))
How can I make this function "failsafe", so it never dies, but returns 0 on "bad" values?
I guess I could do on error resume next and if Err.Number <> 0 then something.
But what can be in a rs("somefield") which is not null, but cannot be trim()'ed?
That error usually relates to an empty recordset.
You should check that the recordset has a row before attempting to retrieve a column value, eg:
set rs = conn.execute(sql)
if not rs.eof then
i = nulblank(rs("somefield"))
end if
I am using a csvReader in order to retrieve data from a csv file. My csv file consists of 500elements and I only need the first 300. How do I limit my csvReader in order to return only 300 elements?
Dim PressureTable As DataTable = GetDataTabletFromCSVFile(BackUpDirDminus1)
Console.WriteLine("Rows count:" + PressureTable.Rows.Count.ToString)
Console.ReadLine()
Using CSVReader As New TextFieldParser(BackUpDirDminus1)
CSVReader.SetDelimiters(New String() {","})
CSVReader.HasFieldsEnclosedInQuotes = True
'read column names
Dim colFields As String() = CSVReader.ReadFields()
'For Each column As String In colFields
While Not CSVReader.EndOfData
Dim fieldData As String() = CSVReader.ReadFields()
'Making empty value as null
For i As Integer = 0 To fieldData.Length-1
If fieldData(i) = "" Then
fieldData(i) = Nothing
End If
Next
PressureTable.Rows.Add(fieldData)
End While
End Using
Please help. Thanks
I suppose there should be a method name "ReadNextRecord()", so your while loop should be like
While CSVReader.ReadNextRecord()
Declare a int k =0
and do k++
Once K++ reaches 300, then you can End While.
I have two CheckBoxList controls (chkListVideoMedia and chkListAudioMedia) on my page that I want to capture information from and insert the records into the database. I have it working for one of the controls, I just need help modifying my code below to include the second CBL
Dim values As New ArrayList()
For counter As Integer = 0 To chkListVideoMedia.Items.Count - 1
If chkListVideoMedia.Items(counter).Selected Then
MyTextBox.Text = chkListVideoMedia.Items(counter).Value
values.Add(newId)
End If
Next
If values.Count > 0 Then
For item As Integer = 0 To values.Count - 1
If item = 0 Then
MyMedia1.Text = values(item).ToString
End If
If item = 1 Then
MyMedia2.Text = values(item).ToString
End If
If item = 2 Then
MyMedia3.Text = values(item).ToString
End If
If item = 3 Then
MyMedia4.Text = values(item).ToString
End If
Next
End If
Thanks,
James
You can find out which Collection has the most Items, then check to make sure that count is not greater than the maximum Items in each collection. Something like this.
Dim values As New ArrayList()
Dim counter As Integer
If chkListVideoMedia.Items.Count > chkListAudioMedia.Items.Count Then
counter = chkListVideoMedia.Items.Count - 1
Else
counter = chkListAudioMedia.Items.Count - 1
End If
For x = 0 To counter
If Not (counter > chkListVideoMedia.Items.Count - 1) Then
'Do your work here
End If
If Not (counter > chkListAudioMedia.Items.Count - 1) Then
'Do your work here
End If
Next
i'm using vb.net i have the following object array that i'm trung to extract all the true value from give it a name and add it to an array
here is the object array
this is what i have tried so far :
Dim myarray() As String
Dim number As Integer = 0
If resultArray(0).BolComment Then
myarray(number) = "comment"
number = number + 1
End If
If resultArray(0).BolComplete Then
myarray(number) = "complete"
number = number + 1
End If
If resultArray(0).BolFinished Then
myarray(number) = "Finished"
number = number + 1
End If
If resultArray(0).BolOutCome Then
myarray(number) = "OutCome"
number = number + 1
End If
If resultArray(0).BolStatred Then
myarray(number) = "Started"
number = number + 1
End If
If resultArray(0).BolUser Then
myarray(number) = "User"
number = number + 1
End If
this is giving me an error : the variable has been used before
Question how can i extract all the items that have the true value and push it to a new array with giving it a new name
Thanks
I think your problem is that you are not initializing the array to a specific size, nor are you re-sizing it each time you add a new item. However, it would be better to just use the List(T) class:
Dim list As New List(Of String)()
If resultArray(x).BolComment Then
list.Add("comment")
End If
If resultArray(0).BolComplete Then
list.Add("complete")
End If
If resultArray(0).BolFinished Then
list.Add("Finished")
End If
If resultArray(0).BolOutCome Then
list.Add("OutCome")
End If
If resultArray(0).BolStatred Then
list.Add("Started")
End If
If resultArray(0).BolUser Then
list.Add("User")
End If
Then, if you need it as an actual array, do this:
Dim myarray() As String = list.ToArray()