Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi guys,

Please somebody tell me how to update an image in a grid view by using an upload button for each row?
Posted
Comments
Al Moje 24-Oct-11 5:48am    
Are you using a virtual directory to save your image or you are saving it in the database table as a varbinary(max). Could you post your code that you had been made...

 
Share this answer
 
In Html Code:

ASP.NET
<asp:GridView ID="gridAdayPreprcircltimactiv" runat="server" AutoGenerateColumns="False" 
            onselectedindexchanged="gridAdayPreprcircltimactiv_SelectedIndexChanged">
            <columns>
                <asp:BoundField DataField="intAdayPreprcircltimactivId" HeaderText="AdayPreprcircltimactivId" 
                    InsertVisible="False" ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="strAdayPreprcircltimactivText" HeaderText="AdayPreprcircltimactivText" 
                    InsertVisible="False" ReadOnly="True" SortExpression="ID" />
                <asp:TemplateField HeaderText="Image">
                    <itemtemplate>
                        <asp:Image ID="Image1" runat="server" Height="28px" 
                            ImageUrl='<%# "HandlerAdayPreprcircltimactiv.ashx?intAdayPreprcircltimactivId=" + Eval("intAdayPreprcircltimactivId")%>' Width="23px" />
                    </itemtemplate>
                
                <asp:CommandField HeaderText="Select" ShowSelectButton="True" />
            </columns>



in Save Button Code:
C#
protected void btnsave_Click(object sender, EventArgs e)
    {
        FileUpload img = (FileUpload)upload_file;
        Byte[] imgByte = null;
        if (upload_file.HasFile && img.PostedFile != null)
        {
            HttpPostedFile File = upload_file.PostedFile;
            imgByte = new Byte[File.ContentLength];
            File.InputStream.Read(imgByte, 0, File.ContentLength);
        }

        string sql = "INSERT INTO DPSAdayPreprcircltimactiv(intAdayPreprcircltimactivId,strAdayPreprcircltimactivText,strImageurl) VALUES(@intAdayPreprcircltimactivId,@strAdayPreprcircltimactivText,@strImageurl)";
        SqlCommand cmd = new SqlCommand(sql, (SqlConnection)Application.Get("DPS"));
        cmd.Parameters.AddWithValue("@intAdayPreprcircltimactivId", txtAdayPreprcircltimactivid.Text.Trim());
        cmd.Parameters.AddWithValue("@strAdayPreprcircltimactivText", txtAdayPreprcircltimactiv.Text.Trim());
        cmd.Parameters.AddWithValue("@strImageurl", imgByte);
        cmd.ExecuteNonQuery();
        load_dgitems();
   
    }


in Update Button:

C#
protected void btnupdate_Click(object sender, EventArgs e)
    {
        FileUpload img = (FileUpload)upload_file;
        Byte[] imgByte = null;
        if (upload_file.HasFile && img.PostedFile != null)
        {
            HttpPostedFile File = upload_file.PostedFile;
            imgByte = new Byte[File.ContentLength];
            File.InputStream.Read(imgByte, 0, File.ContentLength);
        }
        string sql = "update DPSAdayPreprcircltimactiv set strAdayPreprcircltimactivText=@strAdayPreprcircltimactivText,strImageurl=@strImageurl where intAdayPreprcircltimactivId=@intAdayPreprcircltimactivId";
        SqlCommand cmd = new SqlCommand(sql, (SqlConnection)Application.Get("DPS"));
        cmd.Parameters.AddWithValue("@strAdayPreprcircltimactivText", txtAdayPreprcircltimactiv.Text.Trim());
        cmd.Parameters.AddWithValue("@strImageurl", imgByte);
        cmd.Parameters.AddWithValue("@intAdayPreprcircltimactivId", txtAdayPreprcircltimactivid.Text.Trim());
        cmd.ExecuteNonQuery();
        load_dgitems();
    }


In Handle You Write this Code:


C#
<%@ WebHandler Language="C#" Class="HandlerAdayPreprcircltimactiv" %>

using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

public class HandlerAdayPreprcircltimactiv : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) 
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

        // Create SQL Command 

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select intAdayPreprcircltimactivId,strAdayPreprcircltimactivText,strImageurl from DPSAdayPreprcircltimactiv where intAdayPreprcircltimactivId =@intAdayPreprcircltimactivId";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;

        SqlParameter ImageID = new SqlParameter("@intAdayPreprcircltimactivId", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["intAdayPreprcircltimactivId"];
        cmd.Parameters.Add(ImageID);
        con.Open();
        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();
        context.Response.BinaryWrite((byte[])dReader["strImageurl"]);
        dReader.Close();
        con.Close();
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}


Try this Functionality.


Regards,


Anilkumar.D
 
Share this answer
 
v3

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