Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my aspx.cs code.........

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class gallery : System.Web.UI.Page
{
    String fn;
    String path;
    SqlConnection cnn = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adp;
    DataTable dt;
    Int32 id;
    String album_name;
    String caption;
    String image;

    protected void Page_Load(object sender, EventArgs e)
    {
        cnn.ConnectionString = ConfigurationManager.ConnectionStrings["Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True"].ConnectionString;
        cnn.Open();
        
        if (cnn.State == ConnectionState.Closed)
        {
            cnn.Open();
        }
        cnn.Close();

        if (IsPostBack == false)
        {
            
            grd_bind();
        }
    
    }
     protected void btninsert_Click(object sender, EventArgs e)
    {
           if (txtuploder.PostedFile.ContentLength > 0)
        {
            fn = Path.GetFileName(txtuploder.FileName);
            path = Server.MapPath("images") + "/" + fn;
            txtuploder.SaveAs(path);
        }
        if (cnn.State == ConnectionState.Closed)
        {
            cnn.Open();
        }
       
        SqlCommand cmd = new SqlCommand("tb_gallery_insert", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = cnn;
        cmd.Parameters.AddWithValue("@album_name", txtalbum.Text);
        cmd.Parameters.AddWithValue("@caption", txtcaption.Text);
        cmd.Parameters.AddWithValue("@image", fn);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        grd_bind();
        cnn.Close();
        clr();
    }

     private void grd_bind()
     {
         if (cnn.State == ConnectionState.Closed)
         { 
             cnn.Open(); }
       
         adp = new SqlDataAdapter("SELECT * FROM tb_gallery ", cnn);
      
         dt = new DataTable();
     
         adp.Fill(dt);
        
         adp.Dispose();
        
         GridView1.DataSource = dt;
         GridView1.DataBind();
     }

     private void clr()
     {
         txtalbum.Text = "";
         txtcaption.Text = "";
     }


     protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
     {
         try
        {
            if (cnn.State == ConnectionState.Closed)
            {
                cnn.Open();
            }

            id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);
           
            SqlCommand cmd = new SqlCommand("tb_gallery_delete", cnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = cnn;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
           
            SqlDataAdapter adp = new SqlDataAdapter("select * from tb_gallery where id=@id", cnn);
           
            adp.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;
            DataSet ds = new DataSet();
          
            adp.Fill(ds);
            try
            {
                
                image = Convert.ToString(ds.Tables[0].Rows[0]["image"]);
              
                File.Delete(Server.MapPath("images") + "\\" + image);
            }
            catch { }
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            grd_bind();
        }
        catch {
       
        }

    
     }
     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
     {
         GridView1.EditIndex = -1;
         grd_bind();
     }
     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
     {
         GridView1.EditIndex = e.NewEditIndex;
         grd_bind();
     }
     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
     {
         if (cnn.State == ConnectionState.Closed)
        {
            cnn.Open();
        }

        id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label4"))).Text);
        
        album_name = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_album_name"))).Text);
        
        
        caption = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_caption"))).Text);
       
        SqlCommand cmd = new SqlCommand("tb_gallery_update", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = cnn;
       
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
        cmd.Parameters.Add("@album_name", SqlDbType.VarChar, 50).Value = album_name;
        cmd.Parameters.Add("@caption", SqlDbType.VarChar, 50).Value = caption;
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        GridView1.EditIndex = -1;
       
        grd_bind();
    }

     protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
     {
         GridView1.PageIndex = e.NewSelectedIndex;
         grd_bind();
     }
}

this is my aspx code...........

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

<!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:TextBox ID="txtalbum" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label1" runat="server" Text="AlbumName"></asp:Label>
        <br />
        <br />
        <asp:TextBox ID="txtcaption" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label2" runat="server" Text="Caption"></asp:Label>
        <br />
        <br />
        <asp:FileUpload ID="txtuploder" runat="server" />
&nbsp;
        <asp:Label ID="Label3" runat="server" Text="Image"></asp:Label>
        <br />
        <br />
        <asp:Button ID="btninsert" runat="server" Text="Insert"
            onclick="btninsert_Click" />

                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    onrowcancelingedit="GridView1_RowCancelingEdit"
                    onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
                    onrowupdating="GridView1_RowUpdating"
                    onselectedindexchanging="GridView1_SelectedIndexChanging"
            onselectedindexchanged="GridView1_SelectedIndexChanged">
                    <Columns>
                    <%--here i am using templatefields to for binding the gridview--%>
                        <asp:TemplateField HeaderText="Album_name">
                            <EditItemTemplate>

                     <%--here i am using the textbox in the edititmtemplatefield to upadate the data--%>
                                <asp:TextBox ID="txt_album_name" runat="server"
                                    Text='<%# Eval("album_name") %>'></asp:TextBox>
                                <asp:Label ID="Label4" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("album_name") %>'></asp:Label>
                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Caption">
                            <EditItemTemplate>
                                <asp:TextBox ID="txt_caption" runat="server" Text='<%# Eval("caption") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Eval("caption") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Image">
                        <ItemTemplate>
                        <%--for displaying the image inside the gidview here i'm using the <img>tag
                        and specify the path of the folder where image is stored--%>
                        <img alt ="" src ='images/<%#Eval("image") %>' height="50px" width="50px"/>
                        </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Delete">
                        <%--here i am using the linkbutton to delete the record and specify the command name
                        of this linkbutton is delete--%>
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                    CommandName="Delete"
                                    onclientclick="return confirm('are you sure you want to delet this column')">Delete</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Update">
                            <EditItemTemplate>
                           <%-- here i am using the s linkbuttons to update the record and a  cancel button
                           and set the commandname of update button is update and the cancel button is cancel --%>
                                <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False"
                                    CommandName="Update">Update</asp:LinkButton>
                                <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False"
                                    CommandName="Cancel">Cancel</asp:LinkButton>
                            </EditItemTemplate>
                            <ItemTemplate>
                               <%--here i am using the linkbutton for edit and specify the command name
                        of this linkbutton is Edit--%>
                                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
                                    CommandName="Edit">Edit</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

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


this is error..................

Server Error in '/WebSite11' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'System.Web.UI.WebControls.GridViewDeletedEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewDeletedEventArgs' could be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 105:            }<br />
Line 106:<br />
Line 107:            id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);<br />
Line 108:           <br />
Line 109:            SqlCommand cmd = new SqlCommand("tb_gallery_delete", cnn);
Posted
Updated 13-Feb-12 1:55am
v2
Comments
Rajesh Anuhya 13-Feb-12 7:55am    
Edited: Code Tags added.
--RA
Herman<T>.Instance 14-Mar-12 11:33am    
e.Row.RowIndex ?

In this case I would recommend reading the exception message. The class System.Web.UI.WebControls.GridViewDeletedEventArgs indeed has no member with the name 'RowIndex', at least if I can believe MSDN. Here is what you posted:
C#
//Line 107: 
id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);

Assuming that e is indeed of the type GridViewDeletedEventArgs, this will not compile and cause exactly the error you have posted. You will have little other choice than to take a look at the class GridViewDeletedEventArgs in MSDN and then fix this event handler.
 
Share this answer
 
v3
Comments
ProEnggSoft 14-Mar-12 11:28am    
Edit: pre tag for C# and code tags added - PES
use datakeys inplace of Delete Event

remove
C#
id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);

using datakeys
 
Share this answer
 
v2
Comments
ProEnggSoft 14-Mar-12 11:28am    
Edit: pre tag for C# and code tags added - PES
using this instead of
C#
id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);

using datakeys
C#
string drawid = GridView1.DataKeys[e.RowIndex].Value.ToString();
 
Share this answer
 
v2
Comments
[no name] 13-Feb-12 8:13am    
This will fail as well. The GridViewDeletedEventArgs class has no member 'RowIndex'.
Abhi KA 13-Feb-12 9:59am    
try without row index
ProEnggSoft 14-Mar-12 11:24am    
Edit: pre tag for C# code added - PES
On
C#
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
, replacing "GridViewDeletedEventArgs" to "GridViewDeleteEventArgs"

Hope this help...
 
Share this answer
 
v2
Comments
ProEnggSoft 14-Mar-12 11:30am    
Edit: pre tag for C# and code tags added - PES
The Event Handler name is not correct, that is why it couldn't find the RowIndex definition. Instead of GridViewDeletedEventArgs, using GridViewDeleteEventArgs (Delete without d at the end)
 
Share this answer
 
Comments
phil.o 21-Oct-15 13:58pm    
Do you realize this question is over 3 years old?
CHill60 23-Oct-15 11:35am    
Not only is the question over 3 years old, but you have added nothing to what has already been stated in earlier solutions.
For the record, both this and Solution 4 are wrong. The GridView_RowDeleted handler requires GridViewDeletedEventArgs not GridViewDeleteEventArgs. The OP might well be using the wrong handler but that is not what you have said. OP should have used e.Keys property.

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