Posts RSS Comments RSS 133 Posts and 258 Comments till now

Archive for the 'Programming' Category

The Woes of Data-Type Conversion

With the current trend of SOI (Service Oriented Architecture), it is very common for Web Application to use Web Service in their back-end. Since the back-end processing is implemented as a Web Service, the Front-End (UI Layer) must convert the data into XML-text before sending it for processing.

 

This conversion from a data-type to xml text, then back to its original data-type, will introduce a pitfall that will plunge many developers into a runtime error. Learning from my past mistakes, I come up with these points that hopefully will save you from experiencing a runtime error (read, an unpleasant call from your customer explaining the problem):

  1. Always check your data before conversion. VB.Net has these functions to help you: IsDBNull(), IsNothing(), IsNumeric(). Use them wisely.
  2. Always initialize a variable before assigning a value.
  3. Try-Catch is there not for nothing.

To illustrate above points, I’ll make a few examples here:

Bad Code:

  Dim i as Integer
  i = Cint(myDataSet.Tables("MyTable").Row(0).("MyValue"))
  ProcessMyVariable(i)

Better Code:

  Dim i as Integer = 0
  If IsNumeric(myDataSet.Tables("MyTable").Row(0).("MyValue")) Then
      i = Cint(myDataSet.Tables("MyTable").Row(0).("MyValue"))
  End If
  ProcessMyVariable(i)

Best Code:

  Dim i as Integer = 0
  Try
      If myDataset.Tables.Contains("MyTable") Then
          If myDataSet.Tables("MyTable").Rows.Count > 0 Then
              If IsNumeric(myDataSet.Tables("MyTable").Row(0).("MyValue")) Then
                  i = Cint(myDataSet.Tables("MyTable").Row(0).("MyValue"))
              End If
          End If
      End If
  Catch e as Exception
  End Try
  ProcessMyVariable(i)

 

Care to add more?

EnableEventValidation Error

Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation=”true”/> in configuration or <%@ Page EnableEventValidation=”true” %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Event validation is a security measure by ASP.NET to prevent Injection-Attack (i.e. other page post their result to your ASPX page). Turning this option off through the web.config or inside the <%@ Page .. %> tag will guarantee you will never receive above error, but your security might be compromised.

But if your page still producing above error, say when you click a button, or select the value of a server dropdownlist control, these conditions might be applicable to your ASPX page:

  1. You have another <form /> tag inside your main Form tag
  2. Your AJAX/Javascript code changed the value of a server control
  3. You type tags that resemble HTML tags inside a textbox/text-area

What I have today was number 1. I found a <form /> tag inside a < asp:Content .. /> tag. The error came out when I convert my ASP.NET Website project into .NET Web Application Project. So its quite surprising that page never throw any Event validation error during its tenure as Website project. Another possibility is the <form /> was generated by Visual Studio when i convert the project. I’ll do some trial and error more tomorrow…

C# Regex Word Count

I notice from my traffic log that there was someone bumping to this blog searching for ‘C# Regex Word Count’. Since I never create such tutorial, so here you go.

The basic rule of a word would be it doesn’t contain any whitespaces (spaces, tabs, or newlines). So the easiest Regex would be:

 

[^\ ^\t^\n]+

 

Now to implement it using C#, It would be as follows:

      public int WordCount(String input)
      {
         int result = 0;
         try
         {
            Regex rgx = new Regex(@"[^\ ^\t^\n]+");
            MatchCollection mcMatches = rgx.Matches(input);
            result = mcMatches.Count;
         }
         catch (Exception ex)
         {
         }
         return result;
      }

An improvement would be putting the Regex creation out of the function, this way we will not re-created the Regex everytime the function is called.

Regex to match SQL-Style String

This post is copying the style that is used by Stephen Ostermiller in his article HERE.

 

Motivation

I am currently creating a RichTextBox control that will format and highlight the SQL string. I notice that the current regex that I used is not match correctly to a certain combination of SQL-style string.

 

Consider we have the following SQL statement:

Print ‘Testing Regex\’s String Matching’; Print ‘ ‘;

 

First Try

‘.*’

Print ‘Testing Regex\’s String Matching’; Print ‘ ‘;

This is incorrect. The regex should only match those within two single-quotes, not expand to the last single-quote.

 

(After n-Try)

‘.*?[^\\]’

Print ‘Testing Regex\’s String Matching’; Print ‘ ‘;

This should be the correct one.

 

For those who want to practice their Regular Expression skill, try to download THIS software, it’s free and it’s really good for Regex practice.

Credit goes to Buddie for pointing out this PAGE.

UPDATE (22 Sep 2007)

 

As it turns out, I totally forgot that SQL-style string doesn’t use \’ (backslash-quote) but uses ” (quote-quote). So things getting more complicated now.
So we need to change the string into something like this:

Print ‘Testing Regex’’s String Matching’; Print ‘ ‘;

Another Try

‘[a-zA-Z\ ]*(”)*[a-zA-Z\ ]*’

Print ‘Testing Regex’’s String Matching’; Print ‘ ‘;

But if we change the input string into

Print ‘Testing Regex’’s String’’s Matching’; Print ‘ ‘;

It becomes

Print ‘Testing Regex’’s String’’s Matching’; Print ‘ ‘;

Which is wrong.

 

Finally

‘([a-zA-Z\ ]*(”)*)*’

Print ‘Testing Regex’’s String” Matching’; Print ‘ ‘;

This should be the correct one.

Sorting Words by Their Length

Don’t ask why I come up with this post :) Let’s just say it has something to do with Regular Expression.

 

The first method that immediately come up to mind (and usually the worst :) ) is as follows:

struct Res
{
   //To Record the Result
   public String words;
   public int wordcount;
}

public static Res WorstWordByLengthSort(string raw)
{
   Res myresult = new Res();
   myresult.wordcount = 0;
   string[] words = raw.Split("n".ToCharArray());
   raw = "";
   foreach (string word in words)
      raw += word + " ";

   words = raw.Split(" ".ToCharArray());
   ArrayList ar = new ArrayList();

   foreach (string word in words)
   {
      string tstr = word.Trim();
      if (tstr == "")
         continue;
      myresult.wordcount++;
      if (ar.Count == 0)
      {
         ar.Add(tstr);
      }
      else
      {
          int count = ar.Count;
          bool inserted = false;
          for (int i = 0; i < count; i++)
          {
              if (ar[i].ToString().Length <= tstr.Length)
              {
                  if (!ar.Contains(tstr))
                  {
                      ar.Insert(i, tstr);
                      inserted = true;
                   }
              }
          }
          if (!inserted && !ar.Contains(tstr))
             ar.Add(tstr);
      }
   }
   StringBuilder sb = new StringBuilder();
   foreach (object o in ar)
       sb.AppendLine(o.ToString());
    myresult.words= sb.ToString();
    return myresult;
}

 

It will work flawlessly (sort of .. :) ), but it will not allow duplication of word. After tinkering for a while, I came up with an idea to improve its performance. A better solution would be to use a dictionary where the length of the word becomes the key. If a key is already exist in the dictionary, we just simply append the word into the value of that particular key. The idea is implemented as follows:

struct Res
{
   //To Record the Result
   public String words;
   public int wordcount;
}

public static Res BetterWordByLengthSort(string raw)
{
   Res myresult = new Res();
   myresult.wordcount = 0;
   myresult.words = "";
   StringBuilder result = new StringBuilder();
   Dictionary myDict = new Dictionary();
   ArrayList keys = new ArrayList();
   string[] words = raw.Split(”n”.ToCharArray());

   raw = “”;
   foreach (string word in words)
   {
      string tword = word.Trim();
      raw += tword + ” “;
   }
   words = null;
   words = raw.Split(” “.ToCharArray());

   foreach (string word in words)
   {
      string tempWord = word.Trim();
      if (tempWord == “”)
         continue;
      int tlength = tempWord.Length;
      if (myDict.ContainsKey(tlength))
      {
         myDict[tlength] = myDict[tlength] + “n” + tempWord;
      }
      else
      {
          keys.Add(tlength);
          myDict.Add(tlength, tempWord);
      }
      myresult.wordcount++;
   }
   //Sort the keys ASC
   keys.Sort();
   for (int i=keys.Count-1; i>=0; i–)
   {
      result.AppendLine(myDict[(int)keys[i]]);
   }
   myresult.words = result.ToString();
   return myresult;
}

I created a GUI project to compare their performance. With same input of 1443 words, the Worst method took 734 ms, while the Better method took only 15 ms. And yes, if you remember your Big O complexity, the Better method is definitely much more efficient compared to the Worst method :)

Pages (8): « 1 2 [3] 4 5 6 » ... Last »

Close
E-mail It
Socialized through Gregarious 42