Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

When updating grid view all other fields are updating but the image is not, my code is as follows for aspx:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainTable.aspx.cs" Inherits="commonlogin.MainTable" %>

<!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>GridView</title>

    <link href="Content/StyleSheet1.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="False"
            AutoGenerateDeleteButton="True" onrowdeleted="GridView1_RowDeleted" AutoGenerateEditButton ="True"
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating"
            onpageindexchanging="GridView1_PageIndexChanging"
            onrowcancelingedit="GridView1_RowCancelingEdit" AllowPaging="True" CssClass ="bodytext"
            AllowSorting="True" BackColor="White" BorderColor="#336666"
            BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" >


        <Columns>


            <asp:TemplateField HeaderText="ID"  >
                <%--<EditItemTemplate>
                    <asp:TextBox ID="txtID" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
                </EditItemTemplate>--%>
                <ItemTemplate>
                 <%--<asp:Label ID="Label1" runat="server" ImageUrl='<%# "Handler1.ashx?ID=" + Eval("ID")%>'  Text='<%# Bind("ID") %>'></asp:Label>--%>
                    <asp:Label ID="lblID" runat="server" CssClass="bodytext"   Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <EditItemTemplate>
                    <asp:TextBox ID="txtName" runat="server" CssClass ="TextBox"  Text='<%# Bind("Name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server" CssClass="bodytext"  Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Description">
                <EditItemTemplate>
                    <asp:TextBox ID="txtDesc" runat="server" CssClass ="TextBox"  Text='<%# Bind("Description") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblDesc" runat="server" CssClass="bodytext"  Text='<%# Bind("Description") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Image">
                <EditItemTemplate>
                    <asp:TextBox ID="txtImage" runat="server" CssClass ="TextBox"   Text='<%# Bind("Image") %>' ></asp:TextBox>

                </EditItemTemplate>
                <ItemTemplate>
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler1.ashx?ID=" + Eval("ID")%>'/>

                <%--<asp:Image ID="Image2" runat="server" ImageUrl='<%# "Handler1.ashx?ID=" + Bind("ID")%>'/>--%>

                    <%--<asp:Label ID="lblImage" runat="server"  Text='<%# Bind("Image") %>'></asp:Label>--%>

                    <%--<asp:Label ID="lblImage" runat="server"  ImageUrl='<%# "Handler1.ashx?ID=" + Eval("ID") %>'></asp:Label>--%>
                    <%--<asp:Label ID="Label1" runat="server" ImageUrl='<%# "Handler1.ashx?ID=" + Eval("ID")%>'  Text='<%# Bind("Image") %>'></asp:Label>--%>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" BackColor ="AliceBlue" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Active">
                <EditItemTemplate>
                    <asp:TextBox ID="txtActive" runat="server" CssClass ="TextBox" Text='<%# Bind("Active") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblActive" runat="server" CssClass="bodytext"  Text='<%# Bind("Active") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CreatedBy">
                <EditItemTemplate>
                    <asp:TextBox ID="txtCreatedBy" runat="server" CssClass ="TextBox"  Text='<%# Bind("CreatedBy") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblCreatedBy" runat="server" CssClass="bodytext"  Text='<%# Bind("CreatedBy") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CreatedDate">
                <EditItemTemplate>
                    <asp:TextBox ID="txtCreatedDate" runat="server" CssClass ="TextBox"  Text='<%# Bind("CreatedDate") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblCreatedDate" runat="server" CssClass="bodytext"  Text='<%# Bind("CreatedDate") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#487575" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#275353" />
            </asp:GridView><br />
        <tr>

        <td>

        </div>

            <asp:HyperLink ID="HyperLink1" CssClass ="wishlist " NavigateUrl ="~/TableAdd.aspx" runat="server">AddNew</asp:HyperLink><br /><br />

        </td>
        </tr>
        <asp:label id="Messagelbl" forecolor="Red" CssClass="wishlist" runat="server"/>


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


code behind is this:
C#
namespace commonlogin
{
    public partial class MainTable : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
        {
            //SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            if (!IsPostBack)
            {
                bind();
                
            }   
        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lbldeleteid = (Label)row.FindControl("lblID");
            conn.Open();
            //SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);
            SqlCommand cmd = new SqlCommand("Delete tbl_SqlImage where ID=" + lbldeleteid.Text + "", conn);

            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
        }
        public void bind()
        {
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tbl_SqlImage", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count != 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            conn.Close();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
            //GridView1.DataBind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
            //Image Image11 = (Image)row.FindControl("Image1");
            TextBox textName = (TextBox)row.FindControl("txtName");
            TextBox textDesc = (TextBox)row.FindControl("txtDesc");
            TextBox textImage = (TextBox)row.FindControl("txtImage");
            TextBox textActive = (TextBox)row.FindControl("txtActive");
            TextBox textCreatedBy = (TextBox)row.FindControl("txtCreatedBy");
            TextBox textCreatedDate = (TextBox)row.FindControl("txtCreatedDate");
            
            //FileUpload FileUpload1 = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1");
            GridView1.EditIndex = -1;
            conn.Open();

            SqlCommand cmd = new SqlCommand("UPDATE tbl_SqlImage set Name='" + textName.Text + "' , Description='" + textDesc.Text + "' , Image='" + textImage.Text + "' , Active='" + textActive.Text + "' , CreatedBy='" + textCreatedBy.Text + "' , CreatedDate='" + textCreatedDate.Text + "' where ID='" + lblID.Text.Trim() + "' ", conn);

            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
            //GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }        
    }
}

Please help me, can any one give me the code for updating image in gridview.
Posted
Updated 23-Jun-12 4:54am
v2
Comments
Sandeep Mewara 23-Jun-12 10:55am    
You should only add related code snippet instead of dumping everything (even tons of commented lines!)

In your code it will not be updated...
you should use GridView.DataBind() while it's not posting back.
that's why it doesn't appear.
 
Share this answer
 
Comments
Sandeep Mewara 23-Jun-12 10:56am    
OP used bind() method that handles it.
Few observations:
1. You should bing grid with datatable instead of dataset
C#
GridView1.DataSource = ds.Tables[0];
               GridView1.DataBind();


2. I see you are supplying textbox string value for image field. Is that what you intend to do? if you are updating an imagepath then that looks ok or else the code for updating an image does not exists all together.

Use Visual Studio DEBUGGER and step through the code to see if the query formed is correct and has all that is needed. Run the same raw query in Db and check the changes. Everything should be same.
 
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