Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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():-
----------------
C#
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 :-

C#
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
Updated 30-Dec-12 18:33pm
v2

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[^]
 
Share this answer
 
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.
 
Share this answer
 
Comments
AshishChaudha 31-Dec-12 1:34am    
exactly...my +5!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900