Fransiscus Setiawan | EV Charging & Azure Solution Architect | Sydney

Technical Insights: Azure, .NET, Dynamics 365 & EV Charging Architecture

Getting the last day for each month in SQL server

This is the function in SQL server to get the last day for each month until the day that you define in a variable which is in this case is “@genDate”.

I used this function to iterate some stored procedure every last day in the month

DECLARE @genDate datetime
SET @genDate = '1/31/2006'

WHILE @genDate <= getdate()
	BEGIN
		PRINT @genDate
		SET @genDate = DateAdd(month,1,@genDate)

		--this is used to get the last day for each month
		SET @genDate = DateAdd(day, -1, DateAdd(month, DateDiff(month, 0, @genDate)+1, 0))
	END

NOTE: “-1” in this statement means that it’s minus one day from the beginning of the month which gives you the last day for each month
SET @genDate = DateAdd(day, -1, DateAdd(month, DateDiff(month, 0, @genDate)+1, 0))

COMException (0x80040154): Retrieving the COM class factory for component with CLSID

I’ve got this error when i tried to deploy my service with .NET wrapper for VB6 component :
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} failed due to the following error: 80040154
.

The workaround of this problem are:

  • Go to regedit and check for this entry {D8749532-0D3D-43B6-973C-C1D62BB6B3C7} ,(replace this with your clsid key) which is under “\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID”, If it is not exist then it almost certainly that you haven’t registered your VB6 component it self
  • Now you need to go to your project folder and find a reference folder and then copy the DLL that you created before (there will be several DLL under that folder, I believe you need to pick DLL with the largest size) and copy it to the same folder with your .NET DLL on production server
  • Register that DLL using “regsvr32 filename” (e.g regsvr32 APLCryptoCore.dll) and then restart your service and then it should be working
  • Get Selected Item with Gridview in ASP.NET

    I was having a problem before to get selected item with grid view when you click edit button. After spending sometime researching on the internet. I found that we can get the item that you selected by using DataKeynames

    
    
          
          
          
          
          
          
          
    
    
      Protected Sub gvPlayerSearch_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvPlayerSearch.RowCommand
    
                If (e.CommandName = "ViewResult") Then
                   //casting the object to gridview
                    Dim gv As GridView = DirectCast(sender, GridView)
    
                    Dim objEventResults As  MembersDatasetTableAdapters.EventsResultByMemberIDTableAdapter = _
                                            New   MembersDatasetTableAdapters.EventsResultByMemberIDTableAdapter()
    
       //get the selected ID
       Dim dtEventResults As MembersDataset.EventsResultByMemberIDDataTable = _
                                    objEventResults.EventsResultByMemberID(gv.DataKeys(0).Value)
    
       gvPlayerListEvent.DataSource = dtEventResults
       gvPlayerListEvent.DataBind()
      End If
    End Sub
    

    Dynamic array in VB/VB.NET/ASP

    Time to go back to old school ASP, I found that i need to create an array that contains a list of languages from a table in database. I would like to make this array length as many as the number of rows in language table. yes you can do it through keyword of ReDim and Preserve

         <%
                    dim objLanguage
                    dim languageCount
                    dim languageArray()
    
                    languageCount = 0
    
                    set objLanguage = Server.CreateObject("ADODB.Recordset")
                    objLanguage.ActiveConnection = MM_CTG_STRING
                    objLanguage.Source = "SELECT * FROM Languages"
                    objLanguage.CursorType = 0
                    objLanguage.CursorLocation = 2
                    objLanguage.LockType = 3
                    objLanguage.Open()
    
                    WHILE NOT objLanguage.EOF
                        response.Write("

    ") response.Write(objLanguage("Language")) response.Write("

    ") ReDim Preserve languageArray(languageCount) languageArray(languageCount) = objLanguage("LanguageID") languageCount = languageCount + 1 objLanguage.MoveNext() WEND objLanguage.Close() Set objLanguage = nothing %>

    How to clear a gridview

    Normally, we would like to clear a gridview for various reason. I want the data to be cleared from grid view everytime user does the search and the grid view will be binded again if the user click a particular button otherwise the last result will be displayed when the new dataset/datatable does not return any rows. This is the simplest way i ever think of

    gvPlayerListEvent.DataSourceID = String.Empty
    gvPlayerListEvent.DataBind()
    

    Only one instance of script manager can be added to the page

    I’ve got this error when i have ajax toolkit script manager on the master page while at the same time i’ve script manager on the content page. This is the primary cause of this error.

    The work around this is to move the script manager which is located on the master page before the content place holder or if you have any user control which uses script manager as well, place before it. It should be placed on the top of anything which uses this control and the most important thing you need to change the script manager in the content page or user control to be script manager proxy

    App_GlobalResources maps to a directory outside this application, which is not supported

    I got this error when i switched the default web server in visual studio to use IIS instead of visual studio integrated web server. To fix this error is by changing the IIS Default Web Site Properties in (Home Directory -> Local Path). Remove any “\” character at the end of the directory. IIS will automatically add one which causing double “//” at then end and it causes problem if you manually add one before. You can check Default Web Site Properties -> ASP.NET -> File Location for actual setting information

    407 Proxy authentication required

    I found this problem when i try to do any of httpweb request behind proxy and i got error of “407 proxy authentication required”. Normally your company has its own proxy server and sometimes you want to call webservice or reading XML from any website or sending xml to payment gateway.

    The workaround this is to use proxy properties of your httpwebrequest variable. this is some snippet

             Dim objRequest As HttpWebRequest
             objSendXML = New XmlDocument
             objRequest = WebRequest.Create(strPaymentURL)
             objRequest.Method = "POST"
             objRequest.ContentLength = strSend.Length
             objRequest.ContentType = "text/xml"
    
            'for development only!!! have to be blocked on production
             objRequest.Proxy = New System.Net.WebProxy("http://proxy-syd:8080", True)
             objRequest.Proxy.Credentials = CredentialCache.DefaultCredentials
    

    Now you can call your webservice or reading xml behind the proxy server

    not valid Base64 character when doing redirect with query string

    I was having a problem in my checkout page, the problem that i had was i’ve got this memberID from the checkout page and it’s being passed to another page via query string. This problem doesn’t happen everytime and it happens rarely, this is what i hate the most!!. After spending a couple of hours, i found the problem is in the query string it self.

    when you pass something like this in query string

    “c+ckDQiiilyW4r7moRA8oQ==”

    and it will be automatically converted becoming

    “c ckDQiiilyW4r7moRA8oQ==”

    and when you try to decrypt it, it will throw the exception of “not valid Base64 character”

    “+” when you pass it to url it will be automatically converted to ” “(blank space)

    The work around of this problem is

    on the target page, you can do some string replace of ” “(blank space) with “+” , and voila it works!!!!

    Dim strQuery As String = Request.QueryString("OrderID")
    strQuery = strQuery.Replace(" ", "+")
    

    Failed to enable constraints, one or more rows contain values violating non null, unique or foreign-key constraints

    I found this error on my project. Well i spent around one hour to figure out this problem. People might think that this is some silly error message.

    The error message i got is “Failed to enable constraints, one or more rows contain values violating non null, unique or foreign-key constraints”.

    this is caused by my stored procedure which is

    SELECT e.eventid,e.event,e.eventdate,i.email,u.username
    ,u.firstname,u.surname,i.senttime,i.readtime,i.respond
    FROM invitefriends i
    inner join users u ON i.franchiseeid=u.userid
    inner join events e ON i.eventid = e.eventid
    WHERE i.franchiseeid is not null
        and ( (@EventID IS NULL) or (e.eventid=@EventID) )
    ORDER BY e.eventdate DESC
    GO
    

    Since the query is returning multiple rows with the same eventid and the primary key in my datatable is eventid then it caused the error.

    There are two workaround to this problem:

  • by using identity from your own table or you can generate it from your query and you need to regenerate your datatable and make sure check the primary key in datatable since it’s not automatically changed for you.
  • You can also relax the constraint by removing the Primary key on the DataTable
  • Page 18 of 19

    Powered by WordPress & Theme by Anders Norén