Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a image called theme.jpg. Now whenever i upload a new image it should be saved with the name theme.jpg and the previous theme.jpg should be renamed as theme1.jpg. In the same way i have to rename the images as theme2.jpg,theme3.jpg..............The new uploaded image should be saved as theme.jpg all the time
Posted
Updated 5-Dec-11 21:20pm
v2

.cs file

C#
{
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

XML
<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>
 
Share this answer
 
Use this Code to rename the file

Imports System.IO


File.Move(@"C:\Dir1\SomeFile.txt", @"C:\Dir1\RenamedFileName.txt") 
 
Share this answer
 
v2
Try this code save u r file with theme.jpg:

C#
filename = Server.MapPath("~/Uploads/" + theme.jpg);
            FileUpload1.SaveAs(filename);


then read for all the files in the server folder and rename them using the following code:

C#
File.Copy(oldFileName, NewFileName);

File.Delete(oldFileName);
 
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