So i have a gridview that displays image name,images and image link to download from the SQL server database. I want to save the image path in database once an image is downloaded (upon link button click) in a separate db table. Please help me! I need this asap ;(
This Image download webform is connected to my another webform-image upload which has ImgPath (nvarchar255) field.
Below are my codes:
Overview: What i have so far
1. Gridview of images and image links
2.Gridview is retrieved from database
3.Able to download the image
What i need:
1.When image is downloaded--> Save the image path to sql server database
What I have tried:
aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField HeaderText="ID" DataField="AdvID" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Item" DataField="Item" />
<asp:ImageField HeaderText="Image" DataImageUrlField="ImgPath" ControlStyle-Height="120" ControlStyle-Width="140">
<ControlStyle Height="120px" Width="140px"></ControlStyle>
</asp:ImageField>
<asp:TemplateField HeaderText="View Information">
<ItemTemplate>
<asp:LinkButton ID="lnkView" runat="server" CommandArgument='<%#Eval("AdvID") %>' OnClick="lnk_OnClick">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DownloadLink">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("Item") %>' Text='<%# Eval("Item") %>' CommandName="Download"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs:
public partial class AdvGridView : System.Web.UI.Page
{
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
Response.Clear();
Response.ContentType = "application/octect-stream";
Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Images/") + e.CommandArgument);
Response.End();
}
}
void FillGridView()
{
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("ViewAll", sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
sqlCon.Close();
GridView1.DataSource = dtbl;
GridView1.DataBind();
}
protected void lnk_OnClick(object sender, EventArgs e)
{
int AdvertisementID = Convert.ToInt32((sender as LinkButton).CommandArgument);
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("ViewByID", sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDa.SelectCommand.Parameters.AddWithValue("@AdvID", AdvertisementID);
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
sqlCon.Close();
}
private void BindGrid()
{
GridViewService.WebService service = new GridViewService.WebService();
GridView1.DataSource = service.Get();
GridView1.DataBind();
}
}