Monthly Archives: August 2011

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;