Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 30th, 2007, 01:53 PM
Authorized User
 
Join Date: Oct 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default converting dates

I keep getting this error "Argument 'date2' cannot be converted to type 'date'" and I don't understand why. When I run the datediff query in the sql query analyzer, i get the correct number of days. But When I debug in vb.net I get this error. Here's my code. What am I doing wrong?


                Dim strAmount As String
                Dim date1, date2, strDateDiff, date3, strDateDiff2
                date1 = txttodaydate.Text
                date2 = txtddate.Text
                date3 = txtdisplaydate.Text
                strDateDiff = CInt(DateDiff("d", date1, date2))
                strDateDiff2 = CInt(DateDiff("d", date1, date3))

                If ddlFWClass.SelectedItem.Value = "D" Then
                    strAmount = 2000
                End If
                If ddlFWClass.SelectedItem.Value = "M" Then
                    strAmount = 2000
                End If
                If ddlFWClass.SelectedItem.Value = "R" Then
                    strAmount = 200
                End If
                If ddlFWClass.SelectedItem.Value = "W" Then
                    strAmount = 700
                End If
                If ddlFWClass.SelectedItem.Value = "SR1" Then
                    strAmount = 100
                End If
                If ddlFWClass.SelectedItem.Value = "SR2" Then
                    strAmount = 200
                End If
                .Add("@mAmount", CDbl(strAmount))


                If ddlFWClass.SelectedItem.Value = "PD" Then
                    If strDateDiff < 10 Then
                        strAmount = 100
                    Else
                        strAmount = 50
                    End If
                End If
                If ddlFWClass.SelectedItem.Value = "CP" Then
                    If strDateDiff2 < 10 Then
                        strAmount = 200
                    Else
                        strAmount = 100
                    End If
                End If

 
Old July 30th, 2007, 08:55 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

SQL Server and .NET behave quite differently. In SQL Server, the database engine can implicitly convert a string containing a date into a DateTime data type. This is not the case for .NET. You need to explicitly convert a string date into the System.DateTime CLR type. (Also, I'm not sure how you are dimming those variables without a type.)

Try this:
Code:
Dim strAmount As String
Dim strDateDiff, strDateDiff2 As String
Dim date1, date2, date3 As DateTime
date1 = DateTime.Parse(txttodaydate.Text)
date2 = DateTime.Parse(txtddate.Text)
date3 = DateTime.Parse(txtdisplaydate.Text)
strDateDiff = CInt(DateDiff("d", date1, date2)).ToString()
strDateDiff2 = CInt(DateDiff("d", date1, date3)).ToString()
-Peter
 
Old August 1st, 2007, 09:22 AM
Authorized User
 
Join Date: Oct 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now it's throwing up this error.

System.NullReferenceException: Object reference not set to an instance of an object.




 
Old August 1st, 2007, 09:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

if you debug it.. which line throw the error??

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old August 1st, 2007, 10:52 AM
Authorized User
 
Join Date: Oct 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This one:

strReason = optAppReason.SelectedItem.Text

Sub InsertTempTables()

        'Sub proc that inserts info into temp tables

        'Production connection
        Dim DBConn As New SqlConnection(Application("DBConn")) 'Open a new connection
        Dim cmdInsertTempTables As New SqlCommand("spInsertTempFWPermitTables", DBConn)
        Dim sqldatenull As SqlDateTime
        sqldatenull = SqlDateTime.Null
        cmdInsertTempTables.CommandType = CommandType.StoredProcedure

        'Stored proc that inserts record into temp tables

        If lstApplicantMI.SelectedItem.Value = "Select" Then
            lstApplicantMI.SelectedItem.Value = ""
        End If

        Try
            strrdate = txtrdate.Text
            strddate = txtddate.Text
            strdisplaydate = txtdisplaydate.Text
            strlname = txtApplicantLastName.Text
            strfname = txtApplicantFirstName.Text
            strMI = lstApplicantMI.SelectedItem.Value
            strhomeph = txtHomePhone1.Value + txtHomePhone2.Value + txtHomePhone3.Value
            strworkph = txtBusPhone1.Value + txtBusPhone2.Value + txtBusPhone3.Value
            strPCPhone = txtPurchaseCoPhone1.Value + txtPurchaseCoPhone2.Value + txtPurchaseCoPhone3.Value
            dtdob = txtDOB1.Value + "/" + txtDOB2.Value + "/" + txtDOB3.Value
            strReadyDate = txtreadystand.Text
            strSSN = txtSSN1.Value + txtSSN2.Value + txtSSN3.Value
            strAddr1 = txtMailingAddress1.Text
            strAddr2 = txtMailingAddress2.Text
            strCity = txtMailingCity.Text
            strState = lstMailingState.SelectedItem.Value
            strZip = txtMailingZip.Text
            strConame = txtBusinessName.Text
            strCoAddr1 = txtBusAddress1.Text
            strCoAddr2 = txtBusAddress2.Text
            strCoCity = txtBusCity.Text
            strCostate = lstBusState.SelectedItem.Value
            strCoCounty = lstBusCounty.SelectedItem.Value
            strClass = ddlFWClass.SelectedItem.Value
            strSalesID = txtSalesTaxNo.Text
            strReason = optAppReason.SelectedItem.Text
            strPCName = txtPurchaseCoName.Text
            strPCAddr1 = txtPurchaseCoAddress1.Text
            strPCaddr2 = txtPurchaseCoAddress2.Text
            strPCCity = txtPurchaseCoCity.Text
            strPCState = lstPurchaseCoState.SelectedItem.Value
            strPCZip = txtPurchaseCoZip.Text
            strrtime = txtrtime.Text
            strdtime = txtdtime.Text
            strdisplaytime = txtdisplaytime.Text
            strsponsor = txtsponsor.Text
            strconductor = txtconductor.Text
            strFWUserID = Session("FWUserID")
            strTransactionNumber = Session("TransactionNumber")

            DBConn.Open()

            With cmdInsertTempTables.Parameters

                .Add("@vchmiddleInitial", strMI)
                .Add("@vchrtime", strrtime)
                .Add("@vchdtime", strdtime)
                .Add("@vchdisplaytime", strdisplaytime)
                .Add("@vchLastname", strlname)
                .Add("@vchfirstname", strfname)
                .Add("@dtdob", dtdob)
                .Add("@vchSSN", strSSN)
                .Add("@vchApplicantAddress1", strAddr1)
                .Add("@vchApplicantAddress2", strAddr2)
                .Add("@vchApplicantCity", strCity)
                .Add("@vchApplicantState", strState)
                .Add("@vchApplicantZip", strZip)
                .Add("@vchApplicantHomePhone", strhomeph)
                .Add("@vchApplicantWorkPhone", strworkph)
                .Add("@vchcompanyname", strConame)
                .Add("@vchcompanyaddress1", strCoAddr1)
                .Add("@vchcompanyaddress2", strCoAddr2)
                .Add("@vchcompanycity", strCoCity)
                .Add("@vchcompanystate", strCostate)
                .Add("@vchcounty", strCoCounty)
                .Add("@vchFWClass", strClass)
                .Add("@vchSalesID", strSalesID)
                .Add("@vchApplicationreason", strReason)
                .Add("@vchsponsor", strsponsor)
                .Add("@vchconductor", strconductor)
                .Add("@vchFWUserID", strFWUserID)
                .Add("@vchTransactionNumber", strTransactionNumber)
                .Add("@vchPurCoName", strPCName)
                .Add("@vchPurcoaddress1", strPCAddr1)
                .Add("@vchPurcoaddress2", strPCaddr2)
                .Add("@vchPurcocity", strPCCity)
                .Add("@vchPurcoState", strPCState)
                .Add("@vchPurcoZip", strPCZip)
                .Add("@vchPurcophone", strPCPhone)

                If strrdate = "" Then
                    .Add("@dtrdate", sqldatenull)
                Else
                    .Add("@dtrdate", CDate(strrdate))
                End If

                If strReadyDate = "" Then
                    .Add("@dtStandReadyDate", sqldatenull)
                Else
                    .Add("@dtStandReadyDate", CDate(strReadyDate))
                End If

                If strdisplaydate = "" Then
                    .Add("@dtdisplaydate", sqldatenull)
                Else
                    .Add("@dtdisplaydate", CDate(strdisplaydate))
                End If

                If strddate = "" Then
                    .Add("@dtddate", sqldatenull)
                Else
                    .Add("@dtddate", CDate(strddate))
                End If

                Dim strAmount As String
                Dim date1, date2, date3, strDateDiff, strDateDiff2
                date1 = txttodaydate.Text
                date2 = txtddate.Text
                date3 = txtdisplaydate.Text
                strDateDiff = (DateDiff("d", date1, date2))
                strDateDiff2 = (DateDiff("d", date1, date3))

                If ddlFWClass.SelectedItem.Value = "D" Then
                    strAmount = 2000
                End If
                If ddlFWClass.SelectedItem.Value = "M" Then
                    strAmount = 2000
                End If
                If ddlFWClass.SelectedItem.Value = "R" Then
                    strAmount = 200
                End If
                If ddlFWClass.SelectedItem.Value = "W" Then
                    strAmount = 700
                End If
                If ddlFWClass.SelectedItem.Value = "SR1" Then
                    strAmount = 100
                End If
                If ddlFWClass.SelectedItem.Value = "SR2" Then
                    strAmount = 200
                End If
                .Add("@mAmount", CDbl(strAmount))

                If ddlFWClass.SelectedItem.Value = "PD" Then
                    If strDateDiff < 10 Then
                        strAmount = 100
                    Else
                        strAmount = 50
                    End If
                End If
                If ddlFWClass.SelectedItem.Value = "CP" Then
                    If strDateDiff2 < 10 Then
                        strAmount = 200
                    Else
                        strAmount = 100
                    End If
                End If

            End With
            cmdInsertTempTables.ExecuteNonQuery()
            DBConn.Close()

        Catch er As Exception
            LogError(er.ToString, "Application")
            Exit Try
        Finally
            DBConn.Close()
        End Try
    End Sub

 
Old August 1st, 2007, 11:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

opt is a control?? why all the code is inside a try catch?? why not the posibly offending part?? try ...catch is a expensive call for the system...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old August 2nd, 2007, 08:42 AM
Authorized User
 
Join Date: Oct 2006
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

got it working. thanks:)






Similar Threads
Thread Thread Starter Forum Replies Last Post
converting dates: day of week stolte XSLT 2 November 28th, 2007 11:24 AM
dates again dhoward VB.NET 2002/2003 Basics 12 August 22nd, 2007 09:48 AM
Converting dates outspaced XSLT 1 November 27th, 2006 01:31 PM
Converting Dates mg1966 Classic ASP Databases 1 January 19th, 2005 09:15 AM
converting dates Bhavin Crystal Reports 1 October 13th, 2004 10:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.