Getting rid of the time in the date

  • Thread starter Thread starter jj297
  • Start date Start date
J

jj297

I am using a datareader and when I pull the date out of the database
it has the time added to it. How do I get rid of this on the front
end in .net?

Here's my text box.
<asp:TextBox ID="DteIntxt" runat="server" Width="94px" Font-
Names="Calibri"></asp:TextBox></td>

or do I do it on the aspx.vb page?
 
I am using a datareader and when I pull the date out of the database
it has the time added to it. How do I get rid of this on the front
end in .net?

Here's my text box.
<asp:TextBox ID="DteIntxt" runat="server" Width="94px" Font-
Names="Calibri"></asp:TextBox></td>

or do I do it on the aspx.vb page?

How does the TextBox get populated with the date...?
 
How does the TextBox get populated with the date...?

With this:

If Not Page.IsPostBack Then

Dim conn As New
Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Dim cmd As New Data.SqlClient.SqlCommand
With cmd
.CommandType = Data.CommandType.StoredProcedure
.CommandText = "GetClaim"
cmd.Parameters.AddWithValue("@claim",
Request.QueryString("claim"))
cmd.Parameters.AddWithValue("@pic",
Request.QueryString("pic"))
.Connection = conn
End With


Try
conn.Open()
rd = cmd.ExecuteReader
If rd.HasRows Then
rd.Read()
fill_controls()
End If
Finally
conn.Close()
End Try
End If

and this...

Public Function fill_controls() As Boolean

LAFlbl.Text = rd("Laf").ToString()
Doclbl.Text = rd("DocPmt").ToString
FinalLaftxt.Text = rd("FinalLaf").ToString
PCIndtxt.Text = rd("PCInd").ToString
DteIntxt.Text = rd("DteIn").ToString
Remarkstxt.Text = rd("Remarks").ToString

If rd("BICAdj") Is DBNull.Value Then
Else
BicAdjddl.SelectedValue = rd("BICAdj").ToString
End If

NOTxt.Text = rd("NoBIC").ToString

If rd("IA") Is DBNull.Value Then
Else
DropDownList1.SelectedValue = rd("IA").ToString
End If

If rd("FD") Is DBNull.Value Then
Else
DropDownList2.SelectedValue = rd("FD").ToString
End If

DteToFOtxt.Text = rd("DteToFO").ToString
DteFromtxt.Text = rd("DteBack").ToString

If rd("Results") Is DBNull.Value Then
Else
DropDownList3.SelectedValue = rd("Results").ToString
End If

Amt.Text = rd("Amt").ToString


End Function
 
jj297 said:
I am using a datareader and when I pull the date out of the database
it has the time added to it. How do I get rid of this on the front
end in .net?

Here's my text box.
<asp:TextBox ID="DteIntxt" runat="server" Width="94px" Font-
Names="Calibri"></asp:TextBox></td>

or do I do it on the aspx.vb page?

Depends how you present your data to the page.
I would expect some sort of intermediate object and an objectdatsource but
I'm guessing you don't have that at all.
Anyhow, it's a matter of formatting the date as you convert to a string for
presentation.

Dim myDate As Date = DateTime.Now

DteIntxt.Text = Format(myDate, "MMddyy")
or
DteIntxt.Text = myDate.ToString("MMddyy")
 
Depends how you present your data to the page.
I would expect some sort of intermediate object and an objectdatsource but
I'm guessing you don't have that at all.
Anyhow, it's a matter of formatting the date as you convert to a string for
presentation.

Dim myDate As Date = DateTime.Now

DteIntxt.Text = Format(myDate, "MMddyy")
or
DteIntxt.Text = myDate.ToString("MMddyy")

I tried this on the pageload section on the aspx.vb page but nothing
happened:

Dim mydate As Date = DateTime.Now
DteIntxt.Text = Format(mydate, "MMddyy")
 
With this:
DteIntxt.Text = rd("DteIn").ToString

No idea how you do this in VB (never go anywhere near it) but in C# it would
be:

DteIntxt.Text = Convert.ToDateTime(rd["DteIn"]).ToString("dd MMM yyyy");
 
With this:
DteIntxt.Text = rd("DteIn").ToString

No idea how you do this in VB (never go anywhere near it) but in C# it would
be:

DteIntxt.Text = Convert.ToDateTime(rd["DteIn"]).ToString("dd MMM yyyy");

Thanks found a convert to covert it to VB and got this:
I added this and now getting this error message
Object reference not set to an instance of an object.
 
No idea how you do this in VB (never go anywhere near it) but in C# it would
be:
DteIntxt.Text = Convert.ToDateTime(rd["DteIn"]).ToString("dd MMM yyyy");

Thanks found a convert to covert it to VB and got this:
I added this and now getting this error message
Object reference not set to an instance of an object.- Hide quoted text -

- Show quoted text -

Sorry I added this
DteIntxt.Text = Convert.ToDateTime(rd("DteIn")).ToString("dd MMM
yyyy")

And getting Object reference not set to an instance of an object
 
DteIntxt.Text = Convert.ToDateTime(rd("DteIn")).ToString("dd MMM yyyy")

And getting Object reference not set to an instance of an object

Set a breakpoint on the line above and check whether DteIntxt or rd("DteIn")
is causing the problem...
 
How does the TextBox get populated with the date...?
With this:
DteIntxt.Text = rd("DteIn").ToString
No idea how you do this in VB (never go anywhere near it) but in C# it would
be:
DteIntxt.Text = Convert.ToDateTime(rd["DteIn"]).ToString("dd MMM yyyy");
Thanks found a convert to covert it to VB and got this:
I added this and now getting this error message
Object reference not set to an instance of an object.- Hide quoted text-
- Show quoted text -

Sorry I added this
DteIntxt.Text = Convert.ToDateTime(rd("DteIn")).ToString("dd MMM
yyyy")

And getting Object reference not set to an instance of an object

It sounds like you have DbNull returned. Check it like this

if Not IsDBNull(rd["DteIn"]) Then
DteIntxt.Text = Convert.ToDateTime(rd("DteIn")).ToString("dd MMM
yyyy")
End If

You can also format your date directly in the stored procedure by
using convert function, for example

SELECT CONVERT(VARCHAR(11), DteIn, 106) ...

Hope this helps
 
Hi Alexey,
You can also format your date directly in the stored procedure by
using convert function, for example

SELECT CONVERT(VARCHAR(11), DteIn, 106) ...

You are correct, of course.

However, I try to avoid doing this as I'm a great believer in the tiered
approach to application building and, specifically, that each tier should do
its job only and not try to do the job of another tier.

In this specific instance, the job of the data layer is to serve the data to
the presentation layer, and the job of the presentation layer is to present
the data to the user in the correct format...
 
Hi Alexey,



You are correct, of course.

However, I try to avoid doing this as I'm a great believer in the tiered
approach to application building and, specifically, that each tier shoulddo
its job only and not try to do the job of another tier.

In this specific instance, the job of the data layer is to serve the datato
the presentation layer, and the job of the presentation layer is to present
the data to the user in the correct format...

100% agree
 
DteIntxt.Text = rd("DteIn").ToString

Presumably the data is coming as a DateTime from SQL Server, in which case
you might want to look at SqlDataReader.GetDateTime(), onto which you can
directly append .ToString("dd MMM yyyy"). (Not forgetting to check for
IsDbNull first, of course.)

Andrew
 
Presumably the data is coming as a DateTime from SQL Server, in which case
you might want to look at SqlDataReader.GetDateTime(), onto which you can
directly append .ToString("dd MMM yyyy"). (Not forgetting to check for
IsDbNull first, of course.)

Andrew

It is coming from the DB as Datetime.

I put this in the pageload section but it's not working. Saying I need
to declare DteIn tried to do it as string or datetime but it didn't
take it.

Convert.ToDateTime(DteIn.Text).ToString("dd MMM YY")
 
jj297 said:
It is coming from the DB as Datetime.

I put this in the pageload section but it's not working. Saying I need
to declare DteIn tried to do it as string or datetime but it didn't
take it.

Convert.ToDateTime(DteIn.Text).ToString("dd MMM YY")

Perhaps I didn't put it clearly: instead of
DteIntxt.Text = rd("DteIn").ToString

you would have

If rd.IsDbNull(4) Then
DteIntxt.Text="not known"
Else
DteIntxt.Text=rd.GetDateTime(4).ToString("dd MMM yyyy")
End If

- assuming that the datetime from SQL Server is the fifth parameter
returned. Of course, if the datetime column in SQL Server was declared as
NOT NULL then you wouldn't need to check for IsDbNull.

HTH,

Andrew
 
Perhaps I didn't put it clearly: instead of
DteIntxt.Text = rd("DteIn").ToString

you would have

If rd.IsDbNull(4) Then
    DteIntxt.Text="not known"
Else
    DteIntxt.Text=rd.GetDateTime(4).ToString("dd MMM yyyy")
End If

- assuming that the datetime from SQL Server is the fifth parameter
returned. Of course, if the datetime column in SQL Server was declared as
NOT NULL then you wouldn't need to check for IsDbNull.

HTH,

Andrew- Hide quoted text -

- Show quoted text -

I added this:

If rd.IsDbNull(4) Then
DteIntxt.Text="not known"
Else
DteIntxt.Text=rd.GetDateTime(4).ToString("dd MMM yyyy")
End If


and got. The date is not null

Object reference not set to an instance of an object.
 
I added this:

If rd.IsDbNull(4) Then
DteIntxt.Text="not known"
Else
DteIntxt.Text=rd.GetDateTime(4).ToString("dd MMM yyyy")
End If


and got. The date is not null

Object reference not set to an instance of an object.

I've already told you what you need to do...

Set a breakpoint on the first line above, and then inspect which object is
missing.

In the Immediate window, type rd and press Enter - what does it say?

Do the same for DteIntxt
 
I've already told you what you need to do...

Set a breakpoint on the first line above, and then inspect which object is
missing.

In the Immediate window, type rd and press Enter - what does it say?

Do the same for DteIntxt

Use New keyword to create an object instance on

if rd.IsDbNull(4) Then
 
Back
Top