Convert Array list containing Datetime to String array list - asp.net

I am new to Asp.Net and hence this question which I know might sound silly but I am really reaching no where.
Is there a way to convert an array list that contains 'Datetime' values to an array list of strings ? I wanted to use the output as a part of a javascript function.
Thanks for the help !
I am using Asp.Net with VB.
Updated
Here is the snippet
Dim timeLapse As New ArrayList
Dim timelapsehour As New ArrayList
Dim DtTmOfInf As DateTime
DtTmOfInf = DateTime.Parse(temp)
.
.
.
For i = 0 To timeLapse.Count - 1
timelapsehour.Add(DtTmOfInf.AddHours(timeLapse(i)))
Next
Here the 'DtTmOfInf' stores the value of 'temp' which is a string in datetime format and the timelapsehour is that array list that stores the required date and time values.

You could do this in a one-liner even (with Linq):
Dim yourDateList As List(Of DateTime) = ...
Dim yourStringList = yourDateList.Select(Function(d) d.ToString()).ToList()
I've made some assumptions about your data types; if you could add to your question the code you use to build your list, then I can refine as necessary.

Related

LINQ SUM, AVERAGE

This is code that returns list of data from Splatt Table.
Public Function GetListofProduct(ByVal pCOde As String, ByVal date1 As DateTime, ByVal date2 As DateTime) As List(Of Splatt)
Return (From e As Splatt In TradingCTX.Splatts Where e.plCode.Contains(pCOde) And (e.plDate = date1 Or e.plDate = date2)).ToList
End Function
I really need help about getting the sum of the price and transfer it to another gridview.
Instead of writing the Linq the way you are try like this. This will give you the sum, but you can also return your list by calling ToList() instead of Sum. You can also use Average.
Dim sum As Double = TradingCTX.Splatts.Where(Function(e) e.plCode.Contains(pCOde) And (e.plDate = date1 Or e.plDate = date2)).Sum(Function(e) e.Charges)
There is nothing wrong with writing the Linq as you have so you could keep your Linq and just change your ToList() to .Sum(Function(e) e.Charges) where e.Charges is the name of the field you want to sum.
Please explain your difficulties getting the sum value to another table.

Sorting an array of dates

I have created an array from a CSV of date values and now need to be able to sort them so that I can then get the latest date from the array.
I have tried:
Array.Sort()
but this doesn't sort correctly, I suppose because the array values are strings, any body got any ideas??
Thanks for any help.
CODE USED TO CREATE ARRAY
'array string exampel: "19/07/2012,23/07/2012,23/07/2012,19/07/2012,25/07/2012"
Dim ArrDates As Array = ArrDates .Split(",")
SOLUTION
Dim ArrAgentsReportCheck As Array = AgentsReportCheck.Split(",")
Dim ArrDates As New List(Of Date)
For i As Integer = 0 To ArrAgentsReportCheck.Length - 1
ArrDates.Add(ArrAgentsReportCheck(i))
Next
ArrDates.Sort()
Dim LatestDate As Date = ArrDates.Max()
ArrDates = ArrDates.OrderBy(Function(d) DateTime.ParseExact(d, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)).ToArray()
Alternately, you can use OrderByDescending() depending upon your needs.
As astander said, It is very complicated to sort a array having datetime values. Instead just convert the array to List or ArrayList and make your life easy.
For ArrayList you can use the following syntax:
List<DateTime> dates = ... // init and fill
dates.Sort();
dates.Reverse();
One way would be to convert strings to DateTime using DateTime.ParseExact
Another way just to write your own IComparer and pass to Array.Sort

VBScript - Database - Recordset - How to pass DateDiff value in the database

I'm using DateDiff() function of ASP to find the date difference between two dates.
The Function works fine and displays the exact date difference between two dates but when it comes to insert this value in the database it takes 9 as the value irrespect of any date difference.
Suppose difference between two dates is more than 15 or 20 days, in the database it takes "9".
I have used INT as the DATA TYPE for the column where it displaying the date difference.
Is DATA TYPE creating an issue here?
I even tried using session variable to store the value but no luck - here's my code below:
if request.Form("sub") <> "" then
sql = "Select * from emp_leave_details"
rs.open sql , con, 1, 2
dim diff
dim todate
dim fromdate
fromdate= rs("leave_from")
todate= rs("leave_to")
session("date_diff")=datediff("d",fromdate,todate)
rs.addnew
rs("emp_name") = request.Form("name")
rs("emp_no") = request.Form("number")
rs("address") = request.Form("address")
rs("contact_no") = request.Form("contact")
rs("mobile_no") = request.Form("mobile")
rs("contact_onleave") = request.Form("contact_details")
rs("leave_type") = request.Form("rad")
rs("other_leave_details") = request.Form("PS")
rs("leave_from") = request.Form("from")
rs("leave_to") = request.Form("to")
rs("applied_by") = request.Form("apply")
rs("accepted_by") = request.Form("accept")
rs("approved_by") = request.Form("approve")
rs("no_of_leave_taken")= session("date_diff")
rs.update
response.Write("<script language='javascript'>{update();}</script>")
rs.close
end if
The datatype has nothing to do with this. Storing the value in the session is not the solution. You can use a regular variable.
From your code it looks like you always use the same values for fromdate and todate. This is because you do not iterate the rows in the resultset.
if not rs.bof and not rs.eof then
do while not rs.eof
'' code to execute for each row
rs.moveNext
loop
end if
In your current script rs will always return the results of the first row returned by the query.
The second problem your running into might be the Date datatype. Convert your value to a date using cDate and use this to calculate the difference.
Your problem is , you search for "Select * from emp_leave_details" which always gives all records from that table. You retrieve the values of the first record and do a diff of these, which results in always the same value, that is normal. From your question it is unclear what you really want to do. I suppose so want so select a record like
Select * from emp_leave_details where emp_name=<%=request.Form("name")%>
and based on that add a new record with a computed no_of_leave_taken.
Sorry guys my bad...
It was the database field name that I was calling instead of
fromdate= request.form("from")
todate= request.form("to")
I was calling this
fromdate= request.form("leave_from")
todate= request.form("leave_to")
Sorry again..but I really appreciate you all for providing me with all that possible solutions.
Thanks.

Using a Repeater with a dynamically generated table, ie, so unknown field names

I'm trying to produce a repeater showing amounts of money taken by various payment types into a table.
Payment types available come from a global settings file as an array, I am creating a dataTable by looping this list and extracting sales reports (there might be a more efficient way than this loop, but this is not my concern at the minute).
My question: How do I bind this to a repeater and display it when I dont necessarily know the table column names?
I've tried various methods to give the table a header row and give the columns numerical names from a for > next loop, but am either getting no results, or
System.Data.DataRowView' does not contain a property with the name '7'. < or whatever number
This is where I currently am:
EDIT: JUST REALISED MY CODE WAS AWFUL, SO UPDATED:
Dim paymentTable As New DataTable("paymentTable")
For j = 0 To UBound(paymentTypes)
Dim Type = Trim(paymentTypes(j))
Dim headers As DataColumn = New DataColumn(j.ToString)
paymentTable.Columns.Add(headers)
Next
Dim titleRow As DataRow = paymentTable.NewRow()
For k = 0 To UBound(paymentTypes)
Dim Type = Trim(paymentTypes(k))
titleRow.Item(k) = Type
Next
paymentTable.Rows.Add(titleRow)
Dim newRow As DataRow = paymentTable.NewRow()
For i = 0 To UBound(paymentTypes)
Dim Type = Trim(paymentTypes(i))
Try
newRow.Item(i) = '' GO OFF AND GET STUFF FROM DB
Catch
newRow.Item(i) = "0 "
End Try
Next
paymentTable.Rows.Add(newRow)
THIS EDITED CODE WORKS BUT I ONLY GET ONE ITEM
What I was hoping for would look something like:
card | cash | paypal ... etc (headings row)
£250 | £54 | £78 ... etc (values row)
Obviously there're a million ways this can be done, but this makes sense for my application, which has to be expandable and contractable depending on payment types available and this whole table needs to be repeated for multiple locations (also variable depending on who's viewing, and the number of locations in the system)
No, dont give up but just dont name columns by absolute number with no string before try
Dim headers As DataColumn = New DataColumn("col"+ j.ToString)

ASP.net with SQL query

I am working with ASP.net. In that in one sql query my output is 21,22,23, which is a string.
I want to remove those commas and store them as separate integer values...I want to use an array. Plese help. How to do that ?
You can convert a string separated by certain characters by using .Split(char):
string test = "21,22,23";
string[] split = test.Split(',');
This will give you an array of strings though. If you want to use them as integers you will want to convert them as well, and depending on your situation you might want to check if it's parseable or not, but you could use LINQ and do something like this:
string test = "21,22,23";
int[] values = test.Split(',').Select(value => Convert.ToInt32(value)).ToArray();
the String.Split(char[]) function should do this for you. I think in ASP.net it goes:
string values = "21,22,23";
string[] valuesArray = values.split(",");
While a normal String.Split would work, you still won't get integer values. You can try a simple LINQ query like so:
var results = from string s in yourString.Split('s')
select int.Parse(s);
You can then obviously cast it to a list or array, depending on your needs... A sample for converting it to an array directly in the LINQ query is as follows:
int[] results = (from string s in yourString.Split('s')
select int.Parse(s)).ToArray();

Resources