Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Time Format

Author  Topic 

vk18
Posting Yak Master

146 Posts

Posted - 2007-04-02 : 19:15:59
Hi Guys,

Also i tried this format for converting 12 Hour time to 24 Hour time but it doesn't work. Can you guys tell me what i am doing wrong.? appreciate your help.
Thx

Public Function FormatTime(ByVal old As String) As String
Try
Dim AMPM As String = "A"
Dim H As Integer = Integer.Parse(old.Substring(0, 2))
Dim M As Integer = Integer.Parse(old.Substring(2, 2))
If H > 12 Then
If AMPM = "P" Then
H = H + 12
End If
End If
Return H.ToString("00") & "00" 'M.ToString("00") & "00"
Catch SqlEx As SqlClient.SqlException
Session("Error") = SqlEx.Message.ToString
Response.Redirect("Error.aspx?Form=" & Request.Path)
Catch Ex As System.Exception
Session("Error") = Ex.Message.ToString
Response.Redirect("Error.aspx?Form=" & Request.Path)
End Try
End Function

dfiala
Posting Yak Master

116 Posts

Posted - 2007-04-02 : 19:47:13
You are working way too hard. This stuff is all built in to the framework

Stick your original time into a DateTime structure...

Dim TheTime As DateTime = DateTime.Parse(old)
Dim NewTime As String = TheDate.ToString("HH:mm")

Also, if you just work with the DateTime structure instead of strings, you can use the ToString to format it any way you need.


Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

vk18
Posting Yak Master

146 Posts

Posted - 2007-04-03 : 14:56:37
quote:
Originally posted by dfiala

You are working way too hard. This stuff is all built in to the framework

Stick your original time into a DateTime structure...

Dim TheTime As DateTime = DateTime.Parse(old)
Dim NewTime As String = TheDate.ToString("HH:mm")

Also, if you just work with the DateTime structure instead of strings, you can use the ToString to format it any way you need.



Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP



Hi I found a simple way of formatting it.
FormatDateTime(timeactual, 4)
this works fine, but if there is any null value it is not working.
how to make it work even if there is null value. any idea.?
Thx
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-03 : 15:00:58
FormatDateTime("" & timeactual, 4)

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

vk18
Posting Yak Master

146 Posts

Posted - 2007-04-03 : 15:34:41
quote:
Originally posted by Peso

FormatDateTime("" & timeactual, 4)

Peter Larsson
Helsingborg, Sweden



Hi Peso,

I tried this format but i am getting the Error

"Cast from String to type date is not valid"..
Any idea..?
Thx
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-03 : 16:15:56
if you need to explicitly handle null values.

something like:

public function FormatTime(object dateval) as string
if IsDbNull(dateval) Then
return "N/A"
else
return FormatDateTime(dateval,4)
end if
end function


FYI -- It is recommended to use the more standard .net techniques such as casting to correct datatypes and using a datetime's ToString() method to format values. But if you are more familiar/comfortable doing it "vb-style" that is fine as well. Just be sure that Option Strict and Option Explicit are both "on".

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

vk18
Posting Yak Master

146 Posts

Posted - 2007-04-03 : 18:03:14
quote:
Originally posted by jsmith8858

if you need to explicitly handle null values.

something like:

public function FormatTime(object dateval) as string
if IsDbNull(dateval) Then
return "N/A"
else
return FormatDateTime(dateval,4)
end if
end function


FYI -- It is recommended to use the more standard .net techniques such as casting to correct datatypes and using a datetime's ToString() method to format values. But if you are more familiar/comfortable doing it "vb-style" that is fine as well. Just be sure that Option Strict and Option Explicit are both "on".

- Jeff
http://weblogs.sqlteam.com/JeffS



Hi Smith,

I tried this function but no luck, same error again. how to solve this.. any idea.?Also i have time like 18:20:00, I want like 182000
how do i format this. Appreciate your help

Thx
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-03 : 19:26:26
Show me your function, how you are calling it, and your error message.

More on date formatting in .NET here: http://blogs.msdn.com/kathykam/archive/2006/09/29/773041.aspx

Also the MSDN help for Visual Studio has tons of information on it.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

vk18
Posting Yak Master

146 Posts

Posted - 2007-04-04 : 11:12:52
quote:
Originally posted by jsmith8858

Show me your function, how you are calling it, and your error message.

More on date formatting in .NET here: http://blogs.msdn.com/kathykam/archive/2006/09/29/773041.aspx

Also the MSDN help for Visual Studio has tons of information on it.

- Jeff
http://weblogs.sqlteam.com/JeffS




Hi,

I am using the Database Reader and with in that reader i am writing it to a text file. Here is my code


filetext = " & FormatTime(timeactual) & "
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-04 : 11:17:00
Will that work at all?
It will put the text & FormatTime(timeactual) & in the filetext variable/textbox...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

vk18
Posting Yak Master

146 Posts

Posted - 2007-04-04 : 11:32:24
quote:
Originally posted by Peso

Will that work at all?
It will put the text & FormatTime(timeactual) & in the filetext variable/textbox...


Peter Larsson
Helsingborg, Sweden


Yes it works, Only if there is any null value it is not working
THx
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-04 : 11:34:50
No, that doesn't work. You are not posting the actual code that you are using. We need to see things in context. What you posted makes no sense at all. In programming, when you have errors, you can just "kind of copy more or less" what you are doing, we need to see your ACTUAL UNEDITED CODE to be able to help you. Does this make sense?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-04 : 11:38:44
He's all yours


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

vk18
Posting Yak Master

146 Posts

Posted - 2007-04-04 : 11:46:32
quote:
Originally posted by jsmith8858

No, that doesn't work. You are not posting the actual code that you are using. We need to see things in context. What you posted makes no sense at all. In programming, when you have errors, you can just "kind of copy more or less" what you are doing, we need to see your ACTUAL UNEDITED CODE to be able to help you. Does this make sense?

- Jeff
http://weblogs.sqlteam.com/JeffS




Ok Here is my code

Private Function foodrecipie() As String
Dim cmd As SqlClient.SqlCommand
Dim dbfunctions As New DatabaseUtilities
Dim SQL As String
Dim strConn As String
Dim intRows As Integer
Dim filetext As String
strConn = CONNECTIONSTRING
Dim myConn As New SqlConnection(CONNECTIONSTRING)
Dim myCommand As New SqlCommand(SQL, myConn)
myConn.Open()
Dim dbreader As SqlDataReader = myCommand.ExecuteReader()
While dbreader.Read()


SQL = "SELECT copyID,timeactual FROM tblfood where tblfood.invoiceNumber = '" & Me.txtinvoicenumber.Text & "'"

Dim tempcopyid As String
Dim temptimeactual As String


If dbreader("timeactual") Is DBNull.Value Then
timeactual = ""
Else
timeactual = dbreader("timeactual")
temptimeactual = timeactual

End If


Dim copyid As String
If dbreader("copyid") Is DBNull.Value Then
copyid = ""
Else
copyid = dbreader("copyid")
tempcopyid = copyid
End If



filetext = " & FormatTime(temptimeactual) & "" & tempcopyid & "


End While
myConn.Close()

end function







Public Function FormatTime(ByVal dateval As String) As String

If IsDBNull(dateval) Then
Return "N/A"
Else
Return FormatDateTime(dateval, 4)
End If
End Function

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-04 : 11:47:53
this may go on for days .... I really wish sometimes people would simple "bite the bullet" and read a book on how to program in the language (vb.net or sql or whatever) they are using. I always start by simply at least skimming a book so I understand the basic concepts. No one seems to do that any more. They just start writing code and think that they can just ask questions as they go and everyone will tell them what to do without having any foundation at all in the technology they are using.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-04 : 12:01:27
[code]
Private Function foodrecipie() As String
Dim cmd As SqlClient.SqlCommand
Dim dbfunctions As New DatabaseUtilities
Dim SQL As String
Dim strConn As String
Dim intRows As Integer
Dim filetext As String
strConn = CONNECTIONSTRING
Dim myConn As New SqlConnection(CONNECTIONSTRING)
Dim myCommand As New SqlCommand(SQL, myConn)
myConn.Open()
Dim dbreader As SqlDataReader = myCommand.ExecuteReader()

[/code]

The SQL variable has not been yet set! The command cannot execute. Either you are getting an error here or you are not showing us your ACTUAL code.

[code]
While dbreader.Read()
SQL = "SELECT copyID,timeactual FROM tblfood where tblfood.invoiceNumber = '" & Me.txtinvoicenumber.Text & "'"
[/code]

Never, ever, ever do what you are doing there. Always use stored procedures and/or parameters. See:
http://weblogs.sqlteam.com/jeffs/archive/2006/07/21/10728.aspx
for more on this.

And, as mentioned, you are setting the SQL variable too late to execute.

[code]

Dim tempcopyid As String
Dim temptimeactual As String


If dbreader("timeactual") Is DBNull.Value Then
timeactual = ""
Else
timeactual = dbreader("timeactual")
temptimeactual = timeactual
End If

[/code]

You are trying to handle nulls here, but then in my function I already gave you code to handle them! You can't do it over and over. The function I wrote accepts an OBJECT, which may either be dbNull.value OR a valid datetime, and formats it. That's what you need to pass to the function, not a string variable!

[code]
Dim copyid As String
If dbreader("copyid") Is DBNull.Value Then
copyid = ""
Else
copyid = dbreader("copyid")
tempcopyid = copyid
End If

filetext = " & FormatTime(temptimeactual) & "" & tempcopyid & "
[/code]

Set a breakpoint here. Look at the contents of the filetext variable. It is not executing the expression you are trying to execute because it is all in quotes, it's just a string literal! The code highlighting and formatting and intellisense in Visual Studio should have indicated this to you. It needs to something like:

Filetext = FileText & FormatTime(dbreader("timeactual")) & tmpcopyID

If appending to the existing FileText value is what you want to do.

[code]
End While

myConn.Close()
end function
[/code]

I hope this helps. Good luck. And please read a book on programming vb.net so that you can learn the basics.
Go to Top of Page

vk18
Posting Yak Master

146 Posts

Posted - 2007-04-04 : 12:29:04
quote:
Originally posted by jsmith8858


Private Function foodrecipie() As String
Dim cmd As SqlClient.SqlCommand
Dim dbfunctions As New DatabaseUtilities
Dim SQL As String
Dim strConn As String
Dim intRows As Integer
Dim filetext As String
strConn = CONNECTIONSTRING
Dim myConn As New SqlConnection(CONNECTIONSTRING)
Dim myCommand As New SqlCommand(SQL, myConn)
myConn.Open()
Dim dbreader As SqlDataReader = myCommand.ExecuteReader()



The SQL variable has not been yet set! The command cannot execute. Either you are getting an error here or you are not showing us your ACTUAL code.


While dbreader.Read()
SQL = "SELECT copyID,timeactual FROM tblfood where tblfood.invoiceNumber = '" & Me.txtinvoicenumber.Text & "'"


Never, ever, ever do what you are doing there. Always use stored procedures and/or parameters. See:
http://weblogs.sqlteam.com/jeffs/archive/2006/07/21/10728.aspx
for more on this.

And, as mentioned, you are setting the SQL variable too late to execute.



Dim tempcopyid As String
Dim temptimeactual As String


If dbreader("timeactual") Is DBNull.Value Then
timeactual = ""
Else
timeactual = dbreader("timeactual")
temptimeactual = timeactual
End If



You are trying to handle nulls here, but then in my function I already gave you code to handle them! You can't do it over and over. The function I wrote accepts an OBJECT, which may either be dbNull.value OR a valid datetime, and formats it. That's what you need to pass to the function, not a string variable!


Dim copyid As String
If dbreader("copyid") Is DBNull.Value Then
copyid = ""
Else
copyid = dbreader("copyid")
tempcopyid = copyid
End If

filetext = " & FormatTime(temptimeactual) & "" & tempcopyid & "


Set a breakpoint here. Look at the contents of the filetext variable. It is not executing the expression you are trying to execute because it is all in quotes, it's just a string literal! The code highlighting and formatting and intellisense in Visual Studio should have indicated this to you. It needs to something like:

Filetext = FileText & FormatTime(dbreader("timeactual")) & tmpcopyID

If appending to the existing FileText value is what you want to do.


End While

myConn.Close()
end function


I hope this helps. Good luck. And please read a book on programming vb.net so that you can learn the basics.



i don't think this work.
Filetext = FileText & FormatTime(dbreader("timeactual")) & tmpcopyID
i am just exporting to a text file then why do i need to use dbreader again. i am already using in the code above. every body has different approaches in programming, don't think that you are the king in programming. any way i got another idea of make it work
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-04 : 12:40:17
Good luck!
Please share your solution here later, so other people can benefit from your skill.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-04-04 : 13:18:19
best of luck to you.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-04-04 : 13:44:59
vk18, you shouldn't burn your bridges here as it is evident from your code that you will need more free help. Even if they aren't "king in programming", it is apparent that they do have more knowledge than you. You should try to learn from them rather than insulting them.

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

vk18
Posting Yak Master

146 Posts

Posted - 2007-04-04 : 13:51:32
quote:
Originally posted by tkizer

vk18, you shouldn't burn your bridges here as it is evident from your code that you will need more free help. Even if they aren't "king in programming", it is apparent that they do have more knowledge than you. You should try to learn from them rather than insulting them.

Tara Kizer
http://weblogs.sqlteam.com/tarad/



I am not insulting any body here, I am just telling that every body has different approaches in programming and Can be programmed in several ways that is what i am trying to say
Go to Top of Page
    Next Page

- Advertisement -