Click here to Skip to main content
15,914,924 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
good evening,

i am doing insert,edit delete by using gridview.

my aspx code id

C#
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView2.aspx.cs" Inherits="GridView2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnadd" runat="server" Text="Add" onclick="btnadd_Click" />

         <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" 
            onrowdeleting="gv_RowDeleting" onrowediting="gv_RowEditing" 
            onrowupdating="gv_RowUpdating" onselectedindexchanged="gv_SelectedIndexChanged">

        <columns>

        <asp:TemplateField HeaderText="ID">
        <itemtemplate>
            <asp:Label ID="lblid" runat="server" Text='<%# Eval("ID") %>'>
            
        </itemtemplate>
        

        <asp:TemplateField HeaderText="Firstname">
        <itemtemplate>
            <asp:Label ID="lblfirstname" runat="server" Text='<%# Eval("firstname") %>'>
            
        </itemtemplate>
        <edititemtemplate>
            <asp:TextBox ID="txtfirstname" runat="server" Text='<%# Eval("firstname") %>'>
            
        </edititemtemplate>
        

         <asp:TemplateField HeaderText="Lastname">
        <itemtemplate>
            <asp:Label ID="lbllastname" runat="server" Text='<%# Eval("lastname") %>'>
            
        </itemtemplate>
        <edititemtemplate>
            <asp:TextBox ID="txtlastname" runat="server" Text='<%# Eval("lastname") %>'>
            
        </edititemtemplate>
        

        <asp:TemplateField>
            <itemtemplate>
                <asp:LinkButton ID="lbedit" runat="server" CommandName="Edit">Edit
                <asp:LinkButton ID="lbdelete" runat="server" CommandName="Delete">Delete
            </itemtemplate>
            <edititemtemplate>
            <asp:LinkButton ID="lbupdate" runat="server" CommandName="Update">Update
            <asp:LinkButton ID="lbcancel" runat="server" CommandName="Cancel">Cancel
            </edititemtemplate>
            

        </columns>

        
    </div>
    </form>
</body>
</html>


and my .cs code is

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.Odbc;

public partial class GridView2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    public void Bind()
    {
        OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
        con.Open();
        OdbcCommand cmd = new OdbcCommand("select * from add2", con);
        OdbcDataAdapter da = new OdbcDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        gv.DataSource = dt;
        gv.DataBind();
        con.Close();
    }

    protected void btnadd_Click(object sender, EventArgs e)
    {
        Response.Redirect("Add.aspx");
    }

    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv.EditIndex = e.NewEditIndex;
        Bind();
    }
    public void UpdateData(string firstname, string lastname)
    {
        OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
        con.Open();
        OdbcCommand cmd = new OdbcCommand("update add2 set Firstname='" + firstname + "'", con);
        cmd.ExecuteNonQuery();
        Bind();
        con.Close();
    }

    protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
        con.Open();
        string firstName = gv.DataKeys[e.RowIndex].Value.ToString();
        GridViewRow row = gv.Rows[e.RowIndex];
        TextBox lastname = (TextBox)row.FindControl("txtlastname");
        UpdateData(firstName, lastname.Text);
        con.Close();
    }

    protected void gv_RowCancellingedit(object sender, GridViewCancelEditEventArgs e)
    {
        gv.EditIndex = -1;
        Bind();
    }



    public void DeleteDate(int id, string firstname, string lastname)
    {
        OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
        con.Open();
        OdbcCommand cmd = new OdbcCommand("delete from add2 where ID='" + id + "'", con);
        cmd.ExecuteNonQuery();
        Bind();
        con.Close();
    }

    protected void gv_SelectedIndexChanged(object sender, EventArgs e)
    {
    }

    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
        con.Open();
        string firstName = gv.DataKeys[e.AffectedRows].Value.ToString();
        GridViewRow row = gv.Rows[e.AffectedRows];
        OdbcCommand cmd = new OdbcCommand("delete from add2 where Firstname='" + firstName + "' ", con);
        cmd.ExecuteNonQuery();
        Bind();
        con.Close();
    }
}


my gridview is binding but edit and delete operations are not working.

please help me.
Posted
Updated 8-May-13 2:00am
v3
Comments
Aarti Meswania 8-May-13 8:13am    
first put debug point in gv_RowDeleting and updating Events.
check it fired or not

C#
cmd = new MySqlCommand("delete from add2 where ID=@id", con);
            // Adding the parameter for deleting the record.
            cmd.Parameters.AddWithValue("@id", GridView1.Rows[e.RowIndex].Cells[Change Indexof ID Colunm ].Text);
 
Share this answer
 
Hi...


See in your delete() use like blow.

C#
cmd = new MySqlCommand("delete from add2 where ID=@id", con);
            // Adding the parameter for deleting the record.
            cmd.Parameters.AddWithValue("@id", GridView1.Rows[e.RowIndex].Cells[1].Text);


and for edit() like this


gv.EditIndex = e.NewEditIndex;

its may helpful for u.
thank u.
 
Share this answer
 
Comments
Member 9567873 8-May-13 8:06am    
i have changed the code as you said but then also it is not working

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