hello,
How to edit or update file from GridView
I am following this Download (Save) VARBINARY Data as File in ASP.Net using C# and VB.Net and am done everything good
how about if i need update the file
my code SQL
ALTER PROCEDURE [dbo].[vacation]
@Action VARCHAR(10)
,@id INT = NULL
,@idnamev nvarchar(MAX) = NULL
,@namevacation nvarchar(MAX) = NULL
,@idcivilv nvarchar(MAX) = NULL
,@infovacation nvarchar(MAX) = NULL
,@datestart datetime = NULL
,@dateend datetime = NULL
,@vacationdata datetime = NULL
,@el3malv nvarchar(MAX) = NULL
,@datesp10 nvarchar(MAX) = NULL
,@eldarajah nvarchar(MAX) = NULL
,@totalva nvarchar(MAX) = NULL
,@jop nvarchar(MAX) = NULL
,@name nvarchar(MAX) = NULL
,@ContentType nvarchar(MAX) = NULL
,@backupmyfile varbinary(MAX) = NULL
AS
BEGIN
SET NOCOUNT ON;
--SELECT
IF @Action = 'SELECT'
BEGIN
SELECT Id, idnamev, namevacation, idcivilv, infovacation, datestart, dateend,vacationdata,el3malv,datesp10,eldarajah,totalva,jop,name,ContentType,backupmyfile
FROM Table_vacation
WHERE
month(datestart) = month(GETDATE())
and YEAR(datestart) = YEAR(GETDATE())
order by day(datestart) DESC
END
--INSERT
IF @Action = 'INSERT'
BEGIN
INSERT INTO Table_vacation(idnamev, namevacation, idcivilv, infovacation, datestart, dateend,vacationdata,el3malv,datesp10,eldarajah,totalva,jop,name,ContentType,backupmyfile)
VALUES ( @idnamev, @namevacation, @idcivilv, @infovacation, @datestart, @dateend,@vacationdata,@el3malv,@datesp10 ,@eldarajah,@totalva,@jop,@name,@ContentType,@backupmyfile)
END
--UPDATE
IF @Action = 'UPDATE'
BEGIN
UPDATE Table_vacation
SET
idnamev = @idnamev,
namevacation = @namevacation,
idcivilv = @idcivilv,
infovacation = @infovacation,
datestart = @datestart,
dateend = @dateend,
vacationdata = @vacationdata,
el3malv = @el3malv,
datesp10 = @datesp10,
eldarajah = @eldarajah,
totalva = @totalva,
jop = @jop,
backupmyfile = @backupmyfile
WHERE id = @id
END
--DELETE
IF @Action = 'DELETE'
BEGIN
DELETE FROM Table_vacation
WHERE Id = @Id
END
END
HTML:
<asp:BoundField DataField="Name" HeaderText="File Name">
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:TemplateField HeaderText="المرفقات">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="FileUpload2" runat="server" />
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="100px" />
</asp:TemplateField>
C#:
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string datestart = (row.FindControl("txtBoxDate5") as TextBox).Text;
string dateend = (row.FindControl("txtBoxDate6") as TextBox).Text;
string vacationdata = (row.FindControl("txtBoxDate3") as TextBox).Text;
string totalva = (row.FindControl("txttotalva") as TextBox).Text;
string namevacation = (row.FindControl("DropDownListtotal") as DropDownList).SelectedValue;
string constr = ConfigurationManager.ConnectionStrings["kankonConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("vacation"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "UPDATE");
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@datestart", datestart);
cmd.Parameters.AddWithValue("@dateend", dateend);
cmd.Parameters.AddWithValue("@vacationdata", vacationdata);
cmd.Parameters.AddWithValue("@namevacation", namevacation);
cmd.Parameters.AddWithValue("@totalva", totalva);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
GridView1.EditIndex = -1;
this.BindGrid();
}