TechEthical CEHP

Current Path : G:/PleskVhosts/megamind.net.in/careercentre360.com/admin/
Upload File :
Current File : G:/PleskVhosts/megamind.net.in/careercentre360.com/admin/subcategory.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Directory.admin
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        DataTable ds;
        DataSet dd;

        public System.Data.DataTable dt = new System.Data.DataTable();
        public System.Data.SqlClient.SqlDataAdapter SqlAdapter = new SqlDataAdapter();

        protected void Page_Load(object sender, EventArgs e)
        {

            connection_sql obj = new connection_sql();
            obj.MakeConnection();
             DataTable ds = obj.getDataTable(@"SELECT * FROM [dbo].[tblCategory] order by Name");
             int a = 0;
            while (a < ds.Rows.Count)
            {
                cmbCategory.Items.Add(ds.Rows[a]["name"].ToString());
                cmbCategory.DataValueField = ds.Rows[a]["Id"].ToString();
                                          
                a++;
            }

            if (!IsPostBack)
            {
                PopulateDS();        // FILL DATASET WITH MASTER DATA.
                ShowSubcategoryDetails();    // SHOW EMPLOYEE DETAILS IN THE GRIDVIEW.

            }
        }

        protected void butSubmit_Click(object sender, EventArgs e)
        {
            connection_sql obj = new connection_sql();
            obj.MakeConnection();
                      
            DataTable dt = obj.getDataTable(@"SELECT ID
  FROM[dbo].[tblCategory] where name ='"+ cmbCategory.Text + "'");

            

            int x = obj.ExcuteCommand(@"INSERT INTO  [dbo].[tblSubcategory]
            ([Name], [category_Id], Status ) 
            VALUES('" + txtSubcategory.Text.Trim() + "', " + dt.Rows[0]["ID"].ToString() + ", 'Active')");

            obj.CloseConnection();
            if (x > 0)
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "set", "alert('Save Successfully');", true);
                txtSubcategory.Text = "";
            }
        }


        // CANCEL ROW EDITING.
        protected void GridView_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
        {
            GridView.EditIndex = -1;
            ShowSubcategoryDetails();
        }
        
        // ROW EDITING
        protected void GridView_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
        {
            GridView.EditIndex = e.NewEditIndex;
            ShowSubcategoryDetails();
            lblUpdate.Text = "";
        }

        private void PopulateDS()
        {
            dt.Clear();
            connection_sql obj = new connection_sql();
            obj.MakeConnection();
            dt = obj.getDataTable(@"SELECT  ROW_NUMBER() OVER (ORDER BY c.name, sc.Name) AS 'Sr No', sc.ID,
                c.name as Category, sc.Name as Subcategory, sc.Status
                FROM [tblSubcategory] sc inner join tblCategory c on sc.category_ID = c.id 
                order by c.name, sc.Name ");
        }

        private void ShowSubcategoryDetails()
        {
            dt.Clear();
            connection_sql obj = new connection_sql();
            obj.MakeConnection();
            dt = obj.getDataTable(@"SELECT  ROW_NUMBER() OVER (ORDER BY c.name, sc.Name) AS 'Sr No', sc.ID,
                c.name as Category, sc.Name as Subcategory, sc.Status
                FROM [tblSubcategory] sc inner join tblCategory c on sc.category_ID = c.id 
                order by c.name, sc.Name ");

            GridView.DataSource = dt;
            GridView.DataBind();
        }

        protected void GridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && GridView.EditIndex == e.Row.RowIndex)
            {
                DropDownList drpStatus = (DropDownList)e.Row.FindControl("drpStatus");
                string SelectedText = DataBinder.Eval(e.Row.DataItem, "Status").ToString();
                drpStatus.Items.FindByText(SelectedText).Selected = true;
            }

            if (e.Row.RowType == DataControlRowType.DataRow && GridView.EditIndex == e.Row.RowIndex)
            {
                TextBox txtSubCategoryInGrid = (TextBox)e.Row.FindControl("txtSubcategoryInGrid");
                string Text = DataBinder.Eval(e.Row.DataItem, "Subcategory").ToString();
                txtSubCategoryInGrid.Text = Text;
            }
        }

        protected void UpdateStatus(object sender, GridViewUpdateEventArgs e)
        {
            string Status = (GridView.Rows[e.RowIndex].FindControl("drpStatus") as DropDownList).SelectedItem.Text;
            string xstrSubcategory = (GridView.Rows[e.RowIndex].FindControl("txtSubcategoryInGrid") as TextBox).Text;
            string _ID = GridView.DataKeys[e.RowIndex].Value.ToString();

            connection_sql obj = new connection_sql();
            obj.MakeConnection();
            obj.ExcuteCommand("UPDATE tblsubCategory SET Status = '" + Status + "', Name ='" + xstrSubcategory + "'  WHERE ID = " + _ID);
            lblUpdate.Text = "Record updated successfully.";
            Response.Redirect(Request.Url.AbsoluteUri);
        }


    }
}

TechEthical Mini WebShell Version 1.0, Coded By The_M@T3