Error convert timespan to string.

Hi All,

I try below, but error.

   SqlCommand.Parameters.AddWithValue("@CrtDate", Me.dtpTglTerima.Value.ToString("yyyy/MM/dd") + " " + ERV_Global.Get_Server_Date.TimeOfDay.ToString("hh:mm:SS"))

What's wrong ?

Thank you.
LVL 1
emi_sastraAsked:
Who is Participating?
 
Nitin SontakkeConnect With a Mentor DeveloperCommented:
I (personally) think that the code above is unnecessarily complicated. When you have two date time objects, it is somehow not clear to me why you are using Parse. The better approach would be:

Dim dteDate As DateTime = New DateTime(Me.dtpTglTerima.Value.Year, Me.dtpTglTerima.Value.Month, Me.dtpTglTerima.Value.Day, nmTime.Hour, nmTime.Minute, nmTime.Second)
0
 
Nitin SontakkeDeveloperCommented:
It almost always helps if you post the error!

What is the data type of CrtDate parameter?
0
 
emi_sastraAuthor Commented:
Hi Nitin,

 Public Shared Function Get_Server_Date() As DateTime

        If ERV_Global.Is_OleDb_Connection(strSQLSetupMasterConnection) Then

            Try

                Using conApp As OleDbConnection = New OleDbConnection(strSQLSetupMasterConnection)
                    conApp.Open()

                    Dim strCommand As String = "SELECT GETDATE() AS TIMEOUT"

                    Dim cmdSqlCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(strCommand, conApp)

                    Return cmdSqlCommand.ExecuteScalar

                End Using

            Catch ex As Exception

            End Try

        Else

            Try

                If strSQLSetupMasterConnection.Trim = "" Then
                    Return "1900/01/01"
                Else
                    Using conApp As SqlConnection = New SqlConnection(strSQLSetupMasterConnection)
                        conApp.Open()

                        Dim strCommand As String = "SELECT GETDATE() AS TIMEOUT"

                        Dim cmdSqlCommand As SqlCommand = New SqlCommand(strCommand, conApp)

                        Return cmdSqlCommand.ExecuteScalar

                    End Using
                End If

            Catch ex As Exception

            End Try

        End If

        Return Nothing

    End Function

Thank you.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Nitin SontakkeDeveloperCommented:
You posted the code, which is fine, however, what I was interested in knowing is the error that you are getting.

Did you debug? Has your above code ever likely to return 'Nothing'?

Please post the error.

Furthermore, change the time formatting to "HH:mm:ss" and try.
0
 
emi_sastraAuthor Commented:
- Has your above code ever likely to return 'Nothing'?
No.

-Please post the error.
Input string was not in a correct format.
 
        Dim dteDate As DateTime = DateTime.Parse(Me.dtpTglTerima.Value.ToString("yyyy/MM/dd") + " " + nmTime.ToString("HH:mm:ss"))


Thank you.
0
 
emi_sastraAuthor Commented:
Dim dte As DateTime = ERV_Global.Get_Server_Date
        Dim nmTime As New TimeSpan(dte.Hour, dte.Minute, dte.Second)

error at here :
Input string was not in a correct format.
 Dim dteDate As DateTime = DateTime.Parse(Me.dtpTglTerima.Value.ToString("yyyy/MM/dd") + " " + nmTime.ToString("HH:mm:ss"))

Thank you.
0
 
Pawan KumarDatabase ExpertCommented:
Please use like below -
 Dim a As String = "31/08/2017"
        Dim tr As DateTime = Date.ParseExact(a, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
        Dim finalstr As String = Convert.ToDateTime(tr).ToString("yyyy/MM/dd")
        Console.WriteLine(finalstr)

Open in new window

0
 
emi_sastraAuthor Commented:
Hi All,

Let me test first.

- nmTime.Hour, nmTime.Minute, nmTime.Second
should nmTime.Hours, nmTime.Minutes, nmTime.Seconds
with s.

Thank you.
0
 
emi_sastraAuthor Commented:
The solved solution is :

 Dim dte As DateTime = ERV_Global.Get_Server_Date
            Dim nmTime As New TimeSpan(dte.Hour, dte.Minute, dte.Second)

            Dim dteDate As DateTime = New DateTime(dtpTglTerima.Value.Year, dtpTglTerima.Value.Month, dtpTglTerima.Value.Day, nmTime.Hours, nmTime.Minutes, nmTime.Seconds)

            SqlCommand.Parameters.AddWithValue("@CrtId", clsLoginInformation.GetInstance.LoginUserId)
            SqlCommand.Parameters.AddWithValue("@CrtDate", dteDate)

Thank you.
0
 
emi_sastraAuthor Commented:
Hi Nitin,

Your solution inspired me.
Thank you.
0
 
Nitin SontakkeDeveloperCommented:
Got it. In my comment, I meant to use dte and bypass the TimeSpan object altogether. Hence the typo of s. You can think of changing it that way too.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.