Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

I already posted another post today about solving Visual Studio 2018 Stuck at 'preparing solution'  
If you follow the same solution you will be able to build your solution.

Visual Studio 2018 Stuck at 'preparing solution'

Visual Studio 2018  stuck after lunching specific solution. after spending sometime on the internet,
I found the problem and the fix.

I used Community Edition for visual studio 2018.

1- Close all running instances of Visual Studio 2017
2- Launch the Visual Studio 2017 Developer Command Prompt as Admin
3- Type the following commands (replace Community with your edition, either Enterprise or Professional , or adjust the path accordingly):

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Framework.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Engine.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Conversion.Core.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Tasks.Core.dll"

gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Utilities.Core.dll"

Entityframework Oracle.ManagedDataAccess.Client.OracleException: 'ORA-01950: no privileges on tablespace 'USERS''

I used EF to connect to Oracle database and  I received the following error
Oracle.ManagedDataAccess.Client.OracleException: 'ORA-01950: no privileges on tablespace 'USERS''
In my case the connection string has invalid value.
the user password was typed wrong.


EntityFramework Oracle.ManagedDataAccess.Client.OracleException: 'ORA-01918: user 'dbo' does not exist'

While trying to use code first approach to connect to Oracle database using ASP NET C#.
I received the following exception
Oracle.ManagedDataAccess.Client.OracleException: 'ORA-01918: user 'dbo' does not exist'
My current User Is IT_USER
The Reason for the error is that Oracle tried to create my tables which are in my first migration into dbo schema as SQL Server do, but in Oracle it is a User.

So you need to till Entity Framework where is my Table Should be
You have two solutions

First is to use DbModelBuilder.HasDefaultSchema
This will make all of your tables in this schema.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.HasDefaultSchema("IT_USER"); 
    base.OnModelCreating(modelBuilder);
}

Second is to use System.ComponentModel.DataAnnotations.Schema.Table
This will give you the ability to change the schema when ever you want
[Table("STD_FA_VALIDATION", Schema ="IT_USER")] 
public class STD_FA_VALIDATION
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int SGBSTDN_PIDM { get; set; }
    public string NAT_CHANGE_IND { get; set; }
    public string WORK_IND { get; set; }
    public DateTime NAT_PROC_DATE { get; set; }
    public DateTime WORK_PROC_DATE { get; set; }
 }

Unable to create a constant value of type ‘’. Only primitive types or enumeration types are supported in this context.

the problem appears because you are using contains method in non primitive type
here is an example
Audit audit = new Audit();
List<int> ints = userReports.Select(y => y.ReportId).ToList();
List<ReportUserVisit> userReportLastMonthVisit = audit.GetAudit(userId, AuditType.Report)
                .Where(x => ints.Contains((int)x.ObjectId))
                .Select(x=> x.ObjectId)
                .GroupBy(x=>x.Value)
                .Select(x=>new ReportUserVisit { ReportId = x.Key, LastMonthVisitCount= x.Count() })
                .ToList();

note ints object, this will solve the problem.
because ints collection is list of int it can use contains method inside entity framework


How to create Extension method C#

sometimes you need to change an object to another value or to do some calculations on this object.
for example, you had a simple asp.net dropdownlist control and it contains a value for id, and these id's is integers.
so to get the value you need to do something like this
int id = Convert.ToInt32(ddl.SelectedValue);
but what if the SelectedValue was not integer, this will through an .net exception, so you need to write more code, to make sure that the selected value is not empty string. and so on.

so here comes the Extension methods.

the class must be static and the method also must be static and you need to decide which object the method will work on. it can be int, string or even object the parent class for every other classes on .net,

so, I'm gonna stick with the dropdownlist example right now.
namespace Demo
{
    public static class Extension
    {
        public static int ToInt(this string val)
        {
            int d = 0;

            if (!string.IsNullOrEmpty(val.Trim()))
                int.TryParse(val.Trim(), out d);

            return d;
        }
    }
}
as you can see here the namespace is Demo, the class is static and also the method is static.
now how to use it.
using Demo;

string str = "34";
int convertedNum = str.ToInt();
just like this, and the default value will be there, also if there was any error it will be zero.

Getting Mime types and file extensions from registry using C#

If you have a mime type and you want to find the default extension for this mime type, you can get this from the following registry key
HKEY_CLASSES_ROOT\MIME\Database\Content Type\<mime type>

public static string GetDefaultExtension(string mimeType)
{
  string result;
  RegistryKey key;
  object value;
  key = Registry.ClassesRoot.OpenSubKey("MIME\Database\Content Type\" + mimeType, false);
  value = key != null ? key.GetValue("Extension", null) : null;
  result = value != null ? value.ToString() : string.Empty;
  return result;
}

on the other hand, if you have the file extension and you want the mime type of this extension
HKEY_CLASSES ROOT\<extension>

public static string GetMimeTypeFromExtension(string extension)
{
  string result;
  RegistryKey key;
  object value;
  if (!extension.StartsWith("."))
    extension = "." + extension;
  key = Registry.ClassesRoot.OpenSubKey(extension, false);
  value = key != null ? key.GetValue("Content Type", null) : null;
  result = value != null ? value.ToString() : string.Empty;
  return result;
}

and this is all :) 

filter attribute that checks whether current connection is secured

using APS.net Core to mark all website pages working with https protocol we will do this using IAuthorizationFilter. and here is an exampl...