Tuesday, May 26, 2009
TextBox Enable and Disable
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode <> 57))
return false;
else
return true;
}
JavaScript Validator checking only numbers
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode <> 57))
return false;
else
return true;
}
TextBox enable Disable on checkChange
protected void rptrItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBox chk = (CheckBox)e.Item.FindControl("chkbxItem");
TextBox txt = (TextBox)e.Item.FindControl("txtQuantity");
txt.Attributes.Add("onkeypress", "return isNumberKey(event)");
txt.Attributes.Add("onpaste", "return false;");
chk.Attributes.Add("onclick", "if(this.checked) {document.getElementById('" + txt.ClientID + "').disabled = !this.checked;document.getElementById('" + txt.ClientID + "').style.backgroundColor ='white';document.getElementById('" + txt.ClientID + "').focus();}else{document.getElementById('" + txt.ClientID + "').style.backgroundColor ='CCCCCC';document.getElementById('" + txt.ClientID + "').disabled = true;}");
}
}
Monday, May 25, 2009
Build a C# .NET Zip folder backup utility
Zip Backups have become almost ubiquitous, but the various zip and rar utilities often don't have all the command line switches we need. One answer is, "roll your own" ! Thanks to Mike Krueger and his crew, we have the ICSharpCode.ZipLib library which is 100% native .NET Framework code. Peter Bromberg illustrates how to put it all together....
http://dotnetslackers.com/CSharp/re-9984_Build_a_Chash_NET_Zip_folder_backup_utility.aspx
Unzip file to a folder using C# code and asp.net
The ZIP file format is a data compression and archival format. A ZIP file contains one or more files that have been compressed, to reduce their file size, or stored as-is. A number of compression algorithms are permitted in zip files but as of 2008 only DEFLATE is widely used and supported.
ZIP files generally use the file extensions ".zip" or ".ZIP" and the MIME media type application/zip. Some software uses the ZIP file format as a wrapper for a large number of small items in a specific structure. Generally when this is done a different file extension is used. Examples of this usage are Java JAR files, Python .egg files, etc. But in windows C# programming we usually use only zip extension to compress and extract many files inside a folder.
http://www.worldofasp.net/tut/UnzipPath/Unzip_file_to_a_folder_using_Csharp_code_and_aspnet_241.aspx
Simple FTP demo application using C#.Net 2.0
An addition to the Microsoft .NET framework 2.0 to 1.x is the support for FTP. All these days we had to rely on 3rd party libraries which pretty well suited most of our needs, but for sure, there is an extra pleasure using the .net framework library classes. The code included is not designed to be a fulfledged reusable library, but rather an easy to use and reusable pieces of code which is easily comprehensible and can be reused and tweaked to fit your specific needs. Therefore the code for each functionality(upoad, download, delete etc..) can be easy picked up separately and reused. The main motive behind this article was the unavailability of .net2.0 ftp sample codes and their usage in C#; may be because its a new entrant to the .net scenario, or the third party implementations available were working pretty well, that this area of the .net2.0 library haven't got enough focus.
http://www.codeproject.com/KB/IP/SimpleFTPDemo.aspx
Zip / Unzip folders and files with C#
Here is a utility class I wrote that handles zipping or unzipping a folder with all its subfolders and contained files, using SharpZipLib:
http://www.eggheadcafe.com/tutorials/aspnet/9ce6c242-c14c-4969-9251-af95e4cf320f/zip--unzip-folders-and-f.aspx
How to schedule a task using windows Services
This Article basically explains how to schedule a process which can run as windows service
The process may include like Database entry as in my example.
http://www.dotnetspider.com/resources/869-How-schedule-task-using-windows-Services.aspx
Thursday, May 21, 2009
MySQL SubQuery
//mysql_subquery
//copyright 1999 avi bryant, all rights reserved
//feel free to use this and modify it as you see fit
function sub($result)
{
if($result == 0)
return "()";
else
{
$row = mysql_fetch_row($result);
$strResult = "( $row[0]";
while($row = mysql_fetch_row($result))
$strResult = "$strResult , $row[0]";
$strResult = "$strResult )";
return $strResult;
}
}
function mysql_subquery($query)
{
$substart = strpos($query, "(");
if($substart != false)
{
$subend = strrpos($query, ")");
$before = substr($query, 0, $substart);
$after = substr($query, $subend+1, -1);
$subquery = substr($query, $substart+1, $subend-$substart-1);
$subresult = mysql_subquery($subquery);
$subtext = sub($subresult);
return mysql_query($before . $subtext . $after);
}
else return mysql_query($query);
}
C# Code Generator for Stored Procedures
Here is a link for C# Code Generator for Stored Procedures
http://www.codeproject.com/KB/database/CSCodeBuilder.aspx
Class Generator
Here is a code for class generator
http://www.csharpfriends.com/demos/csharp_class_generator.aspx?ftd=3
Wednesday, May 20, 2009
Simple Email Utility Class
By Kara Hewett
Introduction
This useful C# .NET email utility configures easily identifying the receiver, sender, message content and mail server requiring minimal time for implementation.
Creating the Workspace
To start the Email Utility project, open a Microsoft Visual Studio 2005, .NET Framework Version 2.0 development environment and create a Console Application. Add two C# classes, one called EmailHandler.cs and another called AppHandler.cs and an Application Configuration File called the default, App.config.
Code
As with any Microsoft C# project, the application consists of the configuration properties and the application. By default the App.config file is XML with a primary element named .configuration.. To the default .configuration. element, set the properties required for email configuration:
. Recipient
. Sender
. Copy List
. Priority
. Body Encoding
. Subject
. Message Body
. SMTP Server
App.config
The Email Handler
The Email Handler provides a simple method for sending the email using the parameters defined in the application configuration file.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.IO;
namespace EmailApp
{
public sealed class EmailHandler
{
private static char[] charSeparators = new char[] { };
private static String[] result;
public static void SendMailMessage(string smtpHost, MailAddress from, string to, string subject, string message)
{
try
{
MailMessage mailMsg = new MailMessage();
result = to.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
for (int count = 0; count < result.Length; count++)
{ mailMsg.To.Add(new MailAddress(result[count])); }
mailMsg.From = from;
mailMsg.Subject = subject;
mailMsg.Body = message;
mailMsg.Priority = AppHandler.EmailPriority;
mailMsg.BodyEncoding = AppHandler.EmailEncoding;
SmtpClient smtpClient = new SmtpClient(smtpHost);
smtpClient.Send(mailMsg);
}
catch (Exception exc)
{
}
}}
}
The AppHandler class makes the application parameters accessible as static variables in the EmailHandler class.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace EmailApp
{
public static class AppHandler
{
private static MailAddress emailRecipient;
private static String emailSender;
private static String emailCopy;
private static String emailPriority;
private static String emailEncoding;
private static String emailSubject;
private static String emailMessage;
private static String emailSmtp;
public static MailAddress EmailRecipient
{
get { return emailRecipient; }
set { emailRecipient = value; }
}
public static String EmailSender
{
get { return emailSender; }
set { emailSender = value; }
}
public static String EmailCopy
{
get { return emailCopy; }
set { emailCopy = value; }
}
public static String EmailPriority
{
get { return emailPriority; }
set { emailPriority = value; }
}
public static String EmailEncoding
{
get { return emailEncoding; }
set { emailEncoding = value; }
}
public static String EmailSubject
{
get { return emailSubject; }
set { emailSubject = value; }
}
public static String EmailMessage
{
get { return emailMessage; }
set { emailMessage = value; }
}
}
}
The EmailHandler usage involves setting the configuration parameters and calling one line of code.
AppHandler.EmailRecipient = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["Email.Recipient"]);
AppHandler.EmailSender = System.Configuration.ConfigurationManager.AppSettings["Email.Sender"];
AppHandler.EmailCopy = System.Configuration.ConfigurationManager.AppSettings["Email.Copy"];
AppHandler.EmailPriority = System.Configuration.ConfigurationManager.AppSettings["Email.Priority"];
AppHandler.EmailEncoding = System.Configuration.ConfigurationManager.AppSettings["Email.Encoding"];
AppHandler.EmailSubject = System.Configuration.ConfigurationManager.AppSettings["Email.Subject"];
AppHandler.EmailMessage = System.Configuration.ConfigurationManager.AppSettings["Email.Message"];
AppHandler.EmailSmtp = System.Configuration.ConfigurationManager.AppSettings["Email.Smtp"];
EmailHandler.SendEmail(AppHandler.EmailSmtp, AppHandler.EmailSender, AppHandler.EmailRecipient, AppHandler.EmailSubject, AppHandler.EmailMessage);
Kara Hewett is a software developer at RBS Greenwich Capital Markets. She earned a MBA in Information Systems, Finance and Accounting from New York University and a BS in Economics from the Wharton School of Business. Her past experience includes numerous consulting and development projects for SAC Capital, Lehman Brothers, J P Morgan Chase, American Stock Exchange, and Deutsche Bank.