Cannot load MVC4 Web project in VS 2017 or VS 2019

I have a web project, it could load fine in VS15, but in VS2019 it’s failing, saying the project is incompatible. The migration report contains the following message: ========== .csproj: The application which this project type is based on was not found. Please try this link for further information: http://go.microsoft.com/fwlink/?LinkID=299083&projecttype=E3E379DF-F4C6-4180-9B81-6769533ABE47

To resolve it:

1. Open a .csproj file using a text editor such as Visual Studio Code or notepad

2. Find the code <ProjectTypeGuids> {E3E379DF-F4C6-4180-9B81-6769533ABE47}; {349c5851-65df-11da-9384-00065b846f21}; {fae04ec0-301f-11d3-bf4b-00c04f79efbc} ProjectTypeGuids>

3. Delete the code section {E3E379DF-F4C6-4180-9B81-6769533ABE47}  and save the file.

4. Re-open the solution.

How to read DBF to Datatable

OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp;Extended Properties=dBase III");
OleDbCommand command = new OleDbCommand("SELECT * FROM Test.DBF", oConn);
oConn.Open();
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
oConn.Close();

Missing Windows Authentication Feature IIS and Windows 10

If you have windows 10 Home or SL version when might be you are unable to see Windows Authentication under IIS options when installing.

To enable “Windows Authentication” either you need to upgrade the OS to Pro version or you can run this command to enable it.

C:\WINDOWS\system32>dism /online /norestart /add-package:%SystemRoot%\servicing\Packages\Microsoft-Windows-IIS-WebServer-AddOn-2-Package~31bf3856ad364e35~amd64~~10.0.17134.1.mum

The file name will be different based on windows updates. So please update if requires.

How to get list of all always encrypted columns in SQL Server

We do have “Always encrypted” feature in SQL 2016 and later versions, from this feature we can encrypt the column data instead of encrypting whole database.  Sensitive data like credit card numbers, SSN.
We need to prepare a list of encrypted columns at some where to track for future.
Without tracking anywhere in the documents we can run the simple SQL query which returns all the columns in respective tables and encryption type.

SELECT
t.name AS TableName,
c.name AS ColumnName,
k.name AS KeyName,
c.encryption_type_desc,
c.encryption_algorithm_name
FROM sys.columns c
INNER JOIN sys.column_encryption_keys k ON c.column_encryption_key_id = k.column_encryption_key_id
INNER JOIN sys.tables t ON c.object_id = t.object_id
WHERE encryption_type IS NOT NULL

 

 

SQL Server – List all the Constraints by Table or by Column Name

List all Constraints of the Database:

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
OR
SELECT OBJECT_NAME(object_id) AS ConstraintName,
SCHEMA_NAME(schema_id) AS SchemaName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'

List Constraints  on table column:

 Select SysObjects.[Name] As [Contraint Name] ,Tab.[Name] as [Table Name],Col.[Name] As [Column Name]
From SysObjects Inner Join (Select [Name],[ID] From SysObjects Where XType = 'U') As Tab
On Tab.[ID] = Sysobjects.[Parent_Obj]
Inner Join sysconstraints On sysconstraints.Constid = Sysobjects.[ID]
Inner Join SysColumns Col On Col.[ColID] = sysconstraints.[ColID] And Col.[ID] = Tab.[ID]
WHERE Tab.[Name] ='Employee' AND Col.[Name]='DOB'
order by Col.[Name]

The provided anti-forgery token was meant for user “”, but the current user is “user@example.com”

While working with Asp.net MVC, I got HttpAntiForgeryException exception (The provided anti-forgery token was meant for user “”, but the current user is “user@example.com”) at login page. It happens when user login with valid credentials and re-directed to inner page ofr the application. At inner pages he press the browser back button and will show login page again. He entered the valid credentials again that time, this exception will generate and display on the browser.

To fix this exception: Need to add [OutputCache(NoStore=true, Duration = 0, VaryByParam= “None”)] line to your login get method.

[AllowAnonymous]
[HttpGet]
[OutputCache(NoStore=true, Duration = 0, VaryByParam= "None")]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}

What is tuple and when to use

A tuple is a data structure that has a specific number and sequence of elements. It allow us to combine multiple values with similar or different types into single object without creating a custom class. It can also be used where we have to return multiple items from the method instead of “out” keyword.
From MSDN:

Tuples are commonly used in four ways:

  • To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.
  • To provide easy access to, and manipulation of, a data set.
  • To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).
  • To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple<T1, T2, T3> object as the method argument, you can supply the thread’s startup routine with three items of data.

how to remove helper warnings at visual studio mvc view page

While working on MVC project we can get warnings in the MVC view/ partial view / layout page.

The name ‘ViewBag’ does not exist in the current context

‘System.Web.WebPages.Html.HtmlHelper’ does not contain a definition for ‘ActionLink’ …

‘System.Web.WebPages.Html.HtmlHelper’ does not contain a definition for ‘AntiForgeryToken’ …

These are due to wrong version in the config file. To fix this we need to update the version of the few things and reopen the project.

Here are few things which I have updated:

<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Version" value="3.0.0.0" />
<dependentAssembly>
  <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
  <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
  <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
  <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
  <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
  <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
  <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
  <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
  <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
  <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
  <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
  <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
 

 

How to fix CLSID {00024500-0000-0000-C000-000000000046} error when using excel component in background code?

We have  a windows Service application which uses Excel Application Object. It used to work on some machines but not all, the error which I am getting is:

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.

After some struggling I found a solution and it works, solution is:

Solution 1:

  • Create directory C:\Windows\SysWOW64\config\systemprofile\Desktop (for 64 bit Windows) or C:\Windows\System32\config\systemprofile\Desktop (for 32 bit Windows) and give full permission
  • Run program “dcomcnfg” Go to “Console Root/Component Services/Computer/My Computer/DCOM Config/” Look up Microsoft Excel and choose properties.
  • Go to Security and Launch and activation permissions->customize->add network, network service, administrator, interactive, system and everyone and give full access
  • Now go to “Identity” and select “The interactive user”.
  • Now go to “Console Root/Component Services/Computer/My Computer”,  right click Properties of “My Computer”, give full access permission to all users.
  • Solution 2:
    <system.web>
    <identity impersonate=”true” userName=”domain\jack” password=”********” />
    </system.web>