Click here to Skip to main content
12,688,944 members (32,199 online)
Rate this:
 
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]);

                _con.Open();
                _cmd.ExecuteNonQuery();
                _con.Close();
            }

            Response.Clear();
            Response.ContentType = fileContentType;
          
        }

        if (!IsPostBack)
        {
            DataTable fileList = GetFileList();
            gvFiles.DataSource = fileList;
            gvFiles.DataBind();
        }

    }

--------------------------------------
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;"))
        {
            _con.Open();
            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;
            adapter.Fill(fileList);

            _con.Close();
        }

        return fileList;
    }
Posted 30-Dec-12 19:31pm
Updated 30-Dec-12 19:33pm
v2
Rate this: bad
 
good
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.
  Permalink  
Comments
AshishChaudha 31-Dec-12 1:34am
   
exactly...my +5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

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. :)


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.

asynchronously[^]
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170113.4 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2017
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