Currently, I'm populating a DropDown with the following SQL:
SELECT REPORT_DATE FROM MY_TABLE
Although the values stored in the table are in the following format, dd-mon-yyyy (my preferred format), the text that fills my DropDown differs in that it also displays time, like dd-mon-yyyy hh:mm:ss AM.
What is the best way to resolve this? I know of TRUNC and TO_DATE functions in Oracle SQL. But I think I could also loop through the DropDown after its populated and truncate the string there. I have tried the following code, but it returns a runtime error:
For Each i As ListItem In DropDown1.Items
'Strip time here
i.Text.ToString("dd-MMM-yyyy")
Next
Essentially, I just want to loop my DropDown and change ONLY the text to match `dd-MMM-yyyy'. How can I accomplish this? Should I use SQL or VB.NET? Is there a best practice?
Thank you!
you can do this sql side with
SELECT to_char(REPORT_DATE, 'dd-mon-yyyy') report_date FROM MY_TABLE order by report_date;
p.s.
"values stored in the table are in the following format, dd-mon-yyyy". dates are not stored like that, they are stored as an 7 byte numeric internally, how you see them on select is entirely dependant on your NLS_DATE_FORMAT setting.
If the data is of type DateTime, you can simply specify the DataTextFormatString property on the dropdown as so:
<asp:dropdownlist DataTextFormatString ="{0:MM-dd-yyyy}" ... />
It should be to the UI layer to display the date in the format you want. You should try to avoid putting the logic to transform the date to the format you want on the SQL statement, if possible.
Use this string to get data from Oracle
SELECT TO_CHAR(REPORT_DATE,'DD-MON-YYYY') FROM MY_TABLE
Try
i.Text = Convert.ToDateTime(i.Text).ToString("dd-MMM-yyyy")
You cannot use .ToString("dd-MMM-yyyy") for date formatting on string. You need to convert it to DateTime
You can also try <asp:DropDownList DataTextFormatString="{0:dd-MMM-yyyy}" />
It's better to format the date in the DropDownList instead of letting sql doing it.
Just add DataTextFormatString="{0:dd/MM/yyyy}" to your DropDownList tag :
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="adddate"
DataTextFormatString="{0:dd/MM/yyyy}" DataValueField="adddate">
</asp:DropDownList>
Related
I have a varchar column in sql database which stores in the format (dd//mm/yyyy). I want to find difference between the System Date(ie. when a website is hit)and the column where i have stored date in (dd/mm/yyyy) and display the results of 2 columns of a table in a grid view. Can you please help in this.?
You can use DATEDIFF() function.
Syntax
DATEDIFF(datepart,startdate,enddate)
In your query
SELECT DATEDIFF(day,CAST(dateColumn as datetime),CAST(TodaysdateColumn as datetime)) as dateDiffColumn
Edit
SELECT DATEDIFF(day,CONVERT(datetime,dateColumn,103),CONVERT(datetime,TodaysDateColumn,103)) as dateDiffColumn
This gives difference in dates in terms of number of days.
Then you can bind calculated column to Gridview column.
<asp:BoundField Text='<%#Eval("dateDiffColumn")%>' runat="server">
Parse the date string into a DateTime variable using DateTime.ParseExact. You can then compare the two dates as DateTimes and get the difference.
While reading data of sql server from gridview ,the date column adds an extra 12:00:00 AM to the end of the date.
eg:9/29/2013 12:00:00 AM
In the gridview it shows this way, but i only have a date type date saved in the sql table.
I need that to be only 9/29/2013.
same thing happened while reading and displaying that in a textbox , but it could be solved this way
DateTime date =Convert.ToDateTime(VoucherDetails["voucher_date"]);
TextBox3.Text = date.ToString("MM/dd/yyyy");
any solutions for the gridview ?
Here's an example of a bound field.
<asp:BoundField DataField="TheDateField" HeaderText="HeaderText"
DataFormatString="{0:MM-dd-yyyy}" />
All you should have to do is add DataFormatString="{0:MM-dd-yyyy}" to your bound field.
For the Grid View see datetime format in asp.net gridview. However, you can also change your SQL Query to cast the time columns as a DATE. That will cut off the time portion as well.
To format a DATETIME value in a GridView, you can do it like this:
DataFormatString="{0:MM-dd-yyyy}".
I have a GridView where one of the columns/fields has checkboxes; the checkboxes are checked or unchecked, based on a sub-query from the code behind, while the container GridView is bound to a SqlDataSource. What I want to be able to do is that once the GridView is databound then make it sort by the state of checkboxes: All rows with checkboxes checked appear on the top of grid. Here is part of my GridView:
<ItemTemplate>
<asp:CheckBox ID="ProductSelector" runat="server" Checked='<%# ShowCheckMarks(DataBinder.Eval(Container.DataItem,"prodid").ToString()) %>' />
</ItemTemplate>
I am thinking that I can call the GridView's Ondatabound event and someone make it sort from there?
Thanks.
If that is the only row that you need to sort by why not just add an order by to the sql query you're using to pull the data?
You can save yourself a lot of work if you
store the state of checkbox in database
. That way you can easily set a sort expression on the template field.
Edit
You can do this on clientside via jQuery tablesorter but it might be a
bit too much of work Check this.
Again I think you can somehow manipulate the query to achieve this.
Try this
SELECT dbo.Products.*, dbo.products_recommended.* FROM dbo.Products INNER JOIN dbo.products_recommended ON (dbo.Products.prodid = dbo.products_recommended.prodid) WHERE dbo.Products.prodid IN (dbo.products_recommended.prodid) AND (dbo.Products.prodid = " + itemid + " order by dbo.Products.prodid desc )"
Please note : Never use a
select a.* from YourTable a
This will select all columns from your table & may bring a serious problem later on. Only query the columns you want like
select a.column1,a.column2 from YourTable a
SELECT dbo.Products.prodid,
dbo.Products.itemtitle,
dbo.Products.itemnumber,
dbo.Products.image_1,
CAST(ISNULL(dbo.products_recommended.recommendedid, 0) as BIT) as recommendedid
FROM dbo.Products LEFT OUTER JOIN dbo.products_recommended
ON (dbo.Products.prodid = dbo.products_recommended.prodid)
WHERE dbo.Products.prodid IN (dbo.products_recommended.prodid)
AND (dbo.Products.prodid = #itemid)
The left outer join will make sure it pulls all items from the Products table while only pulling matching items from the products_recommended table. It will also convert the recommendedid to a BIT value which should work with the checkboxes
Like the good helpers here suggested, I needed to have main sql query to populate the GridView to sort the records, instead of a different query in the code behind. So now the Declarative SqlDataSource syntax is the following. This solves the problem.
I learned a few hew things from the feedback here. Thank you all!
SELECT DISTINCT Products.prodid,
Products.itemtitle,
Products.itemnumber,
Products.image_1,
CASE WHEN YESNO IS NULL THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT) END AS CheckUncheck
FROM Products
LEFT OUTER JOIN (SELECT prodid_recommended as YESNO FROM products_recommended
WHERE #itemid IN (prodid) ) A
ON Products.prodid = A.YESNO
ORDER BY CheckUncheck DESC
I have a DOB column in a table that is currently in use. See below.
I want to select just the Year from that DOB and display that on a Listbox (or any appropriate interface on a page). We are not using SPs. So I will probably coding SQL directly from the page or using SQL datasource. Using LINQ is alright if that can be done in it.
So please someone suggests me how this can be done.
Thanks a lot.
Use the YEAR function in your SQL statement.
SELECT YEAR([DateOfBirth])
FROM MyTable
SELECT DISTINCT Year(DateOfBirth) AS Year FROM MyTable ORDER BY Year
By including the DISTINCT keyword you will prevent duplicates in your list and by adding the ORDER BY clause the years will be sorted ascending. You can then bind the result to your listbox.
In your TSQL, write this:
SELECT YEAR(DateOfBirth), ...
FROM ...
I have this code:
<asp:CheckBox ID="CheckBoxAppStatus" runat="server" Text="Done" Checked='<%# Bind("Status") %>'/>
The problem is that Bind("Status") returns string (True/False), don't know why though it was defined as Boolean in the entity model.
Is there a way to convert Bind("Status") into Boolean value and still using Bind (I need to edit this value not just show it and I think using Bind is the right way to do it automatically instead of doing it by C# code).
I had once the same problem in ASP.Net, but using ADO.Net. I had to map a column that stored Y/N to a checkbox, and the solution was to simply convert the char(1) column to a bit using cast((case when YesNoColumn = 'Y' then 1 else 0 end) as bit) in the select statement, and the binding worked correctly. This, however, requires that the insert and update statements be also modified accordingly. I am not sure how this can be done in Entity Framework though.