using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using HRHO_Entities;
using HRHO_BL;
namespace HRHO
{
public partial class AddCountry : System.Web.UI.Page
{
DataBaseAccess objDataBaseAccess = new DataBaseAccess();
dboperations db = new dboperations();
SqlConnection con = new SqlConnection(CommonFile.getConnectionString());
int iCompanyId;
string strName, strStatus;
string strCode;
int iStatus;
CountryBL c_bl = new CountryBL();
CountryEntities c_ent = new CountryEntities();
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetNoStore();
try
{
if (Session["staffID"].ToString().Trim() == "")
{
Response.Redirect("Home.aspx");
}
}
catch (Exception)
{
Session["staffID"] = "";
Response.Redirect("Home.aspx");
}
if (!IsPostBack)
{
gvdisplay();
iStatus=0;
Session["strStatus"] = "A";
if (iStatus == 0)
{
btnAddCountry.Text = "Add";
}
}
}
private void gvdisplay()
{
/*
string strQuery = "select * from TB_COUNTRY where Status=0 order by Name";
string strTable = "TB_COUNTRY";
DataSet dt = new DataSet();
dt = objDataBaseAccess.GetRecords(strTable, strQuery);
gvCountry.DataSource = dt.Tables[0];
gvCountry.DataBind();
*/
c_ent.Name = "";
c_ent.CountryCode = "";
c_ent.ID = 0;
c_ent.flag = 4;
List<CountryEntities> countryList = c_bl.GetCountry(c_ent);
gvCountry.DataSource = countryList;
gvCountry.DataBind();
}
private void clear()
{
txtAddCountry.Text = "";
txtAddCountryCode.Text = "";
lblAddCountry.Text = "";
txtSearchby.Text = "";
ddlSearch.Text = "--select--";
btnAddCountry.Text = "Add";
gvdisplay();
Session["strStatus"] = "A";
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Session["strStatus"] = "A";
btnAddCountry.Text = "Add Country";
clear();
}
protected void btnAddCountry_Click(object sender, EventArgs e)
{
string test = Session["strStatus"].ToString();
con.Open();
/*SqlCommand cmd = new SqlCommand("SELECT * FROM TB_COUNTRY WHERE (Name = '" + txtAddCountry.Text + "') OR (CountryCode = '" + txtAddCountryCode.Text + "') and (status=0)", con);
cmd.Parameters.AddWithValue("@Name", txtAddCountry.Text);
cmd.Parameters.AddWithValue("@CountryCode", txtAddCountryCode.Text);*/
c_ent.Name = txtAddCountry.Text;
c_ent.CountryCode = txtAddCountryCode.Text;
if (test=="A")
{
/*
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
clear();
lblAddCountry.Visible = true;
lblAddCountry.Style.Add(HtmlTextWriterStyle.Color, "red");
lblAddCountry.Text = "Country name or Country Code already exist";
}
else
{
string sql = "insert into TB_COUNTRY (Name,CountryCode,Status) values ('" + txtAddCountry.Text + "','" + txtAddCountryCode.Text + "',0)";
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
clear();
lblAddCountry.Visible = true;
lblAddCountry.Style.Add(HtmlTextWriterStyle.Color, "green");
lblAddCountry.Text = "Added succesfully !";
con.Close();
gvdisplay();
}
*/
c_ent.ID = 0;
c_ent.flag = 1;
int a=c_bl.InsertCountry(c_ent);
if (a > 0)
{
clear();
lblAddCountry.Visible = true;
lblAddCountry.Style.Add(HtmlTextWriterStyle.Color, "green");
lblAddCountry.Text = "Added succesfully !";
}
else
{
lblAddCountry.Visible = true;
lblAddCountry.Style.Add(HtmlTextWriterStyle.Color, "red");
lblAddCountry.Text = "Country name or country code already existed !";
}
gvdisplay();
}
if (test == "U")
{
iCompanyId = Convert.ToInt32(Session["iCompanyId"]);
//string s3 = "update TB_COUNTRY set CountryCode='" + txtAddCountryCode.Text + "',Name='" + txtAddCountry.Text + "' where ID=" + iCompanyId + "";
//db.exnon(s3);
c_ent.ID = iCompanyId;
c_ent.Name = txtAddCountry.Text;
c_ent.CountryCode = txtAddCountryCode.Text;
c_ent.flag = 2;
int cnt=c_bl.UpdateCountry(c_ent);
if (cnt > 0)
{
clear();
lblAddCountry.Visible = true;
lblAddCountry.Style.Add(HtmlTextWriterStyle.Color, "green");
lblAddCountry.Text = "Updated Successfully";
btnAddCountry.Text = "Add";
txtAddCountry.Text = "";
txtAddCountryCode.Text = "";
gvdisplay();
Session["strStatus"] = "A";
}
else
{
lblAddCountry.Visible = true;
lblAddCountry.Text = "Country Not Updated: Name or code already existing";
}
}
}
protected void gvCountry_RowEditing(object sender, GridViewEditEventArgs e)
{
lblAddCountry.Text = "";
GridView gvCountry= (GridView)sender;
iCompanyId = Int32.Parse(gvCountry.DataKeys[Int32.Parse(e.NewEditIndex.ToString())].Values["ID"].ToString());
strName=gvCountry.DataKeys[Int32.Parse(e.NewEditIndex.ToString())].Values["Name"].ToString();
strCode=gvCountry.DataKeys[Int32.Parse(e.NewEditIndex.ToString())].Values["CountryCode"].ToString();
txtAddCountry.Text = strName;
txtAddCountryCode.Text = strCode;
iStatus = 1;
Session["strStatus"] ="U";
Session["iCompanyId"] = iCompanyId;
btnAddCountry.Text = "Update";
gvdisplay();
}
protected void Button2_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
Label lblCountryName = (Label)btn.FindControl("Label1");
string strCountryName = lblCountryName.Text;
Label lblCountryCode = (Label)btn.FindControl("Label2");
string strCountrCode = lblCountryCode.Text;
Label lblID = (Label)btn.FindControl("lblID");
}
protected void Button3_Click(object sender, EventArgs e)
{
}
protected void gvCountry_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView gvCountry = (GridView)sender;
int iCountryId = (int)gvCountry.DataKeys[e.RowIndex].Values["ID"];
//int i=Int32.Parse(gvCountry.DataKeys[Int32.Parse(e.NewEditIndex.ToString())].Values["ID"].ToString());
//string iCountryId=gvCountry.Rows[e.RowIndex].Cells[1].ToString();
//iCompanyId = Convert.ToInt32(Session["iCompanyId"]);
/*
string s3 = "update TB_COUNTRY set Status=1 where CountryCode='" + strCountryId + "'";
db.exnon(s3);
clear();*/
c_ent.ID = Convert.ToInt32(iCountryId);
c_ent.flag = 3;
c_ent.Name = "";
c_ent.CountryCode="";
int cnt=c_bl.DeleteCountry(c_ent);
if (cnt > 0)
{
lblAddCountry.Visible = true;
lblAddCountry.Text = "Deleted successfully";
}
else
{
lblAddCountry.Visible = true;
lblAddCountry.Text = " Data cannot be Deleted";
}
gvdisplay();
}
protected void gvCountry_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCountry.PageIndex = e.NewPageIndex;
clear();
gvdisplay();
}
//protected void btnSearch_Click(object sender, EventArgs e)
//{
// if (txtSearchby.Text == "")
// {
// lblSearchCountry.Visible = true;
// lblSearchCountry.Style.Add(HtmlTextWriterStyle.Color, "red");
// lblSearchCountry.Text = "Please enter data to search";
// return;
// }
// lblSearchCountry.Visible = false;
// string ddlvar =(ddlSearch.SelectedValue.ToString());
// string strQuery = "SELECT Name,CountryCode FROM TB_COUNTRY WHERE "+ddlvar+" LIKE '%" + txtSearchby.Text + "%'";
// string strTable = "TB_COUNTRY";
// DataSet dt = new DataSet();
// dt = objDataBaseAccess.GetRecords(strTable, strQuery);
// gvsearch.DataSource = dt.Tables[0];
// gvsearch.DataBind();
// if (gvsearch.Rows.Count == 0)
// {
// clear();
// lblSearchCountry.Visible = true;
// lblSearchCountry.Style.Add(HtmlTextWriterStyle.Color, "red");
// lblSearchCountry.Text = "No data found";
// }
// else
// {
// lblAddCountry.Visible = false;
// gvsearch.DataBind();
// }
//}
protected void gvCountry_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
}
protected void btnSearch_Click(object sender, EventArgs e)
{
if (txtSearchby.Text == "")
{
lblSearchCountry.Visible = true;
lblSearchCountry.Style.Add(HtmlTextWriterStyle.Color, "red");
lblSearchCountry.Text = "Please enter data to search";
return;
}
if (ddlSearch.Text == "--select--")
{
lblSearchCountry.Visible = true;
lblSearchCountry.Style.Add(HtmlTextWriterStyle.Color, "red");
lblSearchCountry.Text = "Please select country or code first";
gvCountry.Visible = false;
return;
}
gvCountry.Visible = true;
lblSearchCountry.Visible = false;
string ddlvar = (ddlSearch.SelectedValue.ToString());
string strQuery = "SELECT ID,Name,CountryCode FROM TB_COUNTRY WHERE " + ddlvar + " LIKE '" + txtSearchby.Text + "%' AND Status='1' ";
string strTable = "TB_COUNTRY";
DataSet dt = new DataSet();
dt = objDataBaseAccess.GetRecords(strTable, strQuery);
gvCountry.DataSource = dt;
gvCountry.DataBind();
if (gvCountry.Rows.Count == 0)
{
//clear();
lblSearchCountry.Visible = true;
lblSearchCountry.Style.Add(HtmlTextWriterStyle.Color, "red");
lblSearchCountry.Text = "No data found";
}
else
{
lblSearchCountry.Visible = false;
gvCountry.DataBind();
}
}
}
}