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 :-
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 THE GetFileList() mETHOD WHICH GIVES THE UPLOADED FILES :-
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;
}
nrk_hi
Member
136 Points
110 Posts
how to get latest uploaded File after uploading a file into database
Dec 31, 2012 04:31 AM|LINK
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 :-
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 THE GetFileList() mETHOD WHICH GIVES THE UPLOADED FILES :- 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; }kedarrkulkar...
All-Star
34013 Points
5468 Posts
Re: how to get latest uploaded File after uploading a file into database
Dec 31, 2012 05:10 AM|LINK
because, u r running the display file code inside !IsPostBack condition
remove this
if (!IsPostBack)
{
DataTable fileList = GetFileList();
gvFiles.DataSource = fileList;
gvFiles.DataBind();
}
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
nrk_hi
Member
136 Points
110 Posts
Re: how to get latest uploaded File after uploading a file into database
Dec 31, 2012 05:20 AM|LINK
I Removed ispostback , code is below :-
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(); // } }gaurabchatte...
Participant
1655 Points
325 Posts
Re: how to get latest uploaded File after uploading a file into database
Dec 31, 2012 05:21 AM|LINK
Yes you have fut the refresh code inside the ! postpack section.. You have two ways
1. Allways call the refresh code by removeing the <!> from IsPostBack method
2. Call the refresh method after the file uploaded.
Microsoft Technologies
kedarrkulkar...
All-Star
34013 Points
5468 Posts
Re: how to get latest uploaded File after uploading a file into database
Dec 31, 2012 05:22 AM|LINK
does it work as expected now?
KK
Please mark as Answer if post helps in resolving your issue
My Site
nrk_hi
Member
136 Points
110 Posts
Re: how to get latest uploaded File after uploading a file into database
Dec 31, 2012 05:46 AM|LINK
No After Removing the - if (!IsPostBack) also it is not working as Expected. any idea how can i achive this
kedarrkulkar...
All-Star
34013 Points
5468 Posts
Re: how to get latest uploaded File after uploading a file into database
Dec 31, 2012 06:03 AM|LINK
hummm... thats becasue u r clearing the response cache... comment these lines as well and try again... let me know if it works this time
Response.Clear();
Response.ContentType = fileContentType;
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
oned_gk
All-Star
30985 Points
6341 Posts
Re: how to get latest uploaded File after uploading a file into database
Dec 31, 2012 06:12 AM|LINK
Usualy uploading file is using button click event. After apload then recall gv databind. To make latest uploaded at top use ORDER BY ID DESC.
nrk_hi
Member
136 Points
110 Posts
Re: how to get latest uploaded File after uploading a file into database
Dec 31, 2012 07:44 AM|LINK
I Commmented these two lines of code :-
Response.Clear();
Response.ContentType = fileContentType;
Still i m facing same problem :(
kedarrkulkar...
All-Star
34013 Points
5468 Posts
Re: how to get latest uploaded File after uploading a file into database
Dec 31, 2012 08:28 AM|LINK
how r u calling this page? i mean how r u passing the querystring parameters to this page?
is it being called from some other page or on button click event from same page?
KK
Please mark as Answer if post helps in resolving your issue
My Site