adsense

Sunday, December 31, 2023

Integrating Vue with .Net Core

 When integrating Vuie Js with .Net Core 6/7, I found this youtube video really helpful.  As .Net core APi and Vue js run in its own port I had to first run the API in one browser instance and Vue js frontend in another instance (typically http://localhost:8080/)


Cheers,

Samitha 

Saturday, December 30, 2023

Fixing 'Access-Control-Allow-Origin' error in ASP.NET Core 6/7

 When working with a WEB API if you want to share resources between different domains we need to enable CORS in Program.cs as shown below.


app.UseCors(builder => builder
       .AllowAnyHeader()
       .AllowAnyMethod()
       .AllowAnyOrigin()
    );

Note that it is recommended to have this after app.UseRouting(); middleware.

Cheers,
Samitha

Friday, December 29, 2023

Multiple Directories for Static Files in ASP.NET Core

 I was under the impression Asp.Net core can have only one directory for static files. Recently I came across this article and it shows how we can configure multiple directories for Static Files in ASP.NET Core.

Cheers,

Samitha

Thursday, December 21, 2023

Windows 11/10: uninstall problematic Programs


Recently my machine gave me some trouble when trying to remove some app from it. I came across this useful post having options remove problematic apps/programs from a windows 10/11 PC.

From the given list Method V - Run the Microsoft Install/Uninstall troubleshooter was the quickest one worked for me.


Cheers,

Samitha



Thursday, December 14, 2023

Response Codes in WEB API

 When designing WEB APIs we, as developers are interested in response codes returned after doing an operation.


Following are the response codes and there meanings

  • 1xx: Informational – Indicates transfer protocol-level information.
  • 2xx: Success – This confirms that the client’s request was accepted successfully.
  • 3xx: Redirection – Request is not completed. The client must take some additional action to complete their request.
  • 4xx: Client Error – Indicates that there is some error in API code.
  • 5xx: Server Error – Indicates an error occurred due to some environment settings.

Regards,

Samitha

Sunday, October 29, 2023

GitHub Codespaces

 GitHub Codespaces templates provides you quick and easy development setup you need, Check this video for more information.


Regards,

Samitha

Sunday, October 22, 2023

ASP.NET Core 6/ASP.NET Core 7: Accessing Configuration during startup

 With the changes to Program.cs in .Net Core 7, we can still access confirmation options as below

appsettings.json

  "Settings": {

    "AccessTokenKey": "abcd123",

    "AccessTokenExpirySeconds": 3600,

    "RefreshTokenExpiryDays": 30

  } 

}

Program.cs

var builder = WebApplication.CreateBuilder();

ConfigurationManager config= builder.Configuration; // allows  access to  the config

IWebHostEnvironment environment = builder.Environment;// access the environment

                    var key = builder .Configuration["Settings:AccessTokenKey"]; 

Cheers,

Samitha 

Sunday, October 1, 2023

.NET 7: a connection establish error

 When working with .Net 7 WEB API projects you might have encountered following error when connection to SQL Server.

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.).

This occurs due to a breaking change in SQL driver.

This issue can be resolved by adding TrustServerCertificate=True attribute to your connection string as shown below.


"ConnectionStrings": {

    "DefaultConnection": "Server=SQL_SERVER_NAME;Database=DATABASE_NAME;Integrated Security=True;TrustServerCertificate=True;",

}

 Cheers,

Samithaa

Saturday, September 9, 2023

Return several types from a single method

 As  a backend developer, I found OneOf package is pretty useful in returning several types from a one method.

With OneOf  you van return,

  • objects
  • validation errors
  • and  try..catch errors

 from the service.

You can download OneOf package here and read more about usage here.

Regards,

Samitha


As a backend developer, I have primarily used this package to return the objects, validation errors, and try..catch errors from the service.

Saturday, August 5, 2023

Thursday, June 29, 2023

asp.net datatable sort numeric stored as varchar

 There was a table storing employee pay details as varchar. There was a requirement to sort the data ascending/descending on the UI. But it would not sort as data type is varchar.

  To resolve the issue I have implemented a workaround as follows and it worked well. 

1. Clone the data table

2. change the desired column's data type

3. change the desired column's data type    

4. Bind datatable to the data source

Sample code is as follows

Public Function GetDataTable(ByVal sortByExprerssion As String) As DataTable
       
        Dim dtBulkSalary As DataTable

        dtBulkSalary = bulkSalary.SelectByFilter()

        'workaround to sort varchar
        If (Not IsNothing(dtBulkSalary)) Then
            Dim dtBulkSalaryCloned As DataTable = dtBulkSalary.Clone()
            dtBulkSalaryCloned.Columns("Yearly_Salary").DataType = Type.[GetType]("System.Decimal")           
            dtBulkSalaryCloned.Columns("Normal_Rate").DataType = Type.[GetType]("System.Decimal")
            
            For Each bulkSalaryRow As DataRow In dtBulkSalary.Rows
                Dim drBulkSalaryCloned As DataRow = dtBulkSalaryCloned.NewRow
                drBulkSalaryCloned("BulkSalaryChange_ID") = bulkSalaryRow("BulkSalaryChange_ID")
                drBulkSalaryCloned("Yearly_Salary") = If(IsDBNull(bulkSalaryRow("Yearly_Salary")), 0.0D, CDec(DecryptValue(bulkSalaryRow("Yearly_Salary"))))
                drBulkSalaryCloned("Normal_Rate") = If(IsDBNull(bulkSalaryRow("Normal_Rate")), 0.00, CDec(DecryptValue(bulkSalaryRow("Normal_Rate"))))
                
            dtBulkSalaryCloned.AcceptChanges()
            Dim dvBulkSalary As DataView = dtBulkSalaryCloned.DefaultView
            dvBulkSalary.Sort = sortByExprerssion
            dtBulkSalary = dvBulkSalary.ToTable()
        End If

        Return dtBulkSalary
    End Function

Please note that data decrypted function is not listed here.

Cheers,
Samitha


Sunday, May 21, 2023

Database First Scaffold-DbContext Build failed error

 When you are building entity classes with Scaffold-DbContext sometimes you might encounter the Build failed error.

We can avoid this error by making sure your project does not have compile time errors. That mean the project should build successfully.


Cheers

Samitha


Friday, April 28, 2023

web application exists on both the local IIS web server and the IIS Express web server

When debugging the website in VS 2022   I got this strange issue, Following steps sorted the issue for me.


  • close VS 2022
  • Delete the files in C:\Users\<username>\Documents\IISExpress\config folder
  • Open the solution and tru running it again


Cheers,

Samitha

Sunday, April 9, 2023

Thursday, March 23, 2023

The type 'ASP.global_asax' exists in both DLLs

When trying to compile .Net web project I encountered following error.


"The type 'ASP.global_asax' exists in both 'c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\timesheet\15a75c54\898b48b9\assembly\dl3\58b062b2\00ceda54_c98cc801\App_global.asax.DLL' and 'c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\timesheet\15a75c54\898b48b9\App_global.asax.q_h6dbfx.dll' c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\timesheet\15a75c54\898b48b9\App_Web_admin.master.fdf7a39c.zecazgwd.0.cs "


When further troubleshooting in found that problem occurred in a virtual application's bin folder. Inside bin-folder there were two files.


App_global.asax.dll

App_global.asax.compiled


Removing these resolves the error for me.


Cheers

Samitha


Tuesday, March 7, 2023

jquery dialog remove title bar

If we wanted to remove  jquery dialog  title for a specific modal, use create event as show below
 $("#details").dialog({
    modal: true,
    create: function() {
      $(".ui-dialog").find(".ui-dialog-titlebar").css({
        'background-image': 'none',
        'background-color': 'white',
        'border': 'none'
      });
    }
  });

Cheers,
Samitha


Monday, February 20, 2023

SQL RAISERROR pass parameter




    If we want to parameterise SQL  RAISERROR  function  we can easily do that as display below.

 

DECLARE @Msg VARCHAR(100)

SET @Msg = 'Number exceeded on ' +FORMAT (getdate(), 'dd/MM/yyyy')

RAISERROR (@Msg , 16, 1) 


Cheers

Samitha

Friday, February 3, 2023

Telerik Radeditor show remaining character count

We can use jQuery to show remaining character count for a RadEditor as shown below.  This will wok for both design and HTML modes. Here MaxTextLength is considered as the upper character limit,

JS

var textLength;

var maxTextLength;

 function radEditorOnClientLoad(editor, args) {

   maxTextLength = editor.get_maxTextLength();

    textLength = editor.get_text().length;

       if (textLength > maxTextLength)

        $('#charCountSpan' ).html("Characters Remaining: Limit Exceeded ");

    else

        $('#charCountSpan').html("Characters Remaining: " + (maxTextLength - textLength) );

    

    $(editor.get_textArea()).on("input propertychange change keyup", ChangeCharacterCount);

    $(editor.get_contentArea()).on("input propertychange change keyup", ChangeCharacterCount);

}

function ChangeCharacterCount( ) {

var editorElement = $find(<%= txtDetails.ClientID %>);

textLength = editorElement.get_text().length;

                if (textLength > maxTextLength)

                    $('#charCountSpan').html("Characters Remaining: <span class='LimitExceed'>Limit Exceeded </span>");

                else

                   $('#charCountSpan').html("Characters Remaining: " + numberWithCommas(maxTextLength - textLength));

}

HTML

 <telerik:RadEditor ID="txtDetails" runat="server" TextMode="MultiLine"   MaxTextLength ="2000" OnClientLoad="radEditorOnClientLoad" ></telerik:RadEditor>

 <span id="charCountSpan" runat ="server" class="float-right"></span>



Cheers,
Samitha