Tag: .Net Framework

  • Convert UTF-8 string To ASCII using C#

    Ever wanted to convert a string from UTF-8 to ASCII in order to use it in a text file or to post it on another on another website? Below you will find a static function that you can use from anyware in your Visual Studio solution to convert a string from UTF-8 to ASCII. It takes as a parameter the UTF-8 string and return the same string in ASCII.

    public static string UTF8toASCII(string text)
    {
    	System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
    	Byte[] encodedBytes = utf8.GetBytes(text);
    	Byte[] convertedBytes =
    			Encoding.Convert(Encoding.UTF8, Encoding.ASCII, encodedBytes);
    	System.Text.Encoding ascii = System.Text.Encoding.ASCII;
    
    	return ascii.GetString(convertedBytes);
    }
    
  • LINQ to SQL Get DateTime from Sql Server (getDate)

    Ever wanted to get the current time and date from the SQL Server to avoid different times between yous server and the clients (for example having a centralized application running on many countries) ?

    Using LINQ to SQL there is no way to get the time from the database build in the class. What you need to do is write your own code as the example below:

        using System.Data.Linq;
        using System.Data.Linq.Mapping;
        using System.Data;
        using System.Collections.Generic;
        using System.Reflection;
        using System.Linq;
        using System.Linq.Expressions;
        using System.ComponentModel;
        using System;
    
        partial class dbGTOnlineDataContext
        {
            [Function(Name = "GetDate", IsComposable = true)]
            public DateTime GetSystemDate()
            {
                MethodInfo mi = MethodBase.GetCurrentMethod() as MethodInfo;
                return (DateTime)this.ExecuteMethodCall(this, mi, new object[] { }).ReturnValue;
            }
        }
    

    Then anywhere in your code you can simple use the code below to get the time from the SQL Server database:

    myDatabaseDataContext db = new myDatabaseDataContext();
    DateTime dtNow = db.GetSystemDate();
    
  • Crystal Reports & Framework 4 (Could not load file or assembly crdb_adoplus.dll)

    If you upgrade your project to .NET Framework 4 or for any reason you get a mysterious error like the one below:

    “Could not load file or assembly ‘file:///C:\Program Files\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll’ or one of its dependencies. The system cannot find the file specified.”

    Try inserting the code below at your app.config or web.config file:

    <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0"/>
    </startup>
    It seems that microsoft changed the binding procedure and this setting changes that.