Author Archives: admin

LINQ to Entities Date Calculations

If you need to perform a DateDiff between 2 dates in your LINQ query:

var results = from r in db.Results
where r.Extra2 == id && r.CampaignID == 64
select new
{
Phone = r.Phone,
Pickup = r.PickUp,
Duration = EntityFunctions.DiffSeconds(r.PickUp,r.HangUp),
AnswerType = r.AnswerType

};

Notice the line - EntityFunctions.DiffSeconds(r.PickUp,r.HangUp)

Twitter Bootstrap Css Extension

Example - To add bottom padding to the row class of bootstrap.

  1. create a style.css file
  2. add the following code in style.css:
  3. .bottom-buffer { margin-bottom:60px; }
  4. make the div tag with "row" class look like this:
<div class="row bottom-buffer"></div>

C# Mapper Code

  1. Mapping ViewModel with Model.
  2. Make sure your viewmodel fields are named just like fields in model.
  3. EditMerchants (ViewModel)
  4. TsMerchants (Model)
  5. Downloaded via NuGet  - http://automapper.codeplex.com/
Mapper.CreateMap();
TsMerchant e = Mapper.Map(model);

Java Calendar object manipulation

Code snippet to generate first day of month and last day of a particular month. It comes in handy if you have an automated process that will run on the first of the current month to pull data from the previous month.

 


Calendar firstDayMonth = null;
Calendar lastDayMonth = null;

//initialize calendar
firstDayMonth = Calendar.getInstance();
lastDayMonth = Calendar.getInstance();
firstDayMonth.add(Calendar.MONTH, -1);
firstDayMonth.set(Calendar.DAY_OF_MONTH, 1);
firstDayMonth.set(Calendar.HOUR, 00);
firstDayMonth.set(Calendar.MINUTE, 00);
firstDayMonth.set(Calendar.SECOND, 00);
firstDayMonth.set(Calendar.MILLISECOND, 00);

lastDayMonth.add(Calendar.MONTH, -1);
lastDayMonth.set(Calendar.DAY_OF_MONTH, lastDayMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
lastDayMonth.set(Calendar.HOUR, 23);
lastDayMonth.set(Calendar.MINUTE, 59);
lastDayMonth.set(Calendar.SECOND, 59);
lastDayMonth.set(Calendar.MILLISECOND, 99);


 

 

If you run the code above in April, firstDayMonth Calendar object would be 2012-03-01 00:00:00 and lastDayMonth would be 2012-03-31 23:59:59

Http Post in C#

Performing Http Post in C#.

The following method will read from a text file and post it to the URL you specified.

private String readStreamPage(string url)
    {

        String result = "";
        StreamReader sr = File.OpenText(@"E:SourceCodecsvfile.txt");

        string strPost = sr.ReadToEnd();

        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);

        objRequest.Method = "POST";
        objRequest.ContentLength = strPost.Length;
        objRequest.ContentType = "application/x-www-form-urlencoded";

        byte[] postDataBytes = Encoding.UTF8.GetBytes(strPost);
        Stream myWriter = objRequest.GetRequestStream();
        Stream Answer = null;
        StreamReader _Answer = null;

        try
        {
            myWriter.Write(postDataBytes, 0, postDataBytes.Length);

            HttpWebResponse WebResp = (HttpWebResponse)objRequest.GetResponse();
            Answer = WebResp.GetResponseStream();
            _Answer = new StreamReader(Answer);

            string vystup = _Answer.ReadToEnd();
            Response.Write(vystup);
        }

        catch (Exception e)
        {

            return e.Message;

        }

        finally
        {
            myWriter.Close();
            myWriter.Dispose();
            Answer.Close();
            Answer.Dispose();
            _Answer.Close();
            _Answer.Dispose();

            sr.Close();
            sr.Dispose();
        }

        return result;

    }

Telerik OpenAccess ORM

I’ve been working on Telerik’s OpenAccess ORM tool. It is a pretty neat tool where it allows you to perform ReverseMapping on your existing Database Schema and generate class files for each table.

It is also great that it has built in tools that integrate with Visual Studio 2008. If you have written a lot of stored procedures in your lifetime, the OpenAccess ORM will let you pick the stored procedures you want to create a mapping for. It generates static method that return an IEnumerable that you can bind it to any Web Control.

Here’s a sample code that binds data return by stored procedure: cleanerGetPerfectScoresByDate

IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
 IEnumerable res = CustomerSatisfactionData.StoredProcedure.cleanerGetPerfectScoresByDate(scope, StartDate.SelectedDate,
                EndDate.SelectedDate);

//Bind to Grid here:

RadGrid1.DataSource = res;