Thursday 31 March 2011

Enable multiple site bindings in WCF 4.0

 

When trying to access my WCF hosted in IIS7..I am getting this error.

This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection.
Parameter name: item

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection.
Parameter name: item
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentException: This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection. 
Parameter name: item]
   System.ServiceModel.UriSchemeKeyedCollection.InsertItem(Int32 index, Uri item) +15583484
   System.Collections.Generic.SynchronizedCollection`1.Add(T item) +65
   System.ServiceModel.UriSchemeKeyedCollection..ctor(Uri[] addresses) +62
   System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) +266
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) +42
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +427
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath) +604
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +46
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +654
 
[ServiceActivationException: The service '/MBPublicScheme/Default.svc' cannot be activated due to an exception during compilation.  The exception message is: This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection. 
Parameter name: item.]
   System.ServiceModel.AsyncResult.End(IAsyncResult result) +15700960
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +15623609
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, Boolean flowContext) +265
   System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +227
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171
 
 
I have gone through so many sites and did tried all the possible solutions like :
 
 With ASP.NET 4.0, add the following lines to your web.config: 




        <system.serviceModel> 
             <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
        </system.serviceModel> 


        With ASP.NET 2.0/3.0/3.5, add the following lines to your web.config:

        <system.serviceModel> 
             <serviceHostingEnvironment> 
                  <baseAddressPrefixFilters> 
                       <add prefix="httplinktoyoursite"/
                  </baseAddressPrefixFilters> 
             </serviceHostingEnvironment> 
        </system.serviceModel> 

         

They may working in different scenarios but in my case i have to remove the one of the bindings in IIS.

clip_image002[4]


 


 


clip_image002[6]


Remove the binding with IPAddress.

Friday 18 March 2011

Remove Decimal Places From Currency Format

 

Solution 1 :

Use the following format in String.Format({0:c} , amount)

{0:c} default

{0:c0} for no decimal

{0:c3}for 3 decimal places and so on.

add 1,2,3 any number after c for that many decimal places

 

Solution 2 :

Dim nfi As New System.Globalization.NumberFormatInfo

nfi.CurrencyDecimalDigits = 0
nfi.CurrencySymbol = "£"

Dim amount As String = "300.440000000000"
Me.Label_Amount.Text = String.Format(nfi, "{0:c}", amount)

 

To Use the current culture instead of creating new one use this.

Dim nfi = DirectCast(System.Globalization.NumberFormatInfo.CurrentInfo.Clone(), System.Globalization.NumberFormatInfo)

nfi.CurrencyDecimalDigits = 0     

Dim amount As String = "300.440000000000"
Me.Label_Amount.Text = String.Format(nfi, "{0:c}", amount)

Thursday 10 March 2011

VB Date Format

VB uses two different date locales - the system locale that determines the output format of the date, and the code locale, which is ALWAYS U.S. English. Using the "#" delimiter (as recommended by MSDN) forces the date to be interpreted as the code locale - U.S. English, and thus mm/dd/yyyy; the system locale has no influence on this.

However, VB tries to be helpful when interpreting an entered date, and this can cause no end of trouble and confusion. For example:

    #18/04/2002# is interpreted as 18th April 2002

    #04/18/2002# is also interpreted as 18th April 2002

    #08/04/2002# is interpreted as 4th August 2002

It seems that the date is taken as mm/dd/yyyy when it makes sense, otherwise VB assumes that it's dd/mm/yyyy, and does it's own format conversion behind the scenes. This problem is compounded when dates are passed to SQL Server as part of an embedded SQL statement.

It is far, far better to ignore the MSDN advice, and pass dates as SQL character strings, always pre-formatted to the desired date format. For example:

"Where startdate > '" & format$(Now, "mm/dd/yyyy") & "' "


Note that the date string MUST be enclosed in single quotes, as above.


This can still cause problems, though, depending on the date format that your particular SQL Server installation is expecting. One solution is to include a "SET DATEFORMAT mdy" statement at the start of the Select statement. A better solution is to always format the date in a way that is not open to misinterpretation by SQL Server; dates such as 2002-08-04 and 04 Aug 2002 seem to be translated OK irrespective of the date format settings in either VB or SQL Server.



So, the above "Where" statement could be coded as either:



"Where startdate > '" & format$(Now, "yyyy-mm-dd") & "' "


or



"Where startdate > '" & format$(Now, "dd mmm yyyy") & "' "


Both of these will work as expected.



As an aside, I don't think that it's a good idea to use "short date" and "long date" as part of a format$ function, where the formatted dates are being used as part of a SQL statement, as these formats can be defined differently on different machines. Thus, a piece of code might work fine on machine A, but either crashes or produces incorrect results on machine B.



 http://www.vb-helper.com/bug_sql_dates.html



 My Problem:



I have one stored procedure and one view.




View :



“SELECT * FROM [dbo].[vwOutstandingHBVBSInstructions] WHERE [Instruction Received] >= '" & Format$(txtFromDate.Value, "mm/dd/yyyy") & "'" & " AND [Instruction Received] <= '" & Format$(txtToDate.Value, "mm/dd/yyyy") & "'"



Its working fine without any problem.



If I use the same date format mm/dd/yyyy while passing date parameters to stored procedures it is changing the format to dd/mm/yyyy(Initially i am very confused and couldn’t figure out the problem until i used SQlProfile to test the parameters it is passing to the stored procedure and view).



    Dim comm As New ADODB.Command

    comm.ActiveConnection = conn


    comm.CommandText = "spMyProc"


    comm.CommandType = adCmdStoredProc


    comm.Parameters.Append comm.CreateParameter("@FromDate", adDBDate, adParamInput, 100, Format$(txtFromDate.Value, "mm/dd/yyyy"))


    comm.Parameters.Append comm.CreateParameter("@ToDate", adDBDate, adParamInput, 100, Format$(txtToDate.Value, "mm/dd/yyyy"))


    Set objRS = comm.Execute



this format is giving error.



So i have to changed it to ‘dd/mm/yyyy’