Format, Year, And Datetime Function not Recognized
-
Thursday, December 03, 2009 11:45 PM
I recently moved a webservice VB.net project from VS 2005 to VS 2008 and now a conversion I use to create a unique receipt number is no longer working.
The Format() function which is supposedly a Visual basic function gives the error:
'Format' is a type and cannot be used as an expression.
The Year() Function which is also a VB function gives the error:
Name 'Year' is not declared.
The Datetime() Function which is also a VB function gives the error:
'DateTime' is a type and cannot be used as an expression.
The actual code statement remains unchanged between the two web services but VS2005 compiles and VS2008 does not.
Dim julda As Long = CLng(Format(Year(oneRowDon.Item(8)), "0000") _ + Format(DateTime("d", CDate("01/01/" _ + Format(Year(oneRowDon.Item(8)), "0000")), oneRowDon.Item(8)) _ + 1, "000"))
All Replies
-
Friday, December 04, 2009 6:20 AM
Could you provide more information on what you are trying to achieve with this formatting?
(I don't know what oneRowDon is and cannot run your code.)
There are *lots* of ways to format dates.
There are features such as this:
Dim dt As DateTime = DateTime.Now Dim year As Long = CType(dt.Year & "0000", Long)Using dt.Year pulls the year value.
There are also date formatting features:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! -
Friday, December 04, 2009 7:16 AMDo you have a reference to Microsoft.VisualBasic?
Right click your project in Solution Explorer, go to references tab, go to Imported namespaces and make sure Microsoft.VisualBasic is checked.
or user other alternatives which is better than using visualbasic namespace
Arjun Paudel -
Friday, December 04, 2009 9:03 PM
In the VS2005 file there are no references and the web.config is as follows:
<?xml version="1.0"?> <!-- Note: As an alternative to hand editing this file you can use the web admin tool to configure settings for your application. Use the Website->Asp.Net Configuration option in Visual Studio. A full list of settings and comments can be found in machine.config.comments usually located in \Windows\Microsoft.Net\Framework\v2.x\Config --> <configuration> <appSettings/> <location allowOverride="true"> <system.web> <securityPolicy> <trustLevel name="Full" policyFile="internal"/> <trustLevel name="High" policyFile="web_hightrust.config"/> <trustLevel name="Medium" policyFile="web_mediumtrust.config"/> <trustLevel name="Low" policyFile="web_lowtrust.config"/> <trustLevel name="Minimal" policyFile="web_minimaltrust.config"/> <trustLevel name="ModifiedMedium" policyFile="modified_mediumtrust.config"/> </securityPolicy> <trust level="Full" originUrl=""/> <!--<identity impersonate="true" username="STUDENT" password="food"/>--> <!-- Set compilation debug="true" to insert debugging symbols into the compiled page. Because this affects performance, set this value to true only during development. Visual Basic options: Set strict="true" to disallow all data type conversions where data loss can occur. Set explicit="true" to force declaration of all variables. --> <compilation debug="true" strict="false" explicit="true"> <assemblies> <add assembly="Microsoft.Data.Odbc, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="Microsoft.SqlServer.SqlCEDest, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91"/> </assemblies> </compilation> <pages> <namespaces> <clear/> <add namespace="System"/> <add namespace="System.Collections"/> <add namespace="System.Collections.Specialized"/> <add namespace="System.Configuration"/> <add namespace="System.Text"/> <add namespace="System.Text.RegularExpressions"/> <add namespace="System.Web"/> <add namespace="System.Web.Caching"/> <add namespace="System.Web.SessionState"/> <add namespace="System.Web.Security"/> <add namespace="System.Web.Profile"/> <add namespace="System.Web.UI"/> <add namespace="System.Web.UI.WebControls"/> <add namespace="System.Web.UI.WebControls.WebParts"/> <add namespace="System.Web.UI.HtmlControls"/> </namespaces> </pages> <httpRuntime minFreeThreads="88" minLocalRequestFreeThreads="76" executionTimeout="600000" maxRequestLength="10000"/> <!-- The <authentication> section enables configuration of the security authentication mode used by ASP.NET to identify an incoming user. --> <authentication mode="Windows"/> <!-- The <customErrors> section enables configuration of what to do if/when an unhandled error occurs during the execution of a request. Specifically, it enables developers to configure html error pages to be displayed in place of a error stack trace. <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> --> </system.web> </location> <system.web> <sessionState cookieless="UseDeviceProfile" /> <customErrors defaultRedirect=""/> <trace enabled="true" localOnly="false" mostRecent="true" pageOutput="true"/> </system.web> </configuration>The VS2008 will not compile even when I add the Microsoft.VisualBasic assembly:
<?xml version="1.0"?> <!-- Note: As an alternative to hand editing this file you can use the web admin tool to configure settings for your application. Use the Website->Asp.Net Configuration option in Visual Studio. A full list of settings and comments can be found in machine.config.comments usually located in \Windows\Microsoft.Net\Framework\v2.x\Config --> <configuration> <appSettings/> <location allowOverride="true"> <system.web> <securityPolicy> <trustLevel name="Full" policyFile="internal"/> <trustLevel name="High" policyFile="web_hightrust.config"/> <trustLevel name="Medium" policyFile="web_mediumtrust.config"/> <trustLevel name="Low" policyFile="web_lowtrust.config"/> <trustLevel name="Minimal" policyFile="web_minimaltrust.config"/> <trustLevel name="ModifiedMedium" policyFile="modified_mediumtrust.config"/> </securityPolicy> <trust level="Full" originUrl=""/> <!--<identity impersonate="true" username="STUDENT" password="food"/>--> <!-- Set compilation debug="true" to insert debugging symbols into the compiled page. Because this affects performance, set this value to true only during development. Visual Basic options: Set strict="true" to disallow all data type conversions where data loss can occur. Set explicit="true" to force declaration of all variables. --> <compilation debug="true" strict="false" explicit="true"> <assemblies> <add assembly="Microsoft.Data.Odbc, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="Microsoft.SqlServer.SqlCEDest, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91"/> <add assembly="Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> </assemblies> </compilation> <pages> <namespaces> <clear/> <add namespace="System"/> <add namespace="System.Collections"/> <add namespace="System.Collections.Specialized"/> <add namespace="System.Configuration"/> <add namespace="System.Text"/> <add namespace="System.Text.RegularExpressions"/> <add namespace="System.Web"/> <add namespace="System.Web.Caching"/> <add namespace="System.Web.SessionState"/> <add namespace="System.Web.Security"/> <add namespace="System.Web.Profile"/> <add namespace="System.Web.UI"/> <add namespace="System.Web.UI.WebControls"/> <add namespace="System.Web.UI.WebControls.WebParts"/> <add namespace="System.Web.UI.HtmlControls"/> </namespaces> </pages> <httpRuntime minFreeThreads="88" minLocalRequestFreeThreads="76" executionTimeout="600000" maxRequestLength="10000"/> <!-- The <authentication> section enables configuration of the security authentication mode used by ASP.NET to identify an incoming user. --> <authentication mode="Windows"/> <!-- The <customErrors> section enables configuration of what to do if/when an unhandled error occurs during the execution of a request. Specifically, it enables developers to configure html error pages to be displayed in place of a error stack trace. <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> --> </system.web> </location> </configuration>Thank you in advance for your help. -
Sunday, December 06, 2009 4:22 AM
Hi. This is everything I maximum out from your expression. It is not running, but for example I don't understand what may DateTime(String, Date, Date) do. Neither we have track what type oneRowDone.Item(8) is.
Dim someDate As Date = oneRowDon.Item(8) Dim someYear As Integer = someDate.Year Dim firstDayOfSomeYear As Date = New Date(someYear, 1, 1) Dim julda As Long = CLng(someYear.ToString("D4") + _ Format(DateTime("d", firstDayOfSomeYear, someDate) + 1, "000"))
I couldn't ease expression more. But you may try New Date and all its options, SomeDate.ToString and its options (for example myDate.ToString("yyyy") instead Format ( obj, `0000` ). You can try myDate.AddDay(num) etc. I guess this should be some calendar date transformation?
Best wishes, Matt -
Monday, December 07, 2009 7:09 PMYeah, onerowdon.Item(8) is a sql datetime object that is converted to the julian date using that formula.
-
Tuesday, December 08, 2009 12:47 AMAh, I guess DateTime("d", date1, date2) creates Date from date1.Date and date2.TimeOfDay, is it possible, right?
-
Thursday, December 10, 2009 6:32 PM
I figured out the problem as I was searching another forum. For some reason DateTime should have been datediff and I don't know when it got switched. Year fixed itself when I added the Microsoft visualbasic namespace and I had to explicitly call format.
julda = CLng(Microsoft.VisualBasic.Format(Year(oneRowDon.Item(8)), "0000") _ + Microsoft.VisualBasic.Format(DateDiff("d", CDate("01/01/" _ + Microsoft.VisualBasic.Format(Year(oneRowDon.Item(8)), "0000")), oneRowDon.Item(8)) _ + 1, "000"))- Marked As Answer by CS_Co-op Thursday, December 10, 2009 6:33 PM
-
Tuesday, March 13, 2012 11:12 AM
Hey Friends I know the solution is found for this but I want to share the reason behind that
If you are facing the same problem then just check your reference for Microsoft.SqlServer.Server
which contains the Format type that is why you r getting that error...just remove that reference or if u r using that reference somewhere then just use Microsoft.VisualBasic.Format() instead only using Format()
- Edited by Kamlesh Pradhan Tuesday, March 13, 2012 11:12 AM

