Click here to Skip to main content
11,584,443 members (64,205 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi All,
I'm uploading a File into sqlserver using fileupload in file upload Method i m calling a method which gives latest file uploads, but it is not showning the file last file uploaded
after uploading the file then i need to refresh the page then only it is showing the latest file which i uploaded. without refreshing the page how to get the latest file that uploaded in DB From FIleUpload() method. i m writing the code as below :-

IN Page Load():-
protected void Page_Load(object sender, EventArgs e)
        if (Request.QueryString["preview"] == "1" && !string.IsNullOrEmpty(Request.QueryString["fileId"]))
            var fileId = Request.QueryString["fileId"];
            var fileContentType = (string)Session["fileContentType_" + fileId];
            var fileName = (string)Session["fileName_" + fileId];

            string ct = (string)Session["fileContentType_" + fileId];
            using (SqlConnection _con = new SqlConnection("data source=test;Initial Catalog=test;User Id=sa;Password=sql;"))
            using (SqlCommand _cmd = new SqlCommand("UploadFile", _con))
                _cmd.CommandType = CommandType.StoredProcedure;
                _cmd.Parameters.AddWithValue("@FileName", fileName);
                _cmd.Parameters.AddWithValue("@FileType", fileContentType);
                _cmd.Parameters.AddWithValue("@FileContent", (byte[])Session["fileContents_" + fileId]);
            Response.ContentType = fileContentType;
        if (!IsPostBack)
            DataTable fileList = GetFileList();
            gvFiles.DataSource = fileList;
AND GetFile List Method is :-

public static DataTable GetFileList()
        DataTable fileList = new DataTable();
        using (SqlConnection _con = new SqlConnection("data source=test;Initial Catalog=test;User Id=sa;Password=sql;"))
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = _con;
            cmd.CommandTimeout = 0;
            cmd.CommandText = "SELECT ID, FileName FROM Files";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
        return fileList;
Posted 30-Dec-12 18:31pm
Edited 30-Dec-12 18:33pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You do no ordering here at all. You should store the date they were added, and sort on that, or sort on the id, I guess. then you can find the last one in.
AshishChaudha at 31-Dec-12 1:34am +5!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


Please don't write Connection string in every function.

Please create one global class or any other way so if you change the connection then you dont need to change it in every function. Smile | :)

Now you solution :

you can you Javascript's or jquery's asynchronous request.

If you want less sever load then use caching, I want to say use one global class or table where whenever you upload a file the change the last upload date at that class or table and then show it on your page synchronously or asynchronously.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 430
1 Sergey Alexandrovich Kryukov 369
2 Dave Kreskowiak 260
3 Richard Deeming 220
4 virusstorm 194
0 OriginalGriff 1,663
1 Sergey Alexandrovich Kryukov 1,229
2 Abhinav S 701
3 Dave Kreskowiak 667
4 Suvendu Shekhar Giri 568

Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100