Archive for Ottobre, 2007

transakcje SQL .NET

Ottobre 31st, 2007

Description: Przyklad wykozystania transakcji

Link: http://www.codekeep.net/snippets/8ce81713-722a-4899-834d-6c0321a46450.aspx

string DeleteContact(int intContactID)
 {
   // Open the database connection.
   ContactMgmt.Open();
   // Declare a transaction object.
   SqlTransaction transDelete;
   // Create the transaction.
   transDelete = ContactMgmt.BeginTransaction(IsolationLevel.ReadCommitted);     // Create the command to delete from Contacts table.     SqlCommand cmdDelete = new SqlCommand("DELETE FROM Contacts" + "
    WHERE ContactID=" + intContactID.ToString(), ContactMgmt, transDelete);     // Execute the commands
     try
       {
           int intRows;
           // Delete row from Contacts table.
           intRows = cmdDelete.ExecuteNonQuery();
           // Delete Calls for this ContactID.
           cmdDelete.CommandText = "DELETE FROM Calls WHERE " +
           " ContactID=" + intContactID.ToString();             intRows = intRows + cmdDelete.ExecuteNonQuery();
           // Commit the transaction.
            transDelete.Commit();
           // Return success message.
            return intRows.ToString() + " deleted.";
        }
        catch
        {
            // Restore the database state if there was an error.
            transDelete.Rollback();
            // Return error message.
            return "Contact could not be deleted.";
         }
         finally
         {
            // Close the database.
            ContactMgmt.Close();
          }
}

Copy and Paste JavaScript with an Ajax Engine

Ottobre 31st, 2007

It’s not necessary to understand the characteristics of Ajax in order to use it. This article provides copy and paste JavaScript with an Ajax engine. No modifications are necessary. By William Bontrager. 1031

test

Ottobre 31st, 2007

Description: tests

Link: http://www.codekeep.net/snippets/f3783b4f-f9ce-45aa-9267-02b964a6744b.aspx

        [SetUp]
        public void setup() {
            UnitOfMeasureUtilities unitOfMeasureUtilities = new UnitOfMeasureUtilities();
            unitConversionTable =  unitOfMeasureUtilities.getConversionTable();
        }

Colecciones de solo lectura

Ottobre 29th, 2007

Description: Como hacer que una coleccion sea de solo lectura

Link: http://www.codekeep.net/snippets/b2db39e3-bafd-4f1a-8974-137aed651cc7.aspx

public class FiltroSql
{
    private readonly List<CondicionSql> _condiciones=new List<CondicionSql>();

        /// <summary>
        /// Gets the condiones.
        /// </summary>
        /// <value>The condiones.</value>
        public ReadOnlyCollection<CondicionSql> Condiones
        {
            get { return _condiciones.AsReadOnly(); }
        }
}

Object-Oriented JavaScript: Part 3

Ottobre 29th, 2007

This week wraps up our section on Object-Oriented JavaScript with a look at prototypes, the JavaScript execution context, var x, this.x, and x, inheritance using closures and prototypes and more. By Cristian Darie, Bogdan Brinzarea. 1029

isValidDateTime

Ottobre 28th, 2007

Description: Is a string a valid Datetime.

Link: http://www.codekeep.net/snippets/f10c5b90-e66b-4a25-8908-7910e2969031.aspx

  public static bool isValidDateTime(string str)
  {
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("^([0]?[1-9]|[1][0-2])[/-]([0]?[1-9]|[1-2][0-9]|3[0-1])[/-](200[0-9])(((\x20)([0]?[1-9]|[1][0-2]):[0-5][0-9]:[0-5][0-9](\x20)?(am|AM|pm|PM))?)$");

    if (!reg.Match(str).Success)
    {
      return false;
    }

    try
    {
      DateTime dt = Convert.ToDateTime(str);
      return true;
    }
    catch
    {
      return false;
    }
  }

isIn

Ottobre 28th, 2007

Description: Search a delimited string for a substring.

Link: http://www.codekeep.net/snippets/fa2e79ad-88a6-4602-ae1a-f42d07d8a63b.aspx

  public static bool isIn(string searchString, string delimitedString)
  {
    return isIn(searchString, delimitedString, ",", false);
  }

  public static bool isIn(string searchString, string delimitedString, string delimiter)
  {
    return isIn(searchString, delimitedString, delimiter, false);
  }

  public static bool isIn(string searchString, string delimitedString, bool caseSensitive)
  {
    return isIn(searchString, delimitedString, ",", caseSensitive);
  }

  public static bool isIn(string searchString, string delimitedString, string delimeter, bool caseSensitive)
  {
    try
    {
      if (delimeter == "" || delimeter == null)
        delimeter = ",";
      if (searchString == "" || searchString == null || delimitedString == "" || delimitedString == null)
        return false;
      else
      {
        bool ret = false;
        string[] arr = delimitedString.Split(delimeter.ToCharArray());
        foreach (string s in arr)
        {
          if (caseSensitive)
          {
            if (searchString == s) { ret = true; }
          }
          else
          {
            if (searchString.ToLower() == s.ToLower()) { ret = true; }
          }
        }
        return ret;
      }

    }
    catch { return false; }
  }

Regex WWW

Ottobre 26th, 2007

Description: Wyrazenie reguralne WWW

Link: http://www.codekeep.net/snippets/9719bab4-6f65-44e2-80c8-2e20083d9322.aspx

Regex reg = new Regex(@"(?<http>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)");

Regex Email

Ottobre 26th, 2007

Description: wyrazenie regularne sprawdzajace email

Link: http://www.codekeep.net/snippets/f44790e1-63df-40d1-a909-8ef55969e7a1.aspx

Regex reg = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");

Workaround for inability to dynamically set WebBrowser.DocumentText

Ottobre 26th, 2007

Description: Allows you to dynamically set the HTML for a .NET 2.0 WebBrowser control, bypassing the DocumentText “bug”.

Link: http://www.codekeep.net/snippets/1bb8c968-3f30-46ee-96e6-006161a0c04b.aspx

/// <summary>
/// A workaround to update a WebBrowser control's text
/// </summary>
/// <param name="html">The full HTML text to write</param>
private void WriteDynamicHTMLToWebBrowser(string html)
{
    webbrowserControl1.AllowNavigation = false;
    if (webbrowserControl1.Document != null)
        webbrowserControl1.Document.OpenNew(true);
    else
        webbrowserControl1.Navigate("about:blank");

    webbrowserControl1.Document.Write(html);
}

Next »