hello
i have problem after update the data from Grideview, wen i press edit then press update the file download going my code.
when i insert data with fileupload everything good.
then when i try edit something same data like i edit the txtname or country without change the file download.
when i press update, the download file going/removed.
HTML
<asp:TemplateField HeaderText="ﻣﺮﻓﻘﺎت">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download"
OnClick="DownloadFile" CommandArgument='<%# Eval("Id") %>' Visible='<%#!string.IsNullOrEmpty(Eval("Namefile").ToString()) ? true : false %>'> </asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fuFile" runat="server" />
</EditItemTemplate>
<ControlStyle Width="100px" />
<ItemStyle HorizontalAlign="Center" Width="70px" />
</asp:TemplateField>
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
string UserHostName = HttpContext.Current.Request.UserHostName;
string UserHostip = System.Net.Dns.GetHostName();
string usernamemove = this.Page.User.Identity.Name;
GridViewRow row = GridView1.Rows[e.RowIndex];
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
FileUpload FileUpload1 = row.FindControl("fuFile") as FileUpload;
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = "";
byte[] bytes = null;
if (FileUpload1.HasFile)
{
contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
bytes = br.ReadBytes((Int32)fs.Length);
}
}
}
var massage = (row.FindControl("txtName") as TextBox).Text.Trim();
var note = (row.FindControl("txtCountry") as TextBox).Text.Trim();
using (var con = new SqlConnection(str))
{
var query =
"UPDATE Table_fileupload SET others1 = @others1,totaltimespplus = @totaltimespplus,noteme = @noteme,note = @note, massage = @massage, Namefile = @Namefile, ContentType = @ContentType, backupmyfile = @backupmyfile WHERE id = @Id";
using (var cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Namefile", fileName);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@backupmyfile", bytes == null ? new byte[] { } : bytes);
cmd.Parameters.AddWithValue("@massage", massage);
cmd.Parameters.AddWithValue("@note", note);
// التتبع
cmd.Parameters.AddWithValue("@others1", usernamemove);
cmd.Parameters.AddWithValue("@totaltimespplus", Convert.ToDateTime(DateTime.Now.ToLongTimeString()));
cmd.Parameters.AddWithValue("@noteme", "(تعديل)");
cmd.Parameters.AddWithValue("@machineName", GetComputerName(Request.UserHostAddress));
cmd.Parameters.AddWithValue("@localIP", UserHostName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
GridView1.EditIndex = -1;
BindGrid2SP();
Response.Redirect(Request.Url.AbsoluteUri);
}