yes course ;)
I've a db with insert users(IDPersonne), interviews, projects...
I want asscoiate files for each users : CVs, covering letters,...
Therefore I use a FileUpload.aspx with a gridview :
<div>
<asp:FileUpload ID="fileUpload1" runat="server" Width="763px" Height="23px" /><br />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
onclick="btnUpload_Click" />
</div>
<div>
<asp:GridView ID="gvDetails" CssClass="Gridview" runat="server" DataKeyNames="FileID"
AutoGenerateColumns="False" onrowdeleting="gvDetails_RowDeleting"
ForeColor="Black" GridLines="Vertical" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="FileId" HeaderText="FileId" InsertVisible="False"
ReadOnly="True" SortExpression="FileId" />
<asp:BoundField DataField="FileName" HeaderText="FileName"
SortExpression="FileName" Visible="False" />
<asp:BoundField DataField="FilePath" HeaderText="FilePath"
SortExpression="FilePath" Visible="False" />
<asp:BoundField DataField="IDPersonne" HeaderText="IDPersonne"
SortExpression="IDPersonne" />
<asp:HyperLinkField DataNavigateUrlFields="FilePath" DataTextField="FileName"
HeaderText="Download File" />
<asp:TemplateField HeaderText="DELETE" HeaderImageUrl="~/Image/trash2.png">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to delete this record?');"
><asp:Image ID="imgFolder" runat="server" ImageUrl="Image/DeleteRed.png" /></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black"
HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True"
ForeColor="White" />
</asp:GridView>
First I select a user from an other page thanks to a querystring IDP.
I can upload file to save it in the folder Files.
I want to insert a filename in a table FileUpload with a gridview to download the file or delete it.
When I insert the filename in my table to see it in my gridview I want insert like this:
4307_CV.doc
4307 is the IDPersonne from my querystring.
Everything is ok till now.
But I want now insert with the scope_identity :
4307_1CV.doc
4307_2CV.doc
etc.
and my c# FileUpload.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class FileUpload : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BE Intranet 2009 01"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
// Bind Gridview Data
protected void BindGridviewData()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from [FilesUpload] WHERE IDPersonne = @IDPersonne ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@IDPersonne", Request.QueryString["IDP"]);
DataTable ds = new DataTable();
da.Fill(ds);
if (ds.Rows.Count > 0)
{
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
else
{
gvDetails.DataBind();
}
con.Close();
}
// Save files to Folder and files path in database
protected void btnUpload_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("FileUpload", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@FileId", SqlDbType.BigInt);
cmd.Parameters["@FileId"].Direction = ParameterDirection.Output;
cmd.Connection.Open();
string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
if (System.IO.File.Exists(Server.MapPath("Files/" + Request.QueryString["IDP"] + "_" + filename)))
{
Label1.Text = "File Name already exists!";
return;
}
else
{
fileUpload1.SaveAs(Server.MapPath("Files/" + Request.QueryString["IDP"] + "_" + filename));
}
cmd.Parameters.AddWithValue("@FileName", Request.QueryString["IDP"] + "_" + filename);
cmd.Parameters.AddWithValue("@FilePath", "Files/" + Request.QueryString["IDP"] + "_" + filename);
cmd.Parameters.AddWithValue("@IDPersonne", Request.QueryString["IDP"]);
cmd.ExecuteScalar();
// Test if the scope_identity return a value
// I must insert a Label2.text in the FileUpload.aspx
string p = cmd.Parameters["@FileId"].Value.ToString();
Label2.Text = "Record inserted successfully. ID = " + p;
con.Close();
BindGridviewData();
}
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String ID;
ID = gvDetails.DataKeys[e.RowIndex].Value.ToString();
SqlCommand cmd = new SqlCommand("select * from [FilesUpload] WHERE FileId = '" + ID + "'", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
if (System.IO.File.Exists(Server.MapPath(dt.Rows[0][2].ToString())))
{
System.IO.File.Delete(Server.MapPath(dt.Rows[0][2].ToString()));
}
}
cmd = new SqlCommand("delete from [FilesUpload] WHERE FileId = '" + ID + "'", con);
cmd.ExecuteNonQuery();
con.Close();
BindGridviewData();
}
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
BindGridviewData();
}
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvDetails.EditIndex = -1;
BindGridviewData();
}
}
I do not know if I'm clear but if you want further details, do not hesitate.
In any case I thank you infinitely.