Date format in textbox [ASP.NET] - sql

I have a grid which gets populate with data from my database. As part of my grid I have a view button which views the records in a modal form.
One of my records has a date. When the textbox in the modal form gets populated with the date it gets displayed like this: "2018/01/25 12:00:00 AM"
I want only the date to be displayed without the time. I have made sure that inside my SQL table that it is only date being saved and it is.
This is the column in my grid:
<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:dd/MMM/yyyy}" SortExpression="Date"/>
This is my textbox:
<asp:TextBox ID="txtDate" runat="server" placeholder="Date" class="form-control"></asp:TextBox>
I have tried the following but it does not work:
txtDate.Text = Y.Date.ToShortDateString();
Text='<%#((DateTime)Bind("NewDate")).ToString("dd/MM/yyyy")%>'
Text='<%# Bind("JoinDate", "{0:MM-dd-yyyy}") %>'
I tried writing javascript code and limit the characters but the time still shows on load.
I would like only the date to be shown in my textbox.
Please advise. Thanks

change your grid code to this:
<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:dd/MM/yyyy}" SortExpression="Date"/>
There are three Ms in date format, just remove one M, the format will be {0:dd/MM/yyyy}

I had to convert my date to a specific format in SQL.
I added this to my stored procedure in my select statement
CONVERT(VARCHAR(10), tb_Schedule.Date, 103) [Date]

This should work.
<asp:TextBox Text='<%#DataBinder.Eval(Container.DataItem,"Date", "{0:MM/dd/yyyy}")%>' ID="txtDate" runat="server" placeholder="Date" class="form-control"></asp:TextBox>

If you wish to directly bind your data, then add to the TextBox:
Text='<%# String.Format("{0:MM-dd-yyyy}", Eval("JoinDate")) %>'
I assume you wish to have two-way communication if you are using textbox, so use Eval instead of Bind.

Related

Display date in TextBox with type="date"

I want to display stored date on database on TextBox type="date"
<asp:TextBox ID="DateStatus" runat="server" Type="date" CssClass="form-control"></asp:TextBox>
but nothing is displayed in the textbox i used. I tried this
Dim getdate As Date = DetailTable.Rows(0)(3)
DateStatus.Text= getDate
Also this which i found here
Dim getdate As Date = DetailTable.Rows(0)(3)
DateStatus.Text = DateTime.UtcNow.ToString(getdate)
When i debugged the code, the date passed correctly.
When i remove type="date" the requested date is displayed just fine
Is it not Possible to do this?
on SideNote when i generate my table using <Asp:detailsview> the date is generated with time 12:00:00 despite the field declared only as Datein the schema, but i don't think it's relevant to my current issue since every thing looked fine during debugging
And Originally i store the date as Text (#date, DateStatus.Text)
This is HTML 5 stuff. If you have a real date from your database, I simulated like this
<asp:TextBox ID="txt1" runat="server" TextMode="date" />
Dim d As New Date(1922, 12, 25)
txt1.Text = d.ToString("yyyy-MM-dd")
'displays 12/25/1922
HTML 5 expects a string in the format yyyy-MM-dd and a supporting browser will display depending on Culture setting.

Dynamically Change Gridview SqlDataSource to Stored Procedure with Parameter from Code Behind

I have a gridview with following sqldatasource code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TSOstring %>" SelectCommand="TSOList" SelectCommandType="StoredProcedure">
I am using a textbox and button to search the value in the gridview. I am trying to control this from the code behind with the following code in the search_button click event.
SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
SqlDataSource1.SelectCommand = "[dbo].[TSOSearchByDie]";
SqlDataSource1.SelectParameters.Add("#DieNo", TypeCode.String, txt_die.Text.ToString());
What I want to do is change the sqldatasource1 to a different stored procedure with a parameter called "#DieNo" in it . Pass the textbox value to the parameter and return the result. The problem i have is that it always give me the error "Sys.WebForms.PageRequestManagerServerErrorException: Procedure or function 'TSOSearchByDie' expects parameter '#DieNo', which was not supplied".
Any Thoughts?
Thanks!!
Problem solved. I don't need the # sign in the code!!!

Lose Filter when Sorting a Gridview

I can filter my Gridview with a text box however when I click on a column to sort it the filter is lost. I am assuming that I need to add something into my SQL to handle this.
The below is the SQLdataSource for the DataGrid which links to it. What I need to then add in is a variable datafield and keep the search value.
I can get a search value with a session when I do a search but unsure what I need to do to make the other part work.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:mySQLConnectionString %>"
DeleteCommand='DELETE FROM [xxx] WHERE ID = #ID'
ProviderName="<%$ ConnectionStrings:mySQLConnectionString.ProviderName %>"
SelectCommand='spFilterSearchModule'
SelectCommandType='StoredProcedure'
>
</asp:SqlDataSource>
I am therefor looking to get two parts of information
1) What do need to do to filter when you click on a column on the datagrid
2) Where do I put it i.e. page Load etc..
You need to add:
FilterExpression="filterfield='expression'"
relpace filterfield with the field you want to filter and change expression to what you want to filter from the field

VB.net Precompiled site, and adding a template column with Date differences

I have a vendor application which is precompiled. I want to add a template column into the aspx page, however I'm having some difficulty.
This works for me fine as "Available" is a column bound to the asp DataGrid:
<asp:TemplateColumn HeaderText="test">
<ItemTemplate>
<asp:Label ID="Label2" Text='<% #DataBinder.Eval(Container.DataItem, "Available") %>' runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
Now, I'm trying to basically create a template column, that takes the (int) "Available" column and divide it by 12 and then multiply by DateTime.Now.Date.Month.
I'm not having any luck with using DateTime within <% %> on this page.
Thanks,
Mark
You can remove inherits="Archive, App_Web_XXXXX" from <%# Page %> then bring your code into ASPX file and modify it.

Adding checkbox control in datagrid at first column (vb.net 2003)

I am assigning a 'ds' dataset to datagrid.In addition to that I want to add new column with check boxes.That new column will be first column of datagrid.
Currently I am using below code,
DataGrid1.DataSource = dsResult.Tables("Result")
Result table has 4 column....in addtion to that i want to column "Selection" with check boxes
You can add a checkbox inside the itemtemplate like
<ItemTemplate>
<asp:CheckBox ID="chkComplete" runat="server" />
</ItemTemplate>
For a basic understanding read this article
Including a CheckBox Control Inside an ASP.NET DataGrid...