.cs file
{
FileUpload AdImageFile = (FileUpload)(e.Item.Cells[1].Controls[1]);
if (File.Exists(Server.MapPath("Ad_Picture/" + dgadvetise.DataKeys[e.Item.ItemIndex] + ".jpg")))
File.Delete(Server.MapPath("Ad_Picture/" + dgadvetise.DataKeys[e.Item.ItemIndex] + ".jpg"));
AdImageFile.SaveAs(Server.MapPath("Ad_Picture/" + dgadvetise.DataKeys[e.Item.ItemIndex] + ".jpg"));
cls.UpdateData("AdDetails", new string[] { "AdName", "AdLink", "AdImagePath" }, new string[] { AdName.Text.ToString(), AdLink.Text.ToString(), dgadvetise.DataKeys[e.Item.ItemIndex] + ".jpg" }, "Where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], "Image Details Updated", this.Page);
}
public void UpdateData(string tblName, string[] colName, string[] colValue, string searchCodn, string msg, Page form)
{
try
{
if (colName.Length != colValue.Length)
{
MsgBox("Column Name not equal Column Value", form);
return;
}
string sqlQuery = "select * from " + tblName + " " + searchCodn;
adp = new SqlDataAdapter(sqlQuery, conn);
tbl = new DataTable();
adp.Fill(tbl);
if (tbl.Rows.Count == 0)
{
return;
}
for (int i = 0; i < colName.Length; i++)
{
if ((tbl.Columns[colName[i]].DataType.ToString() == "System.Int16" | tbl.Columns[colName[i]].DataType.ToString() == "System.Int32" | tbl.Columns[colName[i]].DataType.ToString() == "System.Int64" | tbl.Columns[colName[i]].DataType.ToString() == "System.Decimal"))
{
if (colValue[i] != null)
tbl.Rows[0][colName[i]] = Convert.ToInt64(colValue[i]);
}
else
{
if (colValue[i] != null)
tbl.Rows[0][colName[i]] = colValue[i];
}
}
cmdBld = new SqlCommandBuilder(adp);
adp.Update(tbl);
tbl.Dispose();
adp.Dispose();
cmdBld.Dispose();
if (msg != null)
MsgBox(msg, form);
}
catch (Exception ex)
{
MsgBox("Update Data Error: " + ex.Message, form);
}
}
.cs.aspx file
<asp:DataGrid ID="dgadvetise" runat="server" AutoGenerateColumns="False"
PageSize="5" Width="750px" AllowPaging="True" AllowSorting="True" DataKeyField="AdID">
<Columns>
<asp:BoundColumn DataField="AdID" HeaderText="Image ID" Visible="False" />
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<ul class="hoverbox">
<li>
<a href="#"><img id="imgSmall" src='Ad_Picture/<%# DataBinder.Eval(Container, "DataItem.AdImagePath") %>' alt="Gallery Image" width="100" height="100" /></a>
</li>
</ul>
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="flUploadGrid" runat="server" ForeColor="#2A2F0F"></asp:FileUpload>
</EditItemTemplate>
<HeaderStyle Width="110px" />
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>