RSS

Monthly Archives: July 2011

Using Custom Method in LINQ To Entity Framework (LINQ Method Cannot be Translated into a Store Expression)

I got this error when using a custom method in Linq to Entity Framework:

Here’s my code:

using (var context = DataObject.CreateContext())
{
    var _credits = context.CreditEntities;

    var _creditsSorted = _credits.AsQueryable().OrderBy(sortExpression, null);

    return _creditsSorted.Select(x => Mapper.Map(x)).ToList();
}

The error occurs on line 7.

The problem is caused by calling Mapper.Map() from the object that doesn’t recognize (or can’t implement) it, in this case Entity Framework (LINQ to Entity Framework).

To solve the problem, I can’t use Entity Framework to call my custom method. So, the object needs to be converted. In my case (line 3):

var _credits = context.CreditEntities.ToList();
Advertisements
 
Leave a comment

Posted by on July 31, 2011 in General

 

Tags: , , ,

Use Extension Method as Static Method

In my extension method post, I have explained how extension method works. However, there is also one thing about extension method we need to know. That is: extension method can be used like a static method. Taking from my previous example:

string inputPassword = "ExtensionMethodIsCool";
IsValidPassword(inputPassword);
 
Leave a comment

Posted by on July 31, 2011 in General

 

Tags: ,

The Last (Hopefully) NUnit Error: The Type Initializer for … Threw an Exception (And app.config)

After series of NUnit Error: The Type Initializer for … Threw an Exception: First One and Same One, I concluded (and hopefully this is the last post for the topic):

  1. If you open NUnit project with NUnit, the app.config file will have to be in the same folder as the NUnit project file and have the same name as the project file name:
    C:\Users\Documents\Nunit\ProjectTest.nunit
    C:\Users\Documents\Nunit\ProjectTest.config
  2. If you open Visual Studio .dll file, the app.config will have to be in the same folder as the project file and have the same name as the project file name (including the extension):
    C:\Users\Documents\Nunit\ProjectTest.dll
    C:\Users\Documents\Nunit\ProjectTest.dll.config
  3. If you open Visual Studio project (.csproj) or solution (.sln) file with your NUnit, the app.config will have to be in the same folder as the project file and have the same name as the project file name:
    C:\Users\Documents\Nunit\ProjectTest.csproj
    C:\Users\Documents\Nunit\ProjectTest.config
 
Leave a comment

Posted by on July 27, 2011 in General

 

Tags: , , , , ,

Default Button x Default Focus x ASP.Net Button

To set a default button in your form:

<form id="form1" runat="server" method="post" defaultbutton="btnSave">

To set a default focus in your form:

<form id="form1" runat="server" method="post" defaultbutton="btnSave" defaultfocus="txtFullname">

Keep in mind:

  1. By default, UseSubmitBehavior property in a ASP.Net button is true, this button will be the default button in the form.
  2. UseSubmitBehavior cause a postback when clicked. Be aware of this when using Javascript to submit the form.
  3. When 2 ASP.Net buttons with UseSubmitBehavior set to true, the first button will be the default button.
  4. When ASP.Net button is a default button in the form, it will automatically focus on that button.
  5. To avoid submit behavior of a button, set UseSubmitBehavior to false.
  6. This will also set the focus off from default button.
  7. To set default focus to nowhere, use defaultfocus="#" in the <form>.
 
Leave a comment

Posted by on July 27, 2011 in General

 

Tags: ,

Same Error in NUnit: The Type Initializer for … Threw an Exception (And app.config)

I have already copied app.config file as instructed here but still get the same error. So, what could be the problem here?

Apparently, this error simply means the system not able to access database, namely: Connection String problem.

This is how I access my connection string:

System.Configuration.ConfigurationSettings.AppSettings.Get("DatabaseEntities");

Above code will not return the connection string I have in web.config file. I use Entity Framework and in Entity Framework, the correct way to get connection string is:

System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseEntities"].ConnectionString;
 
2 Comments

Posted by on July 26, 2011 in General

 

Tags: , , , , ,

File Upload Service x Base64 x Byte[] x All in C#

It’s SOA age and I am going to post on how to create File Upload service to be used in SOA design. This is only a basic concept and NOT a step-by-step, very-detail-cliff-note type of guidance.

Normally, to allow file upload in a form, you would include the following enctype in your <form>

enctype="multipart/form-data" runat="server"

This is a traditional web form uploader. More on this later. In SOA, we don’t need this. Well, obviously… because it’s SOA! And because of that too, it’s much simpler than web form. All we need are:

  1. A service (REST or SOAP) that will take HTTP POST data.
    The service will take HTTP POST data, parse it out and pass it to Conversion class. The POST is basically a Base64 string of uploaded file. That being said, to invoke this service, service consumers will need to convert files to Base64 string. Then when invoking the service, the Base64 string is passed on POST data.
  2. Conversion class to convert Base64 to Byte[] and from Byte[] to Stream (File or Memory).
    Conversion class is just a class to convert from Stream > Byte[] > Base64 and other way around.

.Net framework already has static method to convert Byte[] to Base64:

Convert.ToBase64String();
Convert.FromBase64String();

Remember, the order of conversion is:

Stream > Byte[] > Base64

Whether you are converting from file to Base64 or vice versa, you will first have to convert to Byte[]. As for the Stream, this can be System.IO.FileStream (for file) or any other Stream such as System.IO.MemoryStream.

Here’s my conversion class look like:

/// <summary>
/// Convert given file to Base64 string.
/// </summary>
/// <param name="filename">Full path of the file to be converted. Ex: "C:\My Files\Kiba.jpg"</param>
/// <returns>string</returns>
public static string FileToBase64(string filename)
{
    return Convert.ToBase64String(FileToByteArray(filename));
}

/// <summary>
/// Convert given file to byte array.
/// </summary>
/// <param name="filename">Full path of the file to be converted. Ex: "C:\My Files\Kiba.jpg"</param>
/// <returns>byte[]</returns>
public static byte[] FileToByteArray(string filename)
{
    byte[] returnByteArray = null;
    // Instantiate FileStream to read file
    System.IO.FileStream readFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);

    // Instantiate BinaryReader and attach FileStream with the object
    System.IO.BinaryReader readBinaryReader = new System.IO.BinaryReader(readFileStream);

    // Get file's byte length
    long fileByteSize = new System.IO.FileInfo(filename).Length;

    // Read bytes from the file
    returnByteArray = readBinaryReader.ReadBytes((Int32)fileByteSize);

    // Clean up / disposal
    readFileStream.Close();
    readFileStream.Dispose();
    readBinaryReader.Close();
    return returnByteArray;
}

/// <summary>
/// Convert given Base64 string to file based on given filename.
/// </summary>
/// <param name="base64String">String of Base64</param>
/// <param name="filename">Full path of the file to be converted. Ex: "C:\My Files\Kiba.jpg"</param>
public static void Base64ToFile(string base64String, string filename)
{
    byte[] fileByteArray = Convert.FromBase64String(base64String);
    // Instantiate FileStream to create a new file
    System.IO.FileStream writeFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
    // Write converted base64String to newly created file
    writeFileStream.Write(fileByteArray, 0, fileByteArray.Length);
    // Clean up / disposal
    writeFileStream.Close();
}

I will discus traditional web form upload on future post. Meantime, this link will be helpful: Web form file upload.

 
Leave a comment

Posted by on July 26, 2011 in General

 

Tags: , , , ,

Placeholder Input Field for Non-HTML5 (with jQuery)

Hagenburger has a very nice fix, with jQuery, for placeholder input that will work for all other non-html5-supported browser. You know, the placeholder text is the input field that has text like “Search…” but will will be gone when the input field is on focus.

The fix actually utilize the placeholder attribute of input tag, so after implementing Hagenburger‘s jQuery, the placeholder text will work in pretty much all browsers, old and new.

HTML5 Input Placeholder Fix With jQuery

 
Leave a comment

Posted by on July 23, 2011 in General

 

Tags: , ,

 
%d bloggers like this: