ASP.NET GridView fail to sort a Date passed as string - sorting

Hi I am building a site which has a gridview with AutoGenerateColumns = True, the issue that I am having is the Sort condition using a dataview does not work correctly for example. If I have the following records already displayed on the gridview:
Name Address Date (MM/DD/YYYY)
Julio XYZ Australia 12/25/2010
David 1234 New Zealand 12/26/2010
Erika 5555 Singapore 12/14/2011
If I do the sorting this way:
Dim dt As DataTable = Me.GetDataTable(Me.GetQuery(Sentence, params))
Dim dv As DataView = dt.DefaultView
dv.Sort = SortExpression & " " & SortDirection
gv.DataSource = dv
gv.DataBind()
The result in the grid is :
Name Address Date (MM/DD/YYYY)
Erika 5555 Singapore 12/14/2011
Julio XYZ Australia 12/25/2010
David 1234 New Zealand 12/26/2010
So the problem it seems to be the sort is only taking in consideration part of the date only the first five characters instead of the whole string.
The values for SortExpression is the ColumnName for Date which in the datasource is CallDate and the SorDirection Value is ASC
The Me.GetDataTable(Me.GetQuery(Sentence,paramas)) is just a way to populate the datatable with the records from the Database.
Another thing, sadly I cannot change the format of the Date because is a business requirement to be presented that way, also I am not allow to use third party controls because the budget of the project. :-(
Thanks for the any idea in advance.

First I want to confirm that whether your Date is DateTime type, if yes,
please try to sample as below, hope these will give you a hand.
Dim dt As DataTable = Me.GetDataTable(Me.GetQuery(Sentence, params))
dt.DefaultView.Sort = "Date ASC" ''Sort with Asc.
gv.DataSource = dt
gv.DataBind()

Related

How to setup a control with month names

In my db i have a column created_at (date_time format). I would like to create an analytical dashboard when it will be possible to filter data by create_at by giving month name (one or more - multiple choice)
I have added a new calculated field "month" in data set with following formula:
formatDate(truncDate("MM",{created_at}),"MM" )
As a result i get new column with strings like "01", "02" etc.
I've created a new parameter:
name: Month
Data type: string
Values: multiple values: {01, 02, 03 ....}
I've created a new control:
name: Month
Style: Multi select drop down
Values: Link to a data set field -> Data set -> Month column
I've created a new filter for Month columns, based on Month parameter
My problem is: how to achieve the same result (filtering by month, multi select) having month names displayed in the control, not "01", "02" etc. Is it possible?
UPDATE
It is much better to have following formula in Month calculated field:
extract("MM",{create_at})
But it does not solve my problem....
What i did, but i really don't like this solution as it is so ugly.....Any better solution is welcome :)
new calculated field month_number, with formula:
extract("MM",{create_at})
new calculated field month_name with formula:
ifelse({Month_number}=1,"Jan",ifelse({Month_number}=2,"Feb",ifelse({Month_number}=3, "Mar",ifelse({Month_number}=4, "Apr",ifelse({Month_number}=5, "May",ifelse({Month_number}=6, "Jun",ifelse({Month_number}=7, "Jul",ifelse({Month_number}=8, "Aug",ifelse({Month_number}=9, "Sep",ifelse({Month_number}=10, "Oct",ifelse({Month_number}=11, "Nov",ifelse({Month_number}=12, "Dec","Error"))))))))))))

Formatting an individual column in Access "Table/Query" listbox

I have listbox with 1 columns populated with an Oracle based ADODB Recordset using
strsql = "SELECT '£' || expected_cost as ""Cost"""
lstComm.RowSourceType = "Table/Query"
Set lstComm.
Recordset = rs
The query returns £1.58, but the listbox displays #1.58.
If I use
strsql = "trim(TO_CHAR(round(expected_cost,2), 'L9999999999999.99')) as ""Cost"""
The query returns £1.58, but the listbox displays $1.58.
Is there a way to populate the column as UK currency, whilst keeping the RowSourceType as "Table/Query"?
Simple answer: Yes.
The easiest (and best) way to accomplish this is to use a Currency format type. From there you just change the Format field from Currency to £#,##0.00;(£#,##0.00)

Lotus sorting view on orderdate does not work properly

I have created a view in which I also have a column which holds dates. This column can be sorted on ascending and descending. This is my column properties value:
But the problem is that the view does not sort the dates properly:
And here a screenshot of the orderdate field itself:
And here screenshot of the document with the orderdate that is out of order in the view:
Update
Some documents had the orderdate as text instead of date.. I create these documents through a java agent. The field orderdate I fill in like this:
SimpleDateFormat formatterDatumForField=new SimpleDateFormat("dd-MM-yyyy");
docOrder.replaceItemValue("Orderdatum",formatterDatumForField.format(currentDateForField));
But it is saved as text instead of date. Anyone knows why?
Problem was that orderdate field was set by a backend agent and this field was set with a string.
I know saved the current time as a DateTime object and now it works:
DateTime timenow = session.createDateTime("Today");
timenow.setNow();
docOrder.replaceItemValue("Orderdatum", timenow);
It's not clear to me why it's not working for you, but you can brute force it with something like this in the column formula
dateForSure := #TextToTime(OrderDatum);
#Text(#Year(dateForSure)) + "-" + #Text(#Month(dateForSure)) + "-" + #Text(#Day(dateForSure));
Also: your Java code saves a text value because the format() method of SimpleDateFormat returns a StringBuffer. The ReplaceItemValue method generates a text item when its input is a String or StringBuffer. Assuming your form defines OrderDatum as a Time/Date field, you can call Document.ComputeWithForm in your code to force it to convert the text item to a Time/Date. Another method - probably preferable given the potential side-effects of calling ComputeWithForm would be to create a DateTime object in your Java code and pass it to the ReplaceItemValue method instead.
It's because formatterDatumForField.format(currentDateForField) returns a String instead of a date/time value. Assuming that currentDateForField is a date/time value, you should change
SimpleDateFormat formatterDatumForField=new SimpleDateFormat("dd-MM-yyyy");
docOrder.replaceItemValue("Orderdatum",formatterDatumForField.format(currentDateForField));
to
docOrder.replaceItemValue("Orderdatum",currentDateForField);

how to update an existing datatable in vb.net

hope you doing fine. I really need some help. actually I am developing a small application for creating invoices. one thing that I am finding really hard is when you have to edit an invoice. In my application, it generates an invoice number and you fill in the details such as description, qty, unit price and total amount. at first this is saved in the db, printed and shown to the head for approval. if there is any mistake, the user has to come back to the application and makes the changes for the invoice. this is where I am getting stuck. The application can recall the invoice. but once the changes made, I have to save it back to the db and this change can be for a specific record only. for instance,
invoice 001
serial descr qty unitprice total
1 paintx 2 2000 4000
2 painty 3 1000 3000
3 paintz 1 2000 2000
lets say there is a mistake and amendments to be done
serial descr qty unitprice total
1 paintx 2 2000 4000
2 painty 3 1000 3000
3 paintz 2 2000 4000
how am I supposed to make this change ?
any technique or logic I can use?
do I need to use datatable here?
please advise.
Well, you've given no clue as to the database technology/adapter you're using, but it should be something like...
con.Open()
cmd.Connection = con
cmd.CommandText = "UPDATE invoices SET serial = #serial, descr = #descr, qty = #qty ... etc... WHERE invoiceId = #invoiceId"
cmd.Parameters.AddWithValue("#serial", Form.txtSerial.Text)
cmd.Parameters.AddWithValue("#descr", Form.txtDescr.Text)
cmd.Parameters.AddWithValue("#qty", Form.txtQty.Text)
... etc...
cmd.ExecuteNonQuery()
Further reading: MSDN post on updating tables in VB.NET
Newfun()'Function for open the connection
Dim query As String = "update tBL_GageType set GageType = '" & name & "' where GID=" & id
Dim cmd As New OleDbCommand(query)
cmd.Connection = New OleDb.OleDbConnection(connectionString)
cmd.Connection.Open()
cmd.ExecuteNonQuery()

Get the the most recent and the one before the most recent item

I have a table with many anniversaries : Date + Name.
I want to display the next anniversary and the one after with Linq.
How can i build the query ?
I use EF
Thanks
John
Just order by date and then use the .Take(n) functionality
Example with a list of some objects assuming you want to order by Date then Name:
List<Anniversaries> annivDates = GetAnnivDates();
List<Anniversaries> recentAnniv = annivDates.OrderBy(d => d.Date).ThenBy(d => d.Name).Take(2).ToList();
If the anniversaries are stored in regular DateTime structs, they may have the 'wrong' year set (i.e. wedding or birth year). I suggest writing a function which calculates the next date for an anniversary (based on the current day) like:
static DateTime CalcNext(DateTime anniversary) {
DateTime newDate = new DateTime(DateTime.Now.Year, anniversary.Month, anniversary.Day);
if (newDate < DateTime.Now.Date)
newDate = newDate.AddYear(1);
return newDate;
}
Then you proceed with sorting the dates and taking the first two values like described in the other postings:
(from e in anniversaries orderby CalcNext(e.Date) select e).Take(2)

Resources