//To show data in popup for editing
        public CountryEntity GetCountry(Int32 M_Country_Code)
        {
            String selectQuery = @"select *from [M_Country] where [M_Country_Code]=@M_Country_Code";

            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = _ConnStr;
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = selectQuery;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@M_Country_Code", M_Country_Code);

                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                DataTable     dataTable  = new DataTable();
                dataTable.Load(dataReader);
                CountryEntity existingcountry = new CountryEntity();
                foreach (DataRow row in dataTable.Rows)
                {
                    existingcountry.M_Country_Code     = Convert.ToInt32(row["M_Country_Code"]);
                    existingcountry.M_Country_Name     = Convert.ToString(row["M_Country_Name"]);
                    existingcountry.M_Country_Sname    = Convert.ToString(row["M_Country_Sname"]);
                    existingcountry.M_Country_Currency = Convert.ToInt32(row["M_Country_Currency"]);
                    existingcountry.M_Country_ISD      = Convert.ToInt32(row["M_Country_Isd"]);
                }
                conn.Close();

                return(existingcountry);
            }
        }
        // Update country data in database
        public bool UpdateCountry(CountryEntity country)
        {
            String updateQuery = @"UPDATE [M_Country] SET [M_Country_Name]=@M_Country_Name , [M_Country_Sname] = @M_Country_Sname, [M_Country_Currency] = @M_Country_Currency ,[M_Country_Isd] = @M_Country_Isd Where [M_Country_Code]=@M_Country_Code";

            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = _ConnStr;
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = updateQuery;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@M_Country_Code", country.M_Country_Code);
                cmd.Parameters.AddWithValue("@M_Country_Name", country.M_Country_Name);
                cmd.Parameters.AddWithValue("@M_Country_Sname", country.M_Country_Sname);
                cmd.Parameters.AddWithValue("@M_Country_Currency", currencydropdownlistEditpopup.SelectedItem.Value);
                cmd.Parameters.AddWithValue("@M_Country_Isd", country.M_Country_ISD);

                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                cmd.ExecuteNonQuery();
                conn.Close();

                return(true);
            }
        }
        //Adding country data to database
        public bool AddNewCountry(CountryEntity country)
        {
            String insertQuery = @"INSERT INTO [M_Country]([M_Country_Name],[M_Country_Sname],[M_Country_Currency],[M_Country_Isd]) VALUES(@M_Country_Name,@M_Country_Sname,@M_Country_Currency,@M_Country_Isd)";

            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = _ConnStr;
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = insertQuery;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@M_Country_Name", country.M_Country_Name);
                cmd.Parameters.AddWithValue("@M_Country_Sname", country.M_Country_Sname);
                cmd.Parameters.AddWithValue("@M_Country_Currency", currencydropdownlistAddpopup.SelectedItem.Value);

                cmd.Parameters.AddWithValue("@M_Country_Isd", @country.M_Country_ISD);
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                cmd.ExecuteNonQuery();
                conn.Close();

                return(true);
            }
        }
        //View button click event
        protected void ibtnView_Click(object sender, ImageClickEventArgs e)
        {
            GridViewRow   gvRow          = (GridViewRow)((ImageButton)sender).NamingContainer;
            Int32         M_Country_Code = Convert.ToInt32(gvCountry.DataKeys[gvRow.RowIndex].Value);
            CountryEntity country        = GetCountry(M_Country_Code);

            countryidAddpopup.Value           = country.M_Country_Code.ToString();
            txtcountrynameViewpopup.Text      = country.M_Country_Name;
            txtcountryshortnameViewpopup.Text = country.M_Country_Sname;
            if (country.M_Country_ISD != null)
            {
                txtcountryisdViewpopup.Text = country.M_Country_ISD.ToString();
            }
            mpeCountryView.Show();
        }
        protected void EditpopupSaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                CountryEntity country = new CountryEntity();
                country.M_Country_Code     = Convert.ToInt32(countryidEditpopup.Value);
                country.M_Country_Name     = txtcountrynameEditpopup.Text.Trim();
                country.M_Country_Sname    = txtcountryshortnameEditpopup.Text.Trim();
                country.M_Country_Currency = Convert.ToInt32(currencydropdownlistEditpopup.SelectedItem.Value);
                country.M_Country_ISD      = Convert.ToInt32(txtcountryisdEditpopup.Text.Trim());



                if (country.M_Country_Code != 0)
                {
                    UpdateCountry(country);
                }


                LoadData();
            }
        }
        protected void AddpopupSaveButton_Click(object sender, EventArgs e)
        {
            //  ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Sorry there are no attachments');", true);

            if (Page.IsValid)
            {
                CountryEntity country = new CountryEntity();
                country.M_Country_Code     = Convert.ToInt32(countryidAddpopup.Value);
                country.M_Country_Name     = txtcountrynameAddpopup.Text.Trim();
                country.M_Country_Sname    = txtcountryshortnameAddpopup.Text.Trim();
                country.M_Country_Currency = Convert.ToInt32(currencydropdownlistAddpopup.SelectedItem.Value);
                country.M_Country_ISD      = Convert.ToInt32(txtcountryisdAddpopup.Text.Trim());



                if (country.M_Country_Code == 0)
                {
                    AddNewCountry(country);
                }


                LoadData();
            }
        }