Friday, October 16, 2009

Sort a DataTable

Here is a very easy way to sort the records in a DataTable by one of the columns.

    public static DataTable SortDataTable(DataTable dt, string sortColumn, string direction)
{
DataTable tempDataTable = dt.Clone();
int rowCount = dt.Rows.Count;
int columnCount = dt.Columns.Count;
DataRow[] allRows = dt.Select(null, sortColumn + " " + direction.ToUpper());

for (int i = 0; i < rowCount; i++)
{
object[] rowArray = new object[columnCount];
for (int j = 0; j < columnCount; j++)
{
rowArray[j] = allRows[i][j];
}
DataRow tempRow = tempDataTable.NewRow();
tempRow.ItemArray = rowArray;
tempDataTable.Rows.Add(tempRow);
}

dt.Rows.Clear();
for (int i = 0; i < tempDataTable.Rows.Count; i++)
{
object[] rowArray = new object[columnCount];
for (int j = 0; j < columnCount; j++)
{
rowArray[j] = tempDataTable.Rows[i][j];
}
DataRow tempRow = dt.NewRow();
tempRow.ItemArray = rowArray;
dt.Rows.Add(tempRow);
}

return (dt);
}

Friday, October 9, 2009

ResolveUrl

If you ever need to resolve the location of a resource in your code behind, use this sweet little function. This is especially useful in a Master Page or a User Control where the relative location of a resource will change.

Here is an example that dynamically adds some JavaScript onmouseout code for an image rollover:

imgImage.Attributes.Add("onmouseout", "src='" + ResolveUrl("~/Images/something_active.gif") + "'");

Friday, September 11, 2009

How to Add an Event Handler to Control Created in Code Behind

I hope that title makes sense to you. Basically I'm building a big form in the code behind. Sucks. But in my case I had to do it and also had to set AutoPostback and add an EventHandler to some checkboxes.

cb = new CheckBox();
cb.AutoPostBack = true;
cb.CheckedChanged += new System.EventHandler(this.CB_CheckedChanged);


"CB_CheckedChanged" is a method I created to handle the cb checks. You can basically use the same code for RadioButtonLists etc.

Thursday, September 10, 2009

Format Date in GridView

I always forget how to do this one. If you need to format a date a certain way in a
GridView, try this out:

<asp:BoundField DataField="the_date" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false" />

Friday, September 4, 2009

Syntax error: Missing operand after 's' operator.

Don't bother trying to filter a DataView by setting the RowFilter property to a string with a single quote. Make sure you escape the quotes first:

dv.RowFilter = "Something = 'O'Malley'";

becomes...

dv.RowFilter = "Something = 'O''Malley'";

Thursday, August 27, 2009

Oracle Client on Windows 7

Oracle sucks, but we're forced to use it at work. Of course Oracle can't get off their collective asses and put out a compatible client even after Windows 7 has, for all intents and purposes, been released. Here's a potential fix though:

http://msutic.blogspot.com/2009/08/how-to-instal-oracle-client-11g-on.html

Monday, August 24, 2009

Disable a Button (Client Side)

This code is great for when you want to make sure a person doesn't click a button more than once - but do it in a prettier way (you of course should verify everything in your code-behind as well, but this makes it pretty damn clear to your users):

StringBuilder sbValid = new StringBuilder();
sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");
sbValid.Append("if (Page_ClientValidate() == false) { return false; }} ");
sbValid.Append("this.value = 'Please Wait...';");
sbValid.Append("this.disabled = true;");
sbValid.Append("document.forms[0].ctl00_ContentPlaceHolder1_btnPaymentSubmit.disabled = true;");
sbValid.Append(this.Page.GetPostBackEventReference(this.btnPaymentSubmit));
sbValid.Append(";");
btnPaymentSubmit.Attributes.Add("onclick", sbValid.ToString());

Thursday, August 20, 2009

Reset Scroll Position When Using MaintainScrollPositionOnPostback

If you're using MaintainScrollPositionOnPostback="true" to, well, maintain your scroll position on postback, sometimes you still want to bump the scroll up to the top (if you're using multiple panels or whatever. Here is a nice and easy fix.

1. Put this method in your code-behind:

    private void ResetScrollPosition()
{
if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "CreateResetScrollPosition"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "CreateResetScrollPosition", "function ResetScrollPosition() {" + Environment.NewLine +
" var scrollX = document.getElementById('__SCROLLPOSITIONX');" +
Environment.NewLine + " var scrollY = document.getElementById('__SCROLLPOSITIONY');" +
Environment.NewLine + " if (scrollX && scrollY) {" +
Environment.NewLine + " scrollX.value = 0;" +
Environment.NewLine + " scrollY.value = 0;" +
Environment.NewLine + " }" +
Environment.NewLine + "}", true);

ClientScript.RegisterStartupScript(this.GetType(), "CallResetScrollPosition", "ResetScrollPosition();", true);
}
}


2. Call ResetScrollPosition(); in any method where you want to reset the scroll position.

Friday, July 24, 2009

Simple Error Reporting

This should be a no-brainer, but every ASP.NET application you put out there should have some solid error reporting behind it. The simplest way you can do this is by sending an email message each time there is an error. Nothing like debugging an error before the customer even contacts you about it. Code below.

You'll want to put the following in your Global.asax file:

VB.NET:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

If Request.ServerVariables("server_name") <> "localhost" AndAlso InStr(Request.Url.ToString, "get_aspx_ver.aspx") = 0 AndAlso InStr(Request.Url.ToString.ToLower(), "webresource.axd") = 0 AndAlso InStr(Request.Url.ToString.ToLower(), "scriptresource.axd") = 0 Then

' only run this if it's NOT the localhost and it's a real error - don't care about webresource.axd or scriptresource.axd errors since those don't usually show themselves to the end user

Dim objErr As Exception = Server.GetLastError.GetBaseException

Dim err As String

err = "<p><b>Error caught in Application_Error event:</b></p>"
err = err & "<p>Error URL: " & Request.Url.ToString & "</p>"
err = err & "<p>Error Message: " & objErr.Message.ToString & "</p>"
err = err & "<p>Source: " & objErr.Source.ToString & "</p>"
err = err & "<p>Stack Trace: " & objErr.StackTrace.ToString & "</p>"
err = err & "<p>Client IP Address: " & Request.ServerVariables("REMOTE_ADDR") & "</p>"

Server.ClearError()


Dim objMail As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("your.smtp.server")
Dim mailFrom As System.Net.Mail.MailAddress = New System.Net.Mail.MailAddress("from@somewhere.com", "Error")
Dim mailTo As System.Net.Mail.MailAddress = New System.Net.Mail.MailAddress("to@somewhere.com")
Dim mailMsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage(mailFrom, mailTo)
mailMsg.Subject = "Error"

mailMsg.Body = err
mailMsg.IsBodyHtml = True
objMail.Send(mailMsg)

' redirect to a friendly error page
Response.Redirect("~/Error.htm")

End If

End Sub


C#:

void Application_Error(object sender, EventArgs e) 
{
// Code that runs when an unhandled error occurs

string url = Request.Url.ToString();

if (Request.ServerVariables["server_name"] != "localhost" && url.IndexOf("get_aspx_ver.aspx", 0) == -1 && url.IndexOf("WebResource.axd", 0) == -1 && url.IndexOf("ScriptResource.axd", 0) == -1)
{
Exception objErr = Server.GetLastError().GetBaseException();

string err = "<p><b>Error caught in Application_Error event:</b></p>";
err += "<p>Error URL: " + Request.Url.ToString() + "</p>";
err += "<p>Error Message: " + objErr.Message.ToString() + "</p>";
err += "<p>Source: " + objErr.Source.ToString() + "</p>";
err += "<p>Stack Trace: " + objErr.StackTrace.ToString() + "</p>";
err += "<p>Client IP Address: " + Request.ServerVariables["remote_addr"].ToString() + "</p>";

Server.ClearError();

' add your own email code here

Response.Redirect("~/Error.htm");
}
}

Thursday, July 23, 2009

Corrupted ViewState after Postback

A common problem with the Web Forms model of ASP.NET is the overuse and bloat of ViewState. Don't get me wrong, I love me some ViewState, but turning it off when you don't need it can really be beneficial. But I digress. What happens when you have a huge page, maybe with a big ol Wizard control, and your ViewState gets out of control? One of the many errors you see may be something along the lines of:

Unable to validate data.

or

Invalid length for a Base-64 char array.

or

Padding is invalid and cannot be removed.

Not fun for your users at all. Lets say you've taken the steps to turn off ViewState for any controls that don't need them. What else can you do? In my opinion, the easiest thing to do is to compress ViewState between postbacks on the page. But how is this done?

1. Create a new class called "Compressor" in your App_Code directory.

VB.NET:

Imports Microsoft.VisualBasic
Imports System.IO
Imports System.IO.Compression

Public Class Compressor

Public Shared Function Compress(ByVal data As Byte()) As Byte()
Dim output As New MemoryStream()
Dim gzip As New GZipStream(output, CompressionMode.Compress, True)
gzip.Write(data, 0, data.Length)
gzip.Close()
Return output.ToArray()
End Function

Public Shared Function Decompress(ByVal data As Byte()) As Byte()
Dim input As New MemoryStream()
input.Write(data, 0, data.Length)
input.Position = 0
Dim gzip As New GZipStream(input, CompressionMode.Decompress, True)
Dim output As New MemoryStream()
Dim buff As Byte() = New Byte(64) {}
Dim read As Integer = -1
read = gzip.Read(buff, 0, buff.Length)
While read > 0
output.Write(buff, 0, read)
read = gzip.Read(buff, 0, buff.Length)
End While
gzip.Close()
Return output.ToArray()
End Function

End Class


C#:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.IO.Compression;

/// <summary>
/// Summary description for Compressor
/// </summary>
public static class Compressor
{
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}

public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
}


2. Throw the following methods in the code behind of the offending page(s).

VB.NET:

    Protected Overloads Overrides Function LoadPageStateFromPersistenceMedium() As Object
Dim viewState As String = Request.Form("__VSTATE")
Dim bytes As Byte() = Convert.FromBase64String(viewState)
bytes = Compressor.Decompress(bytes)
Dim formatter As New LosFormatter()
Return formatter.Deserialize(Convert.ToBase64String(bytes))
End Function

Protected Overloads Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
Dim formatter As New LosFormatter()
Dim writer As New StringWriter()
formatter.Serialize(writer, viewState)
Dim viewStateString As String = writer.ToString()
Dim bytes As Byte() = Convert.FromBase64String(viewStateString)
bytes = Compressor.Compress(bytes)
ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes))
End Sub


C#:

    protected override object LoadPageStateFromPersistenceMedium()
{
string viewState = Request.Form["__VSTATE"];
byte[] bytes = Convert.FromBase64String(viewState);
bytes = Compressor.Decompress(bytes);
LosFormatter formatter = new LosFormatter();
return formatter.Deserialize(Convert.ToBase64String(bytes));
}

protected override void SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, viewState);
string viewStateString = writer.ToString();
byte[] bytes = Convert.FromBase64String(viewStateString);
bytes = Compressor.Compress(bytes);
ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
}


3. Done

The net result of this is to compress your ViewState into a hidden field and decompress it when your page is initialized. Very little performance impact too. Pretty slick.

Friday, July 17, 2009

Setting Focus After AJAX Postback

This one really pissed me off. It should be easy to set the focus of a control after a postback in an AJAX Panel, right? In fact, if I searched Bing (you heard me) for "set focus after ajax postback" you'd think I would find this code. Not so much. It only took a week and a day to find this code:

ScriptManager.GetCurrent(this.Page).SetFocus(control);


...where "control" is the name of the control. Granted, this assumes an ASP.NET 3.5 application with a ScriptManager on the page. Done.