2
Reply

HOW TO PERFORM EDIT,UPDATE AND DELETE OPERATION ON AUTO GENERATED DATAGRIDVIEW???

arun kumar

arun kumar

Nov 15 2012 1:06 AM
2.7k

I Have 2 Dropdown Lists and 1 Data Grid View

1st Dropdown List Displays DATABASE Names Dynamically... means Dynamically Loaded with DATABASE NAMES.....

2nd Dropdown List is Loaded with TABLE NAMES Of respective database which is selected in
1st Dropdown List...

Data of particular Table is Displayed in DATA GRIDVIEW which is Auto Generated DATA Grid View....

Now i want to  TO PERFORM EDIT,UPDATE AND DELETE OPERATION ON AUTO GENERATED DATAGRIDVIEW???

Following is the code

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

namespace WebApplicationSimpleGridView
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         

            if (!IsPostBack)
            {
                populatedatabasenames();
               
            }

        }

        private void populatedatabasenames()
        {
            try
            {
                //string connectionstring = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
                SqlConnection con;
                con = new SqlConnection(@"Data Source=ARUN\SQLEXPRESS;Integrated Security=True");
                SqlDataAdapter da = new SqlDataAdapter("SELECT name, collation_name FROM sys.databases ", con);
                DataSet ds = new DataSet();
                da.Fill(ds, "db");
                drddatabase.DataSource = ds.Tables["db"];
                drddatabase.DataTextField = "name";
                drddatabase.DataValueField = "name";
                drddatabase.DataBind();
            }

            catch (SqlException ex)
            {

            }


        }
        private void populatetablenames()
        {

            try
            {
                SqlConnection con;
                con = new SqlConnection(@"Data Source=ARUN\SQLEXPRESS;Integrated Security=True");

                //SqlDataAdapter da = new SqlDataAdapter("select name  from   " + DropDownList1.SelectedValue + "." + "sys" + "." + "tables ", con);
               
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select name as TableName from  " + drddatabase.SelectedValue + "." + "sys" + "." + "tables ";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds);
            
                drdtablename.DataSource = ds;
                drdtablename.DataTextField = "TableName";
                drdtablename.DataValueField = "TableName";
                Label1.Text = "";
            }

            catch (SqlException ex)
            {
              

            }
            drdtablename.DataBind();
        }

        protected void drddatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
            populatetablenames();
        }

        private void PopulateGridView()
        {
            try
            {

                SqlConnection con;
                con = new SqlConnection(@"Data Source=ARUN\SQLEXPRESS;" + "Initial Catalog" + "=" +drddatabase.SelectedValue + ";" + "Integrated Security=True");

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select * from " + drdtablename.SelectedValue;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds, "Disp");
                grdview.DataSource = ds.Tables["disp"];
                Label1.Text = " ";
            }
            catch (SqlException ex)
            {
                if (ex.Number == 1045)
                {
                    Label1.Text = "Access Denied";

                }
                else
                {
                    Label1.Text = " ";

                }

            }

            grdview.DataBind();
        }

        protected void drdtablename_SelectedIndexChanged(object sender, EventArgs e)
        {
            PopulateGridView();

        }

        protected void grdview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdview.PageIndex = e.NewPageIndex;
            PopulateGridView();
        }

        protected void grdview_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdview.EditIndex = e.NewEditIndex;
            PopulateGridView();
        }

        protected void grdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdview.EditIndex = -1;
            PopulateGridView();
        }

        protected void grdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
           SqlConnection con;
           SqlCommand cmd1 = new SqlCommand();
           con = new SqlConnection(@"Data Source=CKQJ8433\SQLEXPRESS;" + "Initial Catalog" + "=" + drddatabase.SelectedValue + ";" + "Integrated Security=True");
           cmd1.Connection = con;

        /*   //string id = grdview.DataKeys[e.RowIndex].Value.ToString();

           //string name=grdview.Rows[0].Cells[0].Text;*/

           //string MyVariableData = ((DataRowView)e.Row.DataItem)["COLUMN_NAME"].ToString();
           // string myvar=(DataRowView)e.Keys.
           int i;
           List<String> smp;
            smp=new List<String>();


         smp = grdview.Columns[0].ToString();
          


           //string s = grdview.Cells[0].Text;


           //cmd1.CommandText = "Delete from " + drdtablename.SelectedValue + "where" + grdview.Columns[0] + "=" + "name";
           con.Open();
           cmd1.ExecuteNonQuery();
           con.Close();

           //grdemp.EditIndex= -1;
           PopulateGridView();


       /*     //string id=grdview.DataKeys[e.RowIndex].Value.ToString();

        //    TableCell cell = grdview.Rows[e.RowIndex].Cells[0];

        //    DataRow myDataRow = viewDataSet.Tables[0].Select("ProductID = " + GridView1.DataKeys[e.RowIndex]["ProductID"].ToString())[0];
        //    viewDataSet.Tables[0].Rows.Remove(myDataRow);
        //    viewDataSet.AcceptChanges();

        //    GridView1.DataSource = viewDataSet;
        //    GridView1.DataBind();

           //DataSet dsdelete = null;
           //dsdelete = (DataSet)Session["ds1"];
           //string dbtable = drdtablename.Text;
        
           //dsdelete.Tables["dbtable"].Rows[e.RowIndex].Delete();
           //dsdelete.Tables["dbtable"].AcceptChanges();
           //Session["Ds1"] = dsdelete;
           //Session["Ds1"] = dsdelete;
           //grdview.DataSource = dsdelete.Tables["dbtable"];
           //grdview.DataBind();   */

      

        }


    }
}


Answers (2)