Wednesday, November 30, 2011

Could not load file or assembly 'Microsoft.ReportViewer.Common

You may come across the above error while configuring and deploying SQL Server Reporting Services. Complete error will look like

Error: Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified

Problem: You are missing the installation and configuration info for ReportViewer

Solution: Check the following things

1. Have you installed the Report Viewer? If not you need to download and install it from Microsoft using this link
 
http://www.microsoft.com/downloads/details.aspx?FamilyID=a941c6b2-64dd-4d03-9ca7-4017a0d164fd&displaylang=en
 
2. Reboot the Machine on which you installed the Report Viewer

3. Make sure that following configuration is added in your web.config file

<system.web>
    <httpHandlers>
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
    </httpHandlers>
</system.web>

Thursday, November 10, 2011

The Controls collection cannot be modified because the control

If you are using ASP.NET and JavaScript, also using some client side code to read Server side variables, you may get an error specially when you put that JavaScript Code inside Head section of the pager or if you are doing a dynamic operation such as exporting the Form’s data to Excel. Your error may look like following

Error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Problem: Page is unable to read the data at client side when server variables are not yet ready.

Solution: Put a Div around your script tag and add runat=”Server”, Example is given below

<div id="Div1" runat="server">
    <script language="javascript" type="text/javascript">
 
        function DoSomething(aValue, bValue) {
            document.getElementById("<%=txtAValue.ClientID%>").value = aValue;
            document.getElementById("<%=txtBValue.ClientID%>").value = bValue;
            document.getElementById("<%=btnShowData.ClientID%>").click();
        }
 
   </script>
</div>

Thursday, September 15, 2011

Error: The [Service_Name] service terminated unexpectedly

You may see the following error in your Event Log, specially when running a Windows Service.

The [Service_Name] service terminated unexpectedly.  It has done this 1 time(s).

Problem: There could be multiple reasons for the above. But if you find that your Service executable file (.exe) has been automatically deleted then guess no further, you must be running an AntiVirus Program, which may have recently updated its definition and finding your safe service program as a threat, so it is deleting when program is trying to run.

Solution: Go to AntiVirus console and find an option to exclude the program folder or path from being checked.

You may need an administrative privilege to  perform this operation.

Tuesday, August 23, 2011

WCF Service Schema are not being updated

You may face a situation, when you are updating schema of a WCF Service under another service or .NET project, but unfortunately, the project is not updating the schema.

Solution: Make sure that project’s build output path is pointing to bin\

Friday, July 15, 2011

Multi-threaded Parallel Processing

Many times you come across a situation when you have to execute the same logic across different clients or batch. While running them in simplest manner without multi-threading, your records will be sequentially processed means your process will take longer time to finish. 

You can speed up the above process by implementing multi-threaded parallel processing in few lines of code. To keep it simple in this example, you have to make sure that you have distinct batches or set of records which will not interfere or overlap during this processing. Here is how you can implement in quickest possible manner, make sure your machine has enough processing and threading capabilities.

Step 1

Create a property of method so that you can dynamically set the no of threads if needed

Private ReadOnly Property MaxAllowableThreadCount() As Integer
        Get
            'You can define this value in config file 
            Dim maxThreadCount As Integer = 15
            Return maxThreadCount
        End Get
    End Property

Step 2

Following is the example of multi threading or parallel processing code. Assuming each client will have unique set of records.

Private Function ProcessAllClients() As Integer
      Dim clientList As New List(Of String)
      Dim exceptionMessages As New List(Of String)
      Dim totalNoOfRecProcessed As Integer = 0
 
      'Add some Test Clients
      clientList.Add("A1")
      clientList.Add("A2")
      clientList.Add("A3")
 
 
      If clientList IsNot Nothing AndAlso clientList.Count > 0 Then
          'Customize No of Threads you may want to use as per need or you can simply use a value
          Dim noOfThreadCounts As Integer = MaxAllowableThreadCount
          Dim noOfClients As Integer = clientList.Count
          Dim noOfThreadsGroup As Integer = 0
 
          'Adjust No of Loops as per max allowable thread
          If (noOfClients Mod noOfThreadCounts = 0) Then
              noOfThreadsGroup = CInt(Math.Floor(noOfClients / noOfThreadCounts))
          Else
              noOfThreadsGroup = CInt(Math.Floor(noOfClients / noOfThreadCounts)) + 1
          End If
 
          'Set clients in each group/loop 
          For threadGroup As Integer = 0 To noOfThreadsGroup - 1
              Dim processingTaskCounter As Integer = noOfThreadCounts
 
              If threadGroup = (noOfThreadsGroup - 1) AndAlso (noOfClients Mod noOfThreadCounts > 0) Then
                  processingTaskCounter = noOfClients Mod noOfThreadCounts
              End If
 
              Dim allProcessingTasks(processingTaskCounter - 1) As Task(Of Integer)
              Dim clinetsToProcess As New List(Of String)
              Dim taskCounter As Integer = 0
 
              For i = 0 To processingTaskCounter - 1
                  clinetsToProcess.Add(clientList(CInt(threadGroup * noOfThreadCounts) + i))
              Next
 
              For Each clientCode As String In clinetsToProcess
                  Try
                      Dim currentClient As String = clientCode
                      allProcessingTasks(taskCounter) = New Task(Of Integer)(Function()
                                                                                 Dim pHelper As New ProcessHelper
                                                                                 Return pHelper.ClientProcessingLogic(currentClient)
                                                                             End Function)
                      taskCounter = taskCounter + 1
 
                  Catch ex As Exception
                      exceptionMessages.Add("Sorry! Error Occurred." & ex.Message)
                      totalNoOfRecProcessed = 0
                  End Try
              Next
 
              'Now Execute all client's tasks as once but in seperate thread
              Array.ForEach(allProcessingTasks, Sub(tx) tx.Start())
 
              'Wait till all finishes processing
              Task.WaitAll(allProcessingTasks)
 
              For Each tsk In allProcessingTasks
                  Select Case True
                      Case tsk.IsCanceled
                          exceptionMessages.Add("Task ID" & tsk.Id & " was canceled.")
                      Case tsk.IsFaulted
                          exceptionMessages.Add("Task ID" & tsk.Id & " was faulted.")
                      Case tsk.IsCompleted
                          totalNoOfRecProcessed += tsk.Result
                      Case Else
                          exceptionMessages.Add("Task ID" & tsk.Id & " was in unknown status.")
                  End Select
              Next
          Next
      End If
      Return totalNoOfRecProcessed
  End Function

Step 3

Call the Step 2 from wherever you want to call it

Dim allItemsProcessedCount As Integer = ProcessAllClients()

Now you can see the difference in processing time and no of records processed during one call.

Wednesday, June 15, 2011

XDocument Tips

XDocument was introduced with LINQ to manipulate and query XML Document with ease. Here are some useful tips.

1. Create a simple XDocument

Dim myDoc As XDocument = <?xml version="1.0" encoding="utf-8"?><MyDocument></MyDocument>

2. Merge multiple XDcouments in one with variables and formats

Dim xDocFooter As XDocument = Nothing
Dim docName As String = "XDocument"
Dim docDate As Date = Now
 
       xDocHeader = <?xml version="1.0" encoding="utf-8"?>
                    <DocumentHeader>
                        <DocID>1</DocID>
                        <DocName><%= docName %></DocName>
                        <DocDesc>Test Document 1</DocDesc>
                        <DocDate><%= If(docDate > Date.MinValue, Format(docDate, "yyyy-MM-dd"), "1900-01-01") %></DocDate>
                    </DocumentHeader>
 
       xDocFooter = <?xml version="1.0" encoding="utf-8"?>
                    <DocumentFooter>
                        <FooterName>Footer 1</FooterName>
                        <FooterDesc>End Of Document</FooterDesc>
                    </DocumentFooter>
       myDoc.Root.Add(xDocHeader.Elements)
       myDoc.Root.Add(xDocFooter.Elements)

3. Load XML file as XDocument

Dim xdoc As XDocument = Nothing
xdoc = XDocument.Load("C:\TestXML.xml", LoadOptions.PreserveWhitespace)

4. Read XDocument from/to XML String

Dim xmlString As String = "<MyDocument>Test Doc</MyDocument>"
xdoc = XDocument.Parse(xmlString, LoadOptions.PreserveWhitespace) 'Loading from XML String
xmlString = xdoc.ToString() 'To XML String

5. Read XDocument Elements values

If myDoc.Root.Element("DocumentHeader") IsNot Nothing AndAlso myDoc.Root.Element("DocumentHeader").Element("DocName") IsNot Nothing Then
   docName = myDoc.Root.Element("DocumentHeader").Element("DocName").Value
End If
 
If myDoc.Root.Element("DocumentHeader") IsNot Nothing AndAlso myDoc.Root.Element("DocumentHeader").Element("DocDate") IsNot Nothing Then
   docDate = myDoc.Root.Element("DocumentHeader").Element("DocDate").Value
End If

6. Add New Or Modify Existing XDocument Element’s value

myDoc.Root.Element("DocumentHeader").Add(New XElement("PageNo", "1"))
myDoc.Root.Element("DocumentHeader").Element("DocDesc").Value = "New Test Document Description"

Tuesday, November 23, 2010

LINQ Group By Examples

These are 2 samples of  Group By Examples, one is very simple one but not simplest, second one is little bit intermediate. 

First creating a class to use in the given example

   1: Public Class TestRecord
   2:     Property ID As String
   3:     Property Value As String
   4:     Property RecordDate As Date
   5:  
   6:     Sub New()
   7:  
   8:     End Sub
   9:  
  10:     Sub New(ByVal id As String, ByVal value As String, ByVal recDate As Date)
  11:         Me.ID = id
  12:         Me.Value = value
  13:         Me.RecordDate = recDate
  14:     End Sub
  15: End Class

 

Now, let say use a button and Gridview to present the data on a page.

   1: Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button4.Click
   2:         Dim showSimpleGroup As Boolean = False
   3:  
   4:         Dim lstItems As New List(Of TestRecord)
   5:         lstItems.Add(New TestRecord("1", "100", Now))
   6:         lstItems.Add(New TestRecord("2", "100", Date.MinValue))
   7:         lstItems.Add(New TestRecord("3", "100", #2/2/1980#))
   8:         lstItems.Add(New TestRecord("4", "200", Date.MinValue))
   9:         lstItems.Add(New TestRecord("5", "200", #1/1/2010#))
  10:         lstItems.Add(New TestRecord("6", "300", Now))
  11:  
  12:         If showSimpleGroup Then
  13:             Dim recList = From r In lstItems Group By r.Value Into g = Group Select New With {g, .ID = g.Max(Function(p) p.ID), .Value = g.Max(Function(p) p.Value)}
  14:             Me.gvwTest.DataSource = recList
  15:             Me.gvwTest.DataBind()
  16:         Else
  17:             Dim tempList = From d As TestRecord In lstItems Group By d.Value Into g = Group
  18:                             Select New With {g, .ID = g.Max(Function(d) d.ID), .RecordDate = g.Max(Function(d) IIf(d.RecordDate = Date.MinValue, Now, d.RecordDate))}
  19:  
  20:             If tempList IsNot Nothing AndAlso tempList.Count > 0 Then
  21:                 Dim recList = From r As TestRecord In lstItems Join t In tempList On t.ID Equals r.ID Select r
  22:                 Me.gvwTest.DataSource = recList
  23:                 Me.gvwTest.DataBind()
  24:             End If
  25:         End If
  26:     End Sub

 

The first grouping will display the distinct value excluding Record Date , second one will use the record’s max date to display the record the distinct value.

Friday, November 05, 2010

WCF Service Hosted in Web Farm or Multi-Homed Scenario

If a WCF Service is deployed in a load balanced environment and hosted on multiple Web Servers under IIS, which is accessed on a public domain, WCF WSDL document refers to a the responding machine name which is generally inaccessible on a public domain. So what you should be doing to make it right?

As per Microsoft, this design was initially designed in WCF but later when Microsoft went into Azure or Cloud Computing, this situation became an issue and realized the need to fix this problem. So in order to fix this problem, you need to perform two simple steps

1. Download the following Fix from Microsoft and Install on respective machines

                         http://code.msdn.microsoft.com/KB971842/Release/ProjectReleases.aspx?ReleaseId=3228

2. Add the following sections in behavior section of configuration

   1: <serviceBehaviors>
   2:    <behavior name="<name>">
   3:      <useRequestHeadersForMetadataAddress>
   4:        <defaultPorts>
   5:           <add scheme="http" port="80" />
   6:           <add scheme="https" port="443" />
   7:         </defaultPorts>
   8:       </useRequestHeadersForMetadataAddress>
   9:    </behavior>
  10: </serviceBehaviors>
  11:  
  12:  

For more information, you can checkout the following articles

a. http://support.microsoft.com/kb/980588

b. http://support.microsoft.com/kb/971842

Saturday, October 23, 2010

Using IFRAME in ASP.NET

If you are using an IFRAME in ASP.NET and setting the height and width and not working as per your need. Spent couple of hours trying all possible options and found a very easy solution

1. Remove following line from Page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. How to Set values Dynamically

<iframe id="Testframe" runat="Server" />

<script language="vb" runat="server">

 
Protected WithEvents frame1 As System.Web.UI.HtmlControls.HtmlGenericControl
  
  Sub Page_Load()
      Dim frame1 As HtmlControl = CType(Me.FindControl("TestFrame"), HtmlControl)
      With frame1
          .Attributes("src") = "TestPage.aspx"
          .Attributes("width") = "100%"
          .Attributes("height") = "100%"
      End With
      
  End Sub
 
</script>