21 March 2014

Update auto_increment in SQL Server

Update auto_increment in SQL Server

Create PROC SP_UpdateEmployee @Id int = null out, @employeename varchar(50), @Active bit AS INSERT INTO Employeemaster(employeename,Active)values(@employeename,@Active) Set @Id = Scope_Identity()

Get Count of Unread mail from Outlook Express in C#.Net

Get Count of Unread mail from Outlook Express in C#.Net

 using Microsoft.Office.Interop.Outlook;  
public static int CountOfUnreadMails()
{
Application OutlookApp = new Application();
NameSpace myMail = OutlookApp.GetNamespace("MAPI");
int unread = myMail.GetDefaultFolder(OlDefaultFolders.olFolderInbox).UnReadItemCount;
return unread;
}

20 March 2014

Upload Single file to FTP Server using C#.net

Upload Single file to FTP Server using C#.net

  public static bool UploadSingleFiles(string fileName,int candidateId,int resumeId)  
{
try
{
string parentDir = string.Empty;
string candir = string.Empty;
string desPath = string.Empty;
FileInfo objfileInfo = new FileInfo(fileName);
parentDir = ClsConfigurations.ReadSetting("Folder");
candir = @"/" + parentDir + "/" + Convert.ToString(candidateId);
desPath = candir +"-" + Convert.ToString(resumeId) + objfileInfo.Extension;
CreateDirectory(candir);
objFTPCon.ServerDirectory = candir.Trim();
objFTPCon.UploadFile(fileName, desPath);
return true;
}
catch (Exception ex)
{
Program.WriteLog(ex.Message, ex.StackTrace);
return false;
}
}

19 March 2014

UnCheck All Checked List Box in C#.net

UnCheck All Checked List Box in C#.net
public static void UnCheckedAllChecks(CheckedListBox chkLstBox) { foreach (int i in chkLstBox.CheckedIndices) { chkLstBox.SetItemCheckState(i, CheckState.Unchecked); } }

18 March 2014

TextBox Error Provider for Tab Page in C#.net

TextBox Error Provider for Tab Page in C#.net
public static bool TextBoxErrorControl(TextBox txt, string message,TabPage tp)
{
if (!string.IsNullOrEmpty(txt.Text))
{
ErrProvider.SetError(txt, "");
}
else
{
ErrProvider.SetError(txt, message);
}
object[] control = ErrProvider.GetControls(tp).ToArray();
if (control.Length > 0)
return false;
else
return true;
}

17 March 2014

Set DefaultIntValue in C#.net

Set DefaultIntValue in C#.net
public static int DefaultIntValue(TextBox txt)
{
try
{
if (String.IsNullOrEmpty(txt.Text))
{
return 0;
}
else
{
return Convert.ToInt32(txt.Text);
}
}
catch (Exception ex)
{
Program.WriteLog(ex.Message, ex.StackTrace);
return 0;
}
}

16 March 2014

DefaultDoubleValue on TextBox in C#.net

DefaultDoubleValue on TextBox in C#.net
public static Double DefaultDoubleValue(TextBox txt)
{
try
{
if (String.IsNullOrEmpty(txt.Text))
{
return 0.00;
}
else
{
return Convert.ToDouble(txt.Text);
}
}
catch (Exception ex)
{
Program.WriteLog(ex.Message, ex.StackTrace);
return 0.00;
}
}

15 March 2014

ComboBox With Error Provider in C#.net

ComboBox With Error Provider in C#.net


Create CustomError Provider in your project
 using System.Collections.Generic;  
using System.Windows.Forms;
namespace naraayananProject.UserControl
{
public class CustomErrorProvider :ErrorProvider
{
public List<Control> GetControls()
{
return this.GetControls(this.ContainerControl);
}
public List<Control> GetControls(Control ParentControl)
{
List<Control> ret = new List<Control>();
if (!string.IsNullOrEmpty(this.GetError(ParentControl)))
ret.Add(ParentControl);
foreach (Control c in ParentControl.Controls)
{
List<Control> child = GetControls(c);
if (child.Count > 0)
ret.AddRange(child);
}
return ret;
}
}
}

Declare Custom Error Provider in your Current Class:
 public static CustomErrorProvider ErrProvider = new CustomErrorProvider();  

  public static bool ComboBoxErrorControl(ComboBox cmb, string message, TabPage tp)  
{
if (!String.IsNullOrEmpty(cmb.Text))
{
ErrProvider.SetError(cmb, "");
}
else
{
ErrProvider.SetError(cmb, message);
}
object[] control = ErrProvider.GetControls(tp).ToArray();
if (control.Length > 0)
return false;
else
return true;
}

14 March 2014

Clear ComboBox Text using C#.net

Clear ComboBox Text using C#.net

public static void ClearComBoBoxes(Control parent)
{
foreach (Control child in parent.Controls)
{
ComboBox comBox = child as ComboBox;
if (comBox == null)
ClearComBoBoxes(child);
else
comBox.SelectedIndex = -1;
}
}

13 March 2014

Upload Multiple files into FTP Server using C#.net

Upload Multiple files into FTP Server using C#.Net

public static bool MultipleUploadFiles(string[] fileName, int candidateId, int resumeId)
{
try
{
string parentDir = string.Empty;
string candir = string.Empty;
string desPath = string.Empty;
foreach (string fName in fileName)
{
FileInfo objfileInfo = new FileInfo(fName);
parentDir = ClsConfigurations.ReadSetting("Folder");
candir = @"/" + parentDir + "/" + Convert.ToString(candidateId);
desPath = candir + "-" + Convert.ToString(resumeId) + objfileInfo.Extension;
CreateDirectory(candir);
objFTPCon.ServerDirectory = candir.Trim();
objFTPCon.UploadFile(fName, desPath);
}
return true;
}
catch (Exception ex)
{
Program.WriteLog(ex.Message, ex.StackTrace);
return false;
}
}

12 March 2014

Display file from FTP Server

Display file from FTP Server
public static void DownloadFromFTP(string sourceFile, string destinationFile,RichTextBox rtxtJResume)
{
if (IsFTPConnected())
{
string partnet_Dir = string.Empty;
partnet_Dir = ClsConfigurations.ReadSetting("Folder");
string[] id = sourceFile.Split('-');
objFTPCon.ServerDirectory = @"/" + partnet_Dir + "/" + id[0];
objFTPCon.DownloadFile(destinationFile, sourceFile);
FileInfo fi = new FileInfo(destinationFile);
string file = System.Windows.Forms.Application.StartupPath + "\\temp" + fi.Extension.ToLower();
string text = ClsFile.ExtractTextFromFile(file);
File.Delete(System.Windows.Forms.Application.StartupPath + "\\temp" + fi.Extension.ToLower());
rtxtJResume.Text = text;
}
}

11 March 2014

Connect FTP Server using C#.net

Check FTP Connection using C#.Net
 public static bool IsFTPConnected()  
{
objFTPCon = new FTPConnection();
objFTPCon.ServerAddress = "Server address"
objFTPCon.UserName = "User Name"
objFTPCon.Password = "Password"
objFTPCon.ServerDirectory = "directory name"
if (objFTPCon.ServerAddress != "" && objFTPCon.UserName != "" && objFTPCon.Password != "")
{
objFTPCon.Login();
objFTPCon.Connect();
return true;
}
else
{
MessageBox.Show("Please Fill FTP Server Connection", "Staffing Solution");
return false;
}
}

09 March 2014

Create Directory in FTP using C#.Net

Today , we will discuss about FTP using C#.Net.

FTP Component :EnterpriseDT.Net.Ftp
 public static FTPConnection objFTPCon = new FTPConnection();  
private static void CreateDirectory(string dirName)
{
if (!objFTPCon.DirectoryExists(dirName))
objFTPCon.CreateDirectory(dirName);
}

08 March 2014

Use of a multicast delegate

A multicast delegate may be used to call more than one method.

Remove unused using in VS 2008

Open Class File
Edit>>Intellisense >> Organise Usings >> Remove unused usings

Data Table Function for LINQToSQL

public static DataTable AsDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}

06 March 2014

Sort using in VS 2008


Open Class File
Edit>>Intellisense >> Organise Usings >> Sort Usings

Business Validation via Keypress Event in C#.Net

Today we will discuss about Display Form Icon in the Menu Strip of MDI Form
public static void BOValidation(KeyPressEventValidation mode, KeyPressEventArgs e, object sender)
{
switch (mode)
{
case KeyPressEventValidation.Alphabets:
if (!char.IsLetter(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 32)
e.Handled = true;
break;
case KeyPressEventValidation.AlphabetValidation:
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && e.KeyChar != 32)
e.Handled = true;
break;
case KeyPressEventValidation.AlphaNumeric:
if (!char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 32 && e.KeyChar != 45)
e.Handled = true;
break;
case KeyPressEventValidation.CompanyName:
if (!char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 32 && e.KeyChar != 38 && e.KeyChar != 45 && e.KeyChar != 40 && e.KeyChar != 46 && e.KeyChar != 40)
e.Handled = true;
break;
case KeyPressEventValidation.DecimalValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') e.Handled = true;
// To allow only one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1 )
e.Handled = true;
break; case KeyPressEventValidation.FilePathvalidation:
if ((e.KeyChar >= 48 && e.KeyChar <= 57) && (e.KeyChar >= 65 && e.KeyChar <= 122) && e.KeyChar == 92)
e.Handled = true;
break;
case KeyPressEventValidation.IntegerValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
e.Handled = true;
break;
case KeyPressEventValidation.PhoneValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != 32 && e.KeyChar != 40 && e.KeyChar != 41 && e.KeyChar != 43 && e.KeyChar != 45)
e.Handled = true;
break;
case KeyPressEventValidation.ValidateSpecialCharacters:
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))
e.Handled = true;
break;
case KeyPressEventValidation.PercentageValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
if (!char.IsControl(e.KeyChar))
{
TextBox textBox = (TextBox)sender;
if (textBox.Text.IndexOf('.') > -1 && textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3)
{
e.Handled = true;
}
}
break;
}
}

04 March 2014

Clear Rich Text Box Data in C#.net

public static void ClearRichTextBox(Control parent)
{
foreach (Control child in parent.Controls)
{
RichTextBox rtxtBox = child as RichTextBox;
if (rtxtBox == null)
ClearRichTextBox(child);
else
rtxtBox.Text = string.Empty;
}
}

Business Validation Via Regular Expression in C#.Net

public static bool BOValidation(Validate mode, TextBox input)
{
switch (mode)
{
case Validate.EmailValidation:
_pattern = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.TimeValidation:
_pattern = @"^(20|21|22|23|[01]\d|\d)(([:][0-5]\d){1,2})$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.AlphabetValidate:
_pattern = @"^[a-zA-Z]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.AlphabetSpaceDotValidate:
_pattern = @"^[a-zA-Z\s\.]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.AlphaNumericCommaSpaceHyphenUnderscoreValidate:
_pattern = @"^[a-zA-Z0-9\s,-_&]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.DecimalValidate:
_pattern = @"^[-+]?\d+(\.\d+)?$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.NumberValidate:
_pattern = @"^[0-9]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.PhoneNumberValidate:
_pattern = @"^[-+\s]?[0-9\s,()-]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.PostalCodeValidate:
_pattern = @"^[-+]?[0-9]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.UserNameValidate:
_pattern = @"^[a-zA-Z0-9_]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.FTPFilePathValidation:
_pattern = @"^([a-zA-Z0-9])*/[a-zA-Z0-9]+)$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.URLValidate:
_pattern = @"^((https?|ftp|news):\/\/)?www\.([a-z]([a-z0-9\-]*)+\.
(aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|lan|mil|mobi|museum|nato|name|net|org|pro|store|tel|travel|web|[a-z]{2}|[a-z]{2}\.[a-z]{2}))$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.FilePathValidate:
_pattern = @"([a-zA-Z]:(\\w+)*\\[a-zA-Z0_9]+)?.doc|.docx |.pdf";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.PercentageValidation:
_pattern = @"^(100(?:\.0{1,2})?|0*?\.\d{1,2}|\d{1,2}(?:\.\d{1,2})?)$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
}
return _match.Success;
}

02 March 2014

Display Form Icon in the Menu Strip of MDI Form

Today we will discuss about Display Form Icon in the Menu Strip of MDI Form

childform.MdiParent = this; childform.WindowState = FormWindowState.Maximized; childform.BringToFront();
var bmp = new Bitmap(16, 16);
using (var g = Graphics.FromImage(bmp))
{
g.DrawImage(childform.Icon.ToBitmap(), new Rectangle(0, 0, 16, 16));
}
var newIcon = Icon.FromHandle(bmp.GetHicon());
childform.Icon = newIcon; childform.Show();

ASP.NET Page Directives

ASP.NET Page Directives
Page directives : Page directives configure the runtime environment that will execute the page.
The complete list of directives is as follows:
@ Assembly - Links an assembly to the current page or user control declaratively.
@ Control - Defines control-specific attributes used by the ASP.NET page parser and compiler and can be included only in .ascx files (user controls).
@ Implements - Indicates that a page or user control implements a specified .NET Framework interface declaratively.
@ Import - Imports a namespace into a page or user control explicitly.
@ Master - Identifies a page as a master page and defines attributes used by the ASP.NET page parser and compiler and can be included only in .master files.
@ MasterType - Defines the class or virtual path used to type the Master property of a page.
@ OutputCache - Controls the output caching policies of a page or user control declaratively.
@ Page - Defines page-specific attributes used by the ASP.NET page parser and compiler and can be included only in .aspx files.
@ PreviousPageType - Creates a strongly typed reference to the source page from the target of a cross-page posting.
@ Reference - Links a page, user control, or COM control to the current page or user control declaratively.
@ Register - Associates aliases with namespaces and classes, which allow user controls and custom server controls to be rendered when included in a requested page or user control.