Click here to Skip to main content
15,889,892 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am making an admin main page for my website.
It contains a drop down box that should retrieve the name of all the tables in the db.
When a table is selected, the contents of it should be displayed in the grid box.
Once the contents are displayed a user can update/edit/add/delete using the same grid box.

Currently I have the data entered in grid box manually that contains name of each and every table.
Also when a user selects a table, the contents are displayed in the grid box using following code:
C#
protected void ddSelectTable_SelectedIndexChanged(object sender, EventArgs e)
        {


            SqlConnection conn = new SqlConnection();
            conn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=OCBF;Integrated Security=True");

            conn.Open();
            SqlCommand cmd = new SqlCommand();
            //cmd.Parameters.AddWithValue("selectedValue", Convert.ToString(ddSelectTable.SelectedValue));
            sqlquery = "select * from " + Convert.ToString(ddSelectTable.Text);


            cmd.Connection = conn;

            cmd.CommandText = sqlquery;
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();
            da.Fill(dt);


            gvData.DataSource = dt;
            gvData.DataBind();

            conn.Close();

        }

-------------------
THE PROBLEM ARISES WHEN I TRY TO UPDATE OF DELETE ANYTHING.
I have business logic where the variables are defined.

The main data for edit/update/add is in Data Access Class where the select/edit functions are defined.

And below is the code of the actual page: It has a drop down box, a grid box and a button.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using OCBBusinessObjectsLayer;
using OCBDataAccessLayer;

public partial class Masters_CategoryMaster : System.Web.UI.Page
{

    #region Variables

    clsDACategory objDACategory;
    clsBOCategory objBOCategory;
    

    #endregion

    void FillGrid()
    {
        gvCategory.DataSource = new clsDACategory().select();
        gvCategory.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGrid();

        }
    }
    protected void gvCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCategory.EditIndex = e.NewEditIndex;
        FillGrid();

    }
    protected void gvCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        objBOCategory = new clsBOCategory();
        objBOCategory.CategoryID=Convert.ToInt32(((Label)gvCategory.Rows[e.RowIndex].Controls[1].Controls[1]).Text); //typecasting - converting control to label
        objBOCategory.CategoryName = ((TextBox)gvCategory.Rows[e.RowIndex].FindControl("txtCategoryNameEdit")).Text;

        if (new clsDACategory().update(objBOCategory) > 0)
        {
            Response.Write("Updated Successfully!");
            gvCategory.EditIndex = -1; // it'll make edit option disable and blocked for any further change.
            FillGrid();
        }
        else
        {
            Response.Write("Updation failed");
        }
    }
    protected void gvCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvCategory.EditIndex = -1;
        FillGrid();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (btnAdd.Text.Equals("Add"))
        {
            gvCategory.FooterRow.Visible = true; //a text box gets visible in footer side of the grid box. by default its hidden.
            btnAdd.Text = "Save";
        }
        else
        {
            objDACategory = new clsDACategory();
            objBOCategory = new clsBOCategory();

            objBOCategory.CategoryName = ((TextBox)gvCategory.FooterRow.FindControl("txtCategoryNameAdd")).Text;
            if (objDACategory.insert(objBOCategory) > 0)
            {
                Response.Write("Inserted Succesfully");
                FillGrid();
                btnAdd.Text = "Add";
            }
            else {
                Response.Write("Insertion Failed");
            }

        }
    }
}

---------------------------


Note : The above coding is only for one single table named as CategoryMaster.

I need serious quick help on getting this logic to work with the proper coding as I have invested a week after this and not reaching any conclusion.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900