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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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
Nitin SontakkeDeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.