Date and other validations

public void DateValidation(object sender, EventArgs e)
{

if (sender.GetType().BaseType.BaseType.ToString() == new TextBox().GetType().ToString()){

TextBox _txtBox = (TextBox)sender;
try{
if (_txtBox.Text.Trim() != ""){

if (_txtBox.Text.Length >= 6 && _txtBox.Text.Length <= 8){

String str = "";
int month = Convert.ToInt32(_txtBox.Text.Substring(0, 2)); // for Checking Month Exctracting Month from given dateif (month > 12){
_txtBox.Focus();

throw new Exception("Month is not valid in Date,Please Check");}

int day = Convert.ToInt32(_txtBox.Text.Substring(2, 2)); // for check days Extracting Days form given datestr = _txtBox.Text.Substring(0, 2) + "/" + _txtBox.Text.Substring(2, 2) + "/";
if (_txtBox.Text.Length == 6) // If the user enters 6 digit then {
str = str +
DateTime.Now.ToString("MMddyyyy").Substring(4, 2) + _txtBox.Text.Substring(4, 2); // for Checking Current Century Year We are concatinating Current Century}
else if (_txtBox.Text.Length == 8) // If the user enters 8 digit then{
str = str + _txtBox.Text.Substring(4, 4);
}
else{
_txtBox.Focus();
throw new Exception("Year is not valid in Date, Please Check");}

DateTime dt;
try{
dt =
DateTime.Parse(str);}

catch (Exception){
_txtBox.Focus();

throw new Exception("Days Is Not Valid,Please Check");}

int monthdays = DateTime.DaysInMonth(dt.Year, month);
if (day > monthdays){
_txtBox.Focus();

throw new Exception("Days is not valid in Date,Please Check");}

//int datdiff = dt.CompareTo(DateTime.Now);//if (datdiff > 0)//{// _txtBox.Focus();// throw new Exception("Please Check it is Future Date");//}}
else{
_txtBox.Focus();
throw new Exception("Invalid Date It Should be 6 Or 8 Digit,Please Check");}
}
}

catch (Exception){

if (_txtBox.Name.ToString().Equals("INVOICE_DUEDATE")){

//_txtBox.Parent.Controls["TERMS"].Focus();if (_txtBox.Parent.Controls["TERMS"] != null)_txtBox.Parent.Controls[
"TERMS"].Enabled = true;
if (_txtBox.Parent.Controls["TERMS_TH"] != null)_txtBox.Parent.Controls[
"TERMS_TH"].Enabled = true;
if (_txtBox.Parent.Controls["TERMS_DESCRIPTION"] != null)_txtBox.Parent.Controls[
"TERMS_DESCRIPTION"].Enabled = true;}
}
}

else{
DataGridView _DGV = (DataGridView)sender;
try{

if (_DGV.EditingControl.Text.Trim() != ""){

if (_DGV.EditingControl.Text.Length >= 6 && _DGV.EditingControl.Text.Length <= 8){

String str = "";
int month = Convert.ToInt32(_DGV.EditingControl.Text.Substring(0, 2)); // for Checking Month Exctracting Month from given dateif (month > 12){
_DGV.EditingControl.Focus();

throw new Exception("Month is not valid in Date,Please Check");}

int day = Convert.ToInt32(_DGV.EditingControl.Text.Substring(2, 2)); // for check days Extracting Days form given datestr = _DGV.EditingControl.Text.Substring(0, 2) + "/" + _DGV.EditingControl.Text.Substring(2, 2) + "/";
if (_DGV.EditingControl.Text.Length == 6) // If the user enters 6 digit then {
str = str +
DateTime.Now.ToString("MMddyyyy").Substring(4, 2) + _DGV.EditingControl.Text.Substring(4, 2); // for Checking Current Century Year We are concatinating Current Century}
else if (_DGV.EditingControl.Text.Length == 8) // If the user enters 8 digit then{
str = str + _DGV.EditingControl.Text.Substring(4, 4);
}
else{
_DGV.EditingControl.Focus();
throw new Exception("Year is not valid in Date, Please Check");}

DateTime dt;
try{
dt =
DateTime.Parse(str);}

catch (Exception){
_DGV.EditingControl.Focus();

throw new Exception("Days Is Not Valid,Please Check");}

int monthdays = DateTime.DaysInMonth(dt.Year, month);
if (day > monthdays){
_DGV.EditingControl.Focus();

throw new Exception("Days is not valid in Date,Please Check");}

//int datdiff = dt.CompareTo(DateTime.Now);//if (datdiff > 0)//{// _txtBox.Focus();// throw new Exception("Please Check it is Future Date");//}}
else{
_DGV.EditingControl.Focus();
throw new Exception("Invalid Date It Should be 6 Or 8 Digit,Please Check");}
}
}

catch (Exception){

if (_DGV.EditingControl.Name.ToString().Equals("INVOICE_DUEDATE")){

//_txtBox.Parent.Controls["TERMS"].Focus();if (_DGV.Columns.Contains("TERMS"))_DGV[
"TERMS", _DGV.CurrentRow.Index].ReadOnly = false;
if (_DGV.Columns.Contains("TERMS_TH"))_DGV[
"TERMS_TH", _DGV.CurrentRow.Index].ReadOnly = false;
if (_DGV.Columns.Contains("TERMS_DESCRIPTION"))_DGV[
"TERMS_DESCRIPTION", _DGV.CurrentRow.Index].ReadOnly = false;}
}
}
}

//public void BILL_TO_TextChanged(object sender, EventArgs e)public void BILL_TO_DETAILS_TextChanged(object sender, EventArgs e){

if (sender.GetType().BaseType.BaseType.ToString() == new TextBox().GetType().ToString()){

TextBox textBox = (TextBox)sender;
try{
string strvariable = textBox.Text;
if (strvariable != ""){

string[] SplitValue = strvariable.Split('|');
if (SplitValue.Length >= 1){

string strTerm = SplitValue[0].ToString();
if (SplitValue.Length >= 2){

string strCode = SplitValue[1].ToString();textBox.Text = strTerm.Trim();

if (textBox.Parent.Controls["PULLED_DIVISION"] != null){
textBox.Parent.Controls[
"PULLED_DIVISION"].Text = strCode.Trim();
if (textBox.Parent.Controls["PULLED_DIVISION"].Text != ""){
((
TextBox)textBox.Parent.Controls["PULLED_DIVISION"]).ReadOnly = true;}
}
}
}

else{
if (textBox.Parent.Controls["PULLED_DIVISION"] != null){
textBox.Parent.Controls[
"PULLED_DIVISION"].Text = "";((
TextBox)textBox.Parent.Controls["PULLED_DIVISION"]).ReadOnly = false;}
}
}

else{
if (textBox.Parent.Controls["PULLED_DIVISION"] != null){
textBox.Parent.Controls[
"PULLED_DIVISION"].Text = "";((
TextBox)textBox.Parent.Controls["PULLED_DIVISION"]).ReadOnly = false;}
}

}

catch (Exception){

throw;}
}

else{
DataGridView DGV = (DataGridView)sender;
try{
string strvariable = DGV.EditingControl.Text;
if (strvariable != ""){

string[] SplitValue = strvariable.Split('|');
if (SplitValue.Length >= 1){

string strTerm = SplitValue[0].ToString();
if (SplitValue.Length >= 2){

string strCode = SplitValue[1].ToString();DGV.EditingControl.Text = strTerm.Trim();

if (DGV.Columns.Contains("PULLED_DIVISION")){
DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].Value = strCode.Trim().ToString();
if (DGV["PULLED_DIVISION", DGV.CurrentRow.Index].EditedFormattedValue.ToString() != ""){
DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].ReadOnly = true;}
}
}
}

else{
if (DGV.Columns.Contains("PULLED_DIVISION")){
DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].Value = "";DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].ReadOnly = false;}
}
}

else{
if (DGV.Columns.Contains("PULLED_DIVISION")){
DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].Value = "";DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].ReadOnly = false;}
}

}

catch (Exception){

throw;}

}
}


public void BILL_TO_Leave(object sender, EventArgs e){

if (sender.GetType().BaseType.BaseType.ToString() == new TextBox().GetType().ToString()){

TextBox textBox = (TextBox)sender;
try{
string strvariable = textBox.Parent.Controls["BILL_TO_DETAILS"].Text;
if (strvariable != ""){

string[] SplitValue = strvariable.Split('|');
if (SplitValue.Length >= 1){

string strTerm = SplitValue[0].ToString();
if (SplitValue.Length >= 2){

string strCode = SplitValue[1].ToString();textBox.Parent.Controls[
"BILL_TO_DETAILS"].Text = strTerm.Trim();
if (textBox.Parent.Controls["PULLED_DIVISION"] != null){
textBox.Parent.Controls[
"PULLED_DIVISION"].Text = strCode.Trim();
if (textBox.Parent.Controls["PULLED_DIVISION"].Text != ""){
((
TextBox)textBox.Parent.Controls["PULLED_DIVISION"]).ReadOnly = true;}
}
}
}

//else//{// if (textBox.Parent.Controls["PULLED_DIVISION"] != null)// {// textBox.Parent.Controls["PULLED_DIVISION"].Text = "";// ((TextBox)textBox.Parent.Controls["PULLED_DIVISION"]).ReadOnly = false;// }//}}
else{
if (textBox.Parent.Controls["PULLED_DIVISION"] != null){
textBox.Parent.Controls[
"PULLED_DIVISION"].Text = "";((
TextBox)textBox.Parent.Controls["PULLED_DIVISION"]).ReadOnly = false;}
}

}

catch (Exception){

throw;}
}

else{
DataGridView DGV = (DataGridView)sender;
try{
string strvariable = DGV["BILL_TO_DETAILS", DGV.CurrentRow.Index].EditedFormattedValue.ToString();
if (strvariable != ""){

string[] SplitValue = strvariable.Split('|');
if (SplitValue.Length >= 1){

string strTerm = SplitValue[0].ToString();
if (SplitValue.Length >= 2){

string strCode = SplitValue[1].ToString();DGV[
"BILL_TO_DETAILS", DGV.CurrentRow.Index].Value = strTerm.Trim();
if (DGV.Columns.Contains("PULLED_DIVISION")){
DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].Value = strCode.Trim();
if (DGV["PULLED_DIVISION", DGV.CurrentRow.Index].EditedFormattedValue.ToString() != ""){
DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].ReadOnly = true;}
}
}
}

//else//{// if (textBox.Parent.Controls["PULLED_DIVISION"] != null)// {// textBox.Parent.Controls["PULLED_DIVISION"].Text = "";// ((TextBox)textBox.Parent.Controls["PULLED_DIVISION"]).ReadOnly = false;// }//}}
else{
if (DGV.Columns.Contains("PULLED_DIVISION")){
DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].Value = "";DGV[
"PULLED_DIVISION", DGV.CurrentRow.Index].ReadOnly = false;}
}

}

catch (Exception){

throw;}
}
}


public void INVOICE_DUEDATE_Leave(object sender, EventArgs e){

if (sender.GetType().BaseType.BaseType.ToString() == new TextBox().GetType().ToString()){

try{
TextBox _txtBox = (TextBox)sender;


if (_txtBox.Text.Trim() == ""){

if (_txtBox.Parent.Controls["TERMS"] != null)_txtBox.Parent.Controls[
"TERMS"].Enabled = true;
if (_txtBox.Parent.Controls["TERMS_TH"] != null)_txtBox.Parent.Controls[
"TERMS_TH"].Enabled = true;
if (_txtBox.Parent.Controls["TERMS_DESCRIPTION"] != null)_txtBox.Parent.Controls[
"TERMS_DESCRIPTION"].Enabled = true;_txtBox.Focus();

throw new Exception("Invoice Duedate field is blank");}

else{
if (_txtBox.Text.Length >= 6 && _txtBox.Text.Length <= 8){

String str = "";
int month = Convert.ToInt32(_txtBox.Text.Substring(0, 2)); // for Checking Month Exctracting Month from given dateif (month > 12){
_txtBox.Focus();

throw new Exception("Month is not valid in Invoice Duedate,Please Check");}

int day = Convert.ToInt32(_txtBox.Text.Substring(2, 2)); // for check days Extracting Days form given datestr = _txtBox.Text.Substring(0, 2) + "/" + _txtBox.Text.Substring(2, 2) + "/";
if (_txtBox.Text.Length == 6) // If the user enters 6 digit then {
str = str +
DateTime.Now.ToString("MMddyyyy").Substring(4, 2) + _txtBox.Text.Substring(4, 2); // for Checking Current Century Year We are concatinating Current Century}
else if (_txtBox.Text.Length == 8) // If the user enters 8 digit then{
str = str + _txtBox.Text.Substring(4, 4);
}
else{
_txtBox.Focus();
throw new Exception("Year is not valid in Invoice Duedate, Please Check");}

DateTime dt;
try{
dt =
DateTime.Parse(str);}

catch (Exception){
_txtBox.Focus();

throw new Exception("Days Is Not Valid,Please Check");}

int monthdays = DateTime.DaysInMonth(dt.Year, month);
if (day > monthdays){
_txtBox.Focus();

throw new Exception("Days is not valid in Invoice Duedate,Please Check");}

int datdiff = dt.CompareTo(DateTime.Now);
//if (datdiff > 0)//{// _txtBox.Focus();// throw new Exception("Please Check it is Future Duedate");//}

if (_txtBox.Parent.Controls["TERMS"] != null)_txtBox.Parent.Controls[
"TERMS"].Enabled = false;
if (_txtBox.Parent.Controls["TERMS_TH"] != null)_txtBox.Parent.Controls[
"TERMS_TH"].Enabled = false;
if (_txtBox.Parent.Controls["TERMS_DESCRIPTION"] != null)_txtBox.Parent.Controls[
"TERMS_DESCRIPTION"].Enabled = false;}

elseif (_txtBox.Text.Trim() == "#"){
_txtBox.Focus();

throw new Exception("Recheck the Invoice DueDate ");
}

else{
if (_txtBox.Parent.Controls["TERMS"] != null)_txtBox.Parent.Controls[
"TERMS"].Enabled = true;
if (_txtBox.Parent.Controls["TERMS_TH"] != null)_txtBox.Parent.Controls[
"TERMS_TH"].Enabled = true;
if (_txtBox.Parent.Controls["TERMS_DESCRIPTION"] != null)_txtBox.Parent.Controls[
"TERMS_DESCRIPTION"].Enabled = true;_txtBox.Focus();

throw new Exception("Invalid Duedate It Should be 6 Or 8 Digit,Please Check");}
}
}

catch (Exception){

throw;}
}

else{
try{
DataGridView DGV = (DataGridView)sender;

if (DGV.EditingControl.Text.Trim() == ""){

if (DGV.Columns.Contains("TERMS"))DGV[
"TERMS", DGV.CurrentRow.Index].ReadOnly = false;
if (DGV.Columns.Contains("TERMS_TH"))DGV[
"TERMS_TH", DGV.CurrentRow.Index].ReadOnly = false;
if (DGV.Columns.Contains("TERMS_DESCRIPTION"))DGV[
"TERMS_DESCRIPTION", DGV.CurrentRow.Index].ReadOnly = false;DGV.EditingControl.Focus();

throw new Exception("Invoice Duedate field is blank");
}

else{
if (DGV.EditingControl.Text.Length >= 6 && DGV.EditingControl.Text.Length <= 8){

String str = "";
int month = Convert.ToInt32(DGV.EditingControl.Text.Substring(0, 2)); // for Checking Month Exctracting Month from given dateif (month > 12){
DGV.EditingControl.Focus();

throw new Exception("Month is not valid in Invoice Duedate,Please Check");}

int day = Convert.ToInt32(DGV.EditingControl.Text.Substring(2, 2)); // for check days Extracting Days form given datestr = DGV.EditingControl.Text.Substring(0, 2) + "/" + DGV.EditingControl.Text.Substring(2, 2) + "/";
if (DGV.EditingControl.Text.Length == 6) // If the user enters 6 digit then {
str = str +
DateTime.Now.ToString("MMddyyyy").Substring(4, 2) + DGV.EditingControl.Text.Substring(4, 2); // for Checking Current Century Year We are concatinating Current Century}
else if (DGV.EditingControl.Text.Length == 8) // If the user enters 8 digit then{
str = str + DGV.EditingControl.Text.Substring(4, 4);
}
else{
DGV.EditingControl.Focus();
throw new Exception("Year is not valid in Invoice Duedate, Please Check");}

DateTime dt;
try{
dt =
DateTime.Parse(str);}

catch (Exception){
DGV.EditingControl.Focus();

throw new Exception("Days Is Not Valid,Please Check");}

int monthdays = DateTime.DaysInMonth(dt.Year, month);
if (day > monthdays){
DGV.EditingControl.Focus();

throw new Exception("Days is not valid in Invoice Duedate,Please Check");}

int datdiff = dt.CompareTo(DateTime.Now);
//if (datdiff > 0)//{// _txtBox.Focus();// throw new Exception("Please Check it is Future Duedate");//}

if (DGV.Columns.Contains("TERMS"))DGV[
"TERMS", DGV.CurrentRow.Index].ReadOnly = true;
if (DGV.Columns.Contains("TERMS_TH"))DGV[
"TERMS_TH", DGV.CurrentRow.Index].ReadOnly = true;
if (DGV.Columns.Contains("TERMS_DESCRIPTION"))DGV[
"TERMS_DESCRIPTION", DGV.CurrentRow.Index].ReadOnly = true;}

else{
if (DGV.Columns.Contains("TERMS"))DGV[
"TERMS", DGV.CurrentRow.Index].ReadOnly = false;
if (DGV.Columns.Contains("TERMS_TH"))DGV[
"TERMS_TH", DGV.CurrentRow.Index].ReadOnly = false;
if (DGV.Columns.Contains("TERMS_DESCRIPTION"))DGV[
"TERMS_DESCRIPTION", DGV.CurrentRow.Index].ReadOnly = false;DGV.EditingControl.Focus();

throw new Exception("Invalid Duedate It Should be 6 Or 8 Digit,Please Check");}
}
}

catch (Exception){

throw;}
}
}

/// <summary>/// 1 /// </summary>/// <param name="sender"></param>/// <param name="e"></param>

Comments