Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi all

first i want to get tables from schema and select table to display its data in gridview in another page
i want to add delete & edit templates to this grid view but i have errors
please help me ??


to get schema tables i used :

Sql statement in data source :

SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

             string y  = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
             Session["table_name"] = y;
             Response.Redirect("Default2.aspx");
    }


at next page to get table data i used :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            table_columns();

        }

    }
private void table_columns()
    {
        string x;
        x= Session["table_name"].ToString();
        string sql = "Select * from " + x+ "";
        SqlCommand cmd = new SqlCommand(sql, connstr);

        SqlDataAdapter dt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dt.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();


        connstr.Close();
    }
Posted
Comments
Wendelius 18-Dec-11 15:59pm    
What are the exact errors and if you debug where do they occur?
shms_rony 18-Dec-11 16:00pm    
i want to add delete button in gridview
Code 89 19-Dec-11 23:38pm    
thank you..

You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work.values of these key fields are passed to the data source control in order to specify the row to update or delete.


so set datakeyname first..


go thru this link
http://www.codedigest.com/Articles/ASPNET/212_How_to_edit_and_Delete_records_in_a_gridview_using_datakeys.aspx
 
Share this answer
 
v2
add delete button as template field
C#
 <asp:templatefield itemstyle-width="40px" xmlns:asp="#unknown">
                 <itemtemplate>
                 <asp:linkbutton id="lb1" runat="server" causesvalidation="false" onclientclick="return confirm('Are You sure to delete this entry')" text="Delete" commandargument="<%#Eval("id")%>"></asp:linkbutton>
                 </itemtemplate>
                 
<itemstyle width="40px"></itemstyle>
                 
                 </asp:templatefield>


on row command of grid view use
C#
protected void rowcmd(object sender, GridViewCommandEventArgs e)
    {
      // create and open DB connection
        try
        {
            //int index = Convert.ToInt32(e.CommandArgument);
            //GridViewRow row = GridView1.Rows[index];
            Int32 id = Convert.ToInt32(e.CommandArgument);
            string comm = "Delete from tblComments where id=@id";
            SqlCommand cmd = new SqlCommand(comm, Db.GetConnection());
            cmd.Parameters.AddWithValue("id", id);
            cmd.ExecuteNonQuery();
            //ClientScript.RegisterStartupScript
            //(GetType(), "Javascript", "javascript: return alert('Deleted');; ", true);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }

    }
 
Share this answer
 
 
Share this answer
 
Comments
shms_rony 18-Dec-11 16:53pm    
i use these functions but my gridview don't have datakeyname
Use Delete in Row Command



if (e.CommandName == "DEL")
{
//Call ur Auto Generated Id here
Int32 yourID= Convert.ToInt32(e.CommandArgument.ToString());
//
Id is called in method as if you need to delete row one by one
then call fillgrid function
Class.Method(IdofyourProject);
fillgrid();
Reset();
}
 
Share this answer
 

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