I have an page called Update.aspx, and in it I am updating all values instead of filename. When I am going to update info, all values are fetched from the database to corresponding textboxes. But if I have a filename such as abc.jpg
in the database and I go on the update page without browsing the file and update, it's inserted as null
.
How can I keep the same file (i.e. abc.jpg) in database even though it's not updated by user?
This is my code:
string onlyname = string.Empty;
string filename = "";
string path = "";
if (FileUp.HasFile)
{
filename = FileUp.PostedFile.FileName;
path = Server.MapPath("/clients_images/") + FileUp.FileName;
onlyname = path.Substring(path.LastIndexOf("\\") + 1);
FileUp.SaveAs(path);
}
else
{
onlyname = dr["Client_Logo"].ToString().Trim();
}
param[4] = new SqlParameter("@Client_Logo", SqlDbType.VarChar, 200);
param[4].Value = onlyname.ToString().Trim();
cmd.Parameters.Add(param[4]);
ASPX code:
<table align="center" cellpadding="3" cellspacing="0" border="0" width="638" class="tableborder">
<tr>
<td colspan="2" class="mainbg" align="center" height="13">
<font class="general">Update Client Detail</font></td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<b>Client Name :</b></td>
<td align="left">
<asp:TextBox ID="txtUpdateclientname" Text="" Columns = "30" CssClass="checkbox02" runat="server" onchange="Javascript: return initialCap(this);"></asp:TextBox>
<font class="mandatory">*</font>
</td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<b>Company Name :</b></td>
<td align="left">
<asp:TextBox ID="updatecompanyname" Text="" Columns = "30" CssClass="checkbox02" runat="server" onchange="Javascript: return initialCap(this);"></asp:TextBox>
<font class="mandatory">*</font>
</td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<b>Email :</b></td>
<td align="left">
<asp:TextBox ID="updateemail" Text="" Columns = "30" CssClass="checkbox02" runat="server"></asp:TextBox>
<font class="mandatory">*</font>
</td>
</tr>
<tr>
<td class="general" width="50%" valign="top" align="right">
<b>Logo :</b></td>
<td class="general" width="50%" align="left">
<asp:FileUpload ID="FileUp" runat="server" CssClass="checkbox02" />
<br /> (Height of image should not be more than 67 And Width of image should not be more than 380)
</td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<%--<b><font class="bluetext"><strong> Upload : </strong>--%></td>
<td align="left">
<asp:LinkButton ID="viewImage" Text="View Existing" runat="server" cssClass="general-white" class="link" OnCommand="LinkButton_Command" CommandName="downloadfile" ToolTip="Click to View Existing" Font-Bold="true"></asp:LinkButton>
</td>
</tr>
<tr>
<td class="general" width="50%" align="right" valign="top">
<b>Proje开发者_C百科ct Type :</b></td>
<td width="100%" align="left" valign="top" colspan="2">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="right" valign="top" colspan="3">
<asp:ListBox ID="LstUserLeft" Width="100%" Autoscroll="false" SelectionMode="Multiple" Height="150" CssClass="checkbox02"
runat="server">
</asp:ListBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
</tr>
</table>
You can use HiddenField
or ViewState
to store the filename from the Client_logo field (from the database).
For instance.
Assign the value to the HiddenField1
while retrieving result,
HiddenField1.Value=dr["Client_Logo"].ToString().Trim();
and while updating,
if (FileUp.HasFile)
{
...
}
else
{
onlyname = HiddenField1.Value;
}
精彩评论