using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Newdelhibarassocia.dhadmin { public partial class Newshopdata : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // BindShopType(); if (Request.QueryString["id"] != null) { int id = Convert.ToInt32(Request.QueryString["id"]); GetChamberById(id); } } } void GetChamberById(int id) { string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { string query = "SELECT * FROM Shops WHERE ShopID=@Id"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@Id", id); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { txtShopName.Text = dr["ShopName"].ToString(); txtShopNo.Text = dr["ShopNo"].ToString(); txtAddress.Text = dr["ShopAddress"].ToString(); txtMobile.Text = dr["MobileNo"].ToString(); txtRent.Text = dr["Rent"].ToString(); txtBalance.Text = dr["Balance"].ToString(); txtDate.Text = Convert.ToDateTime(dr["AsOnDate"]).ToString("yyyy-MM-dd"); ddlShopType.SelectedValue = dr["ShopType"].ToString(); ddlStatus.SelectedValue = dr["Status"].ToString(); // Owner TextBox1.Text = dr["OwnerName"].ToString(); TextBox2.Text = dr["OwnerAddress"].ToString(); TextBox3.Text = dr["OwnerMobile"].ToString(); TextBox4.Text = dr["OwnerAadhar"].ToString(); // Attendant TextBox5.Text = dr["AttendantName"].ToString(); TextBox6.Text = dr["AttendantAddress"].ToString(); TextBox7.Text = dr["AttendantMobile"].ToString(); TextBox8.Text = dr["AttendantAadhar"].ToString(); // Staff TextBox9.Text = dr["StaffDetails"].ToString(); } } } void BindShopType() { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(connStr)) { string query = "SELECT Name FROM AccountType ORDER BY Name"; using (SqlCommand cmd = new SqlCommand(query, con)) { con.Open(); SqlDataReader dr = cmd.ExecuteReader(); ddlShopType.DataSource = dr; ddlShopType.DataTextField = "Name"; ddlShopType.DataValueField = "Name"; ddlShopType.DataBind(); con.Close(); } } // Default item ddlShopType.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select Shop Type--", "")); } bool IsChamberNoExists(string chamberNo) { string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { string query = "SELECT COUNT(1) FROM Shops WHERE ShopNo = @ChamberNo"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@ChamberNo", chamberNo); con.Open(); int count = Convert.ToInt32(cmd.ExecuteScalar()); return count > 0; } } protected void btnSave_Click(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(connStr)) { con.Open(); decimal rent = 0, balance = 0; DateTime asOnDate = DateTime.Now; decimal.TryParse(txtRent.Text.Trim(), out rent); decimal.TryParse(txtBalance.Text.Trim(), out balance); DateTime.TryParse(txtDate.Text.Trim(), out asOnDate); // UPDATE if (Request.QueryString["id"] != null) { string id = Request.QueryString["id"].ToString(); string checkQuery = "SELECT COUNT(*) FROM Shops WHERE ShopNo=@ShopNo AND ShopID<>@ShopID"; using (SqlCommand checkCmd = new SqlCommand(checkQuery, con)) { checkCmd.Parameters.AddWithValue("@ShopNo", txtShopNo.Text.Trim()); checkCmd.Parameters.AddWithValue("@ShopID", id); int count = Convert.ToInt32(checkCmd.ExecuteScalar()); if (count > 0) { ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Shop No already exists!');", true); return; } } string updateQuery = @" UPDATE Shops SET ShopName=@ShopName, ShopNo=@ShopNo, ShopAddress=@ShopAddress, ShopType=@ShopType, MobileNo=@MobileNo, Rent=@Rent, Balance=@Balance, AsOnDate=@AsOnDate, Status=@Status, OwnerName=@OwnerName, OwnerAddress=@OwnerAddress, OwnerMobile=@OwnerMobile, OwnerAadhar=@OwnerAadhar, AttendantName=@AttendantName, AttendantAddress=@AttendantAddress, AttendantMobile=@AttendantMobile, AttendantAadhar=@AttendantAadhar, StaffDetails=@StaffDetails WHERE ShopID=@ShopID"; using (SqlCommand cmd = new SqlCommand(updateQuery, con)) { cmd.Parameters.AddWithValue("@ShopName", txtShopName.Text.Trim()); cmd.Parameters.AddWithValue("@ShopNo", txtShopNo.Text.Trim()); cmd.Parameters.AddWithValue("@ShopAddress", txtAddress.Text.Trim()); cmd.Parameters.AddWithValue("@ShopType", ddlShopType.SelectedValue); cmd.Parameters.AddWithValue("@MobileNo", txtMobile.Text.Trim()); cmd.Parameters.AddWithValue("@Rent", rent); cmd.Parameters.AddWithValue("@Balance", balance); cmd.Parameters.AddWithValue("@AsOnDate", asOnDate); cmd.Parameters.AddWithValue("@Status", ddlStatus.SelectedValue); cmd.Parameters.AddWithValue("@OwnerName", TextBox1.Text.Trim()); cmd.Parameters.AddWithValue("@OwnerAddress", TextBox2.Text.Trim()); cmd.Parameters.AddWithValue("@OwnerMobile", TextBox3.Text.Trim()); cmd.Parameters.AddWithValue("@OwnerAadhar", TextBox4.Text.Trim()); cmd.Parameters.AddWithValue("@AttendantName", TextBox5.Text.Trim()); cmd.Parameters.AddWithValue("@AttendantAddress", TextBox6.Text.Trim()); cmd.Parameters.AddWithValue("@AttendantMobile", TextBox7.Text.Trim()); cmd.Parameters.AddWithValue("@AttendantAadhar", TextBox8.Text.Trim()); cmd.Parameters.AddWithValue("@StaffDetails", TextBox9.Text.Trim()); cmd.Parameters.AddWithValue("@ShopID", id); cmd.ExecuteNonQuery(); } ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Shop Updated Successfully!');window.open('ShopReceipt.aspx?id=" + id + "','_blank');window.location='shoplist.aspx';", true); } else { if (IsChamberNoExists(txtShopNo.Text.Trim())) { ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Shop No already exists!');", true); return; } string insertQuery = @" INSERT INTO Shops ( ShopName,ShopNo,ShopAddress,ShopType,MobileNo, Rent,Balance,AsOnDate,Status, OwnerName,OwnerAddress,OwnerMobile,OwnerAadhar, AttendantName,AttendantAddress,AttendantMobile,AttendantAadhar, StaffDetails ) VALUES ( @ShopName,@ShopNo,@ShopAddress,@ShopType,@MobileNo, @Rent,@Balance,@AsOnDate,@Status, @OwnerName,@OwnerAddress,@OwnerMobile,@OwnerAadhar, @AttendantName,@AttendantAddress,@AttendantMobile,@AttendantAadhar, @StaffDetails ); SELECT SCOPE_IDENTITY();"; int newId = 0; using (SqlCommand cmd = new SqlCommand(insertQuery, con)) { cmd.Parameters.AddWithValue("@ShopName", txtShopName.Text.Trim()); cmd.Parameters.AddWithValue("@ShopNo", txtShopNo.Text.Trim()); cmd.Parameters.AddWithValue("@ShopAddress", txtAddress.Text.Trim()); cmd.Parameters.AddWithValue("@ShopType", ddlShopType.SelectedValue); cmd.Parameters.AddWithValue("@MobileNo", txtMobile.Text.Trim()); cmd.Parameters.AddWithValue("@Rent", rent); cmd.Parameters.AddWithValue("@Balance", balance); cmd.Parameters.AddWithValue("@AsOnDate", asOnDate); cmd.Parameters.AddWithValue("@Status", ddlStatus.SelectedValue); cmd.Parameters.AddWithValue("@OwnerName", TextBox1.Text.Trim()); cmd.Parameters.AddWithValue("@OwnerAddress", TextBox2.Text.Trim()); cmd.Parameters.AddWithValue("@OwnerMobile", TextBox3.Text.Trim()); cmd.Parameters.AddWithValue("@OwnerAadhar", TextBox4.Text.Trim()); cmd.Parameters.AddWithValue("@AttendantName", TextBox5.Text.Trim()); cmd.Parameters.AddWithValue("@AttendantAddress", TextBox6.Text.Trim()); cmd.Parameters.AddWithValue("@AttendantMobile", TextBox7.Text.Trim()); cmd.Parameters.AddWithValue("@AttendantAadhar", TextBox8.Text.Trim()); cmd.Parameters.AddWithValue("@StaffDetails", TextBox9.Text.Trim()); newId = Convert.ToInt32(cmd.ExecuteScalar()); } ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Shop Saved Successfully!');window.open('ShopReceipt.aspx?id=" + newId + "','_blank');window.location='shoplist.aspx';", true); } } ClearForm(); } void ClearForm() { txtShopName.Text = ""; txtShopNo.Text = ""; txtAddress.Text = ""; txtMobile.Text = ""; txtRent.Text = ""; txtBalance.Text = ""; txtDate.Text = ""; ddlShopType.SelectedIndex = 0; ddlStatus.SelectedIndex = 0; } } }