Monday, November 16, 2009

AddLocation !!

protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString.HasKeys())
{
if (Request.QueryString["action"].ToString() == "edit")
{
if (!Page.IsPostBack)
{
lblLocation.Text = "Edit Location";
btnSubmit.Text = "Save";
addLocType();
addCompName();
addCountry();
FillLocationDetail();

}
}
else if (Request.QueryString["action"].ToString() == "copy")
{
if (!Page.IsPostBack)
{
lblLocation.Text = "Copy Location";
btnSubmit.Text = "Save";
addLocType();
addCompName();
addCountry();
FillLocationDetail();
//addCountry();
}


}
}
else
{
if (!Page.IsPostBack)
{
addLocType();
addCompName();
addCountry();
}
}
}

######################################

// Add Company Location Type

private void addLocType()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conIDLDPL"]))
{
try
{
DDCountry.Items.Clear();
conn.Open();
string str = "select loc_type_name,loc_type_id from tblLocationType where loc_type_active='Yes' and loc_type_delete='No'";
ListItem Locat;
SqlCommand insertCom = new SqlCommand(str, conn);
SqlDataReader userReader = insertCom.ExecuteReader();
if (userReader.HasRows)
{
while (userReader.Read())
{
ListItem insert;
insert = new ListItem(userReader["loc_type_name"].ToString(), userReader["loc_type_id"].ToString());
DDLocation.Items.Add(insert);

}
}

Locat = new ListItem("Select--------------------->");
DDLocation.Items.Insert(0, Locat);
userReader.Close();

}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
conn.Close();
}

}

}

##########################################

// Add Company Name

private void addCompName()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conIDLDPL"]))
{
try
{
DDCountry.Items.Clear();
conn.Open();
string str = "select comp_name,comp_id from tblCompanyMaster where comp_active='Yes' and comp_delete='No'";
ListItem Locat;
SqlCommand insertCom = new SqlCommand(str, conn);
SqlDataReader userReader = insertCom.ExecuteReader();
if (userReader.HasRows)
{
while (userReader.Read())
{
ListItem insert;
insert = new ListItem(userReader["comp_name"].ToString(), userReader["comp_id"].ToString());
DDCompName.Items.Add(insert);

}
}

Locat = new ListItem("Select--------------------->");
DDCompName.Items.Insert(0, Locat);
userReader.Close();

}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
conn.Close();
}

}

}

#######################################

// Add Country
private void addCountry()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conIDLDPL"]))
{
try
{
DDCountry.Items.Clear();
conn.Open();
string str = "select country_name,country_id from tblCountryMaster where country_active='Yes'and country_delete='No'";
ListItem Locat;
SqlCommand insertCom = new SqlCommand(str, conn);
SqlDataReader userReader = insertCom.ExecuteReader();
if (userReader.HasRows)
{
while (userReader.Read())
{
ListItem insert;
insert = new ListItem(userReader["country_name"].ToString(), userReader["country_id"].ToString());
DDCountry.Items.Add(insert);

}
}

Locat = new ListItem("Select--------------------->");
DDCountry.Items.Insert(0, Locat);
userReader.Close();

}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
conn.Close();
}

}

}

###################################################

// Add Company Location
private void LocationAdd()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conIDLDPL"]))
{
try
{
conn.Open();
SqlCommand comLocation = new SqlCommand("insert_tblLocationMaster", conn);
comLocation.CommandType = CommandType.StoredProcedure;
comLocation.Parameters.Add(new SqlParameter("@comp_id", DDCompName.SelectedValue.ToString()));
comLocation.Parameters.Add(new SqlParameter("@loc_type_id", DDLocation.SelectedValue.ToString()));
comLocation.Parameters.Add(new SqlParameter("@loc_name", txtLocName.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_contect_person", txtContPerson.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_addr1", txtaddr1.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_addr2", txtaddr2.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_city", txtcity.Text));


if (DDState.SelectedItem.Text == "Others")
{
comLocation.Parameters.Add(new SqlParameter("@loc_state", txtother.Text));
}
else
{
comLocation.Parameters.Add(new SqlParameter("@loc_state", DDState.SelectedItem.Text));
}

// comLocation.Parameters.Add(new SqlParameter("@loc_state", DDState.SelectedItem.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_country", DDCountry.SelectedValue.ToString()));
comLocation.Parameters.Add(new SqlParameter("@loc_pin", txtpin.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_phone", txtphone.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_mobile", txtmobile.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_fax", txtfax.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_email", txtemail.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_active", "Yes"));
comLocation.Parameters.Add(new SqlParameter("@loc_delete", "No"));
comLocation.Parameters.Add(new SqlParameter("@loc_add_by", int.Parse(Session["EmpID"].ToString())));
//comLocation.Parameters.Add(new SqlParameter("@loc_add_date", "No"));

comLocation.ExecuteNonQuery();


}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{

}


}


}
private void FillLocationDetail()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conIDLDPL"]))
{
try
{
conn.Open();

string id = Request.QueryString["id"].ToString();
// string str = "select * from tblLocationMaster ";
string str = "select * from viewLocationDetail where loc_id=@Locid";


SqlCommand com = new SqlCommand(str, conn);
SqlParameter LOCID = new SqlParameter("@Locid", SqlDbType.Int, 4);
LOCID.Value = Request.QueryString["id"];
com.Parameters.Add(LOCID);
SqlDataReader dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
DDCompName.SelectedIndex = DDCompName.Items.IndexOf(DDCompName.Items.FindByValue(dr["comp_id"].ToString()));
DDLocation.SelectedIndex = DDLocation.Items.IndexOf(DDLocation.Items.FindByValue(dr["loc_type_id"].ToString()));
txtLocName.Text = dr["loc_name"].ToString();
ViewState["locName"] = dr["loc_name"].ToString();

txtContPerson.Text = dr["loc_contect_person"].ToString();
txtaddr1.Text = dr["loc_addr1"].ToString();
txtaddr2.Text = dr["loc_addr2"].ToString();
txtcity.Text = dr["loc_city"].ToString();
// DDState.SelectedItem.Text = dr["loc_state"].ToString();
DDCountry.SelectedIndex = DDCountry.Items.IndexOf(DDCountry.Items.FindByValue(dr["loc_country"].ToString()));
if (DDCountry.SelectedIndex > 0)
{
DDState.SelectedIndex = DDState.Items.IndexOf(DDState.Items.FindByText(dr["loc_state"].ToString()));
if (DDState.SelectedIndex <= 0)
{

txtother.Visible = true;
txtother.Text = dr["loc_state"].ToString();
DDState.SelectedIndex = DDState.Items.Count - 1;

}
}
else
{
txtother.Visible = false;

}
txtpin.Text = dr["loc_pin"].ToString();
txtphone.Text = dr["loc_phone"].ToString();
txtmobile.Text = dr["loc_mobile"].ToString();
txtfax.Text = dr["loc_fax"].ToString();
txtemail.Text = dr["loc_email"].ToString();

}

}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
conn.Close();
}

}

}
/*This code is used for update the LocationDetail*/
private void UpdateLocationDetail()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conIDLDPL"]))
{
try
{
conn.Open();
SqlCommand comLocation = new SqlCommand("update_tblLocationMaster1", conn);
comLocation.CommandType = CommandType.StoredProcedure;
string id = Request.QueryString["id"].ToString();
comLocation.Parameters.Add(new SqlParameter("@loc_id", id));
comLocation.Parameters.Add(new SqlParameter("@comp_id", DDCompName.SelectedValue.ToString()));
comLocation.Parameters.Add(new SqlParameter("@loc_type_id", DDLocation.SelectedValue.ToString()));
comLocation.Parameters.Add(new SqlParameter("@loc_name", txtLocName.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_contect_person", txtContPerson.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_addr1", txtaddr1.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_addr2", txtaddr2.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_city", txtcity.Text));


if (DDState.SelectedItem.Text == "Others")
{
comLocation.Parameters.Add(new SqlParameter("@loc_state", txtother.Text));
}
else
{
comLocation.Parameters.Add(new SqlParameter("@loc_state", DDState.SelectedItem.Text));
}

// comLocation.Parameters.Add(new SqlParameter("@loc_state", DDState.SelectedItem.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_country", DDCountry.SelectedValue.ToString()));
comLocation.Parameters.Add(new SqlParameter("@loc_pin", txtpin.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_phone", txtphone.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_mobile", txtmobile.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_fax", txtfax.Text));
comLocation.Parameters.Add(new SqlParameter("@loc_email", txtemail.Text));

comLocation.Parameters.Add(new SqlParameter("@loc_edit_by", int.Parse(Session["EmpID"].ToString())));

comLocation.ExecuteNonQuery();

}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{

}


}


}

protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Request.QueryString.HasKeys())
{
if (Request.QueryString["action"].ToString() == "edit")
{
UpdateLocationDetail();
Response.Redirect("viewlocation.aspx");
}
else if (Request.QueryString["action"].ToString() == "copy")
{
checkLocationStauts();

}
}
else
{
checkLocationStauts();
//LocationAdd();
//Response.Redirect("viewlocation.aspx");

}
}
public void checkLocationStauts()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conIDLDPL"]))
{

try
{
lblmsg.Text = "";
conn.Open();
// string str = "select * from tblLocationMaster where loc_name='" + ViewState["locName"] + "'";
string str = "select * from tblLocationMaster where loc_name='" + txtLocName.Text + "'";
SqlCommand chkCom = new SqlCommand(str,conn);
SqlDataReader dr = chkCom.ExecuteReader();
if (dr.HasRows)
{

dr.Read();
lblmsg.Text="Location name already exists!";
}
else
{
LocationAdd();
Response.Redirect("viewlocation.aspx",false);
//checkLocationStauts();
}
}
catch (Exception ex)
{
Response.Redirect(ex.Message.ToString());
}
finally
{
conn.Close();
}
}


}
protected void btCancle_Click(object sender, EventArgs e)
{
if (Request.QueryString.HasKeys())
{
Response.Redirect("viewlocation.aspx");
}
else
{
Response.Redirect("adminhome.aspx");
}
}
protected void DDState_SelectedIndexChanged(object sender, EventArgs e)
{
if (DDState.SelectedItem.Text == "Others")
{

txtother.Visible = true;
}
else
{
txtother.Visible = false;
}

}
protected void DDCountry_SelectedIndexChanged(object sender, EventArgs e)
{
if (DDCountry.SelectedItem.Text != "India")
{
DDState.SelectedValue = "Others";
txtother.Visible = true;
// lblStar.Visible = true;
this.txtother.Focus();
}
else
{
txtother.Visible = false;
DDState.SelectedValue = DDState.SelectedValue.ToString();
// DDState.SelectedIndex = 0;
// lblStar.Visible = false;

}
}

No comments: