Asp.net c# ทําไมเวลากด Button Event เพื่อ Post ค่าไปทำอะไรสักอย่างแล้ว มันวนกลับมาหน้าฟอร์มเดิมแต่กด Refresh F5 error
Asp.net c# ทําไมเวลากด Button Event เพื่อ Post ค่าไปทำอะไรสักอย่างแล้ว มันวนกลับมาหน้าฟอร์มเดิมแต่กด Refresh F5 error
ต้องแก้ไขอย่างไรครับCode (C#)
if (this.FileUpload.HasFile)
{
if (Path.GetExtension(FileUpload.FileName) == ".txt")
{
string folderPath = Server.MapPath("Files/");
string folderPathConvert = Server.MapPath("FilesConvert/");
string oldFilename = FileUpload.FileName;
string newFilename = "TUS" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".txt";
try
{
this.FileUpload.SaveAs(folderPath + FileUpload.FileName);
File.Move(folderPath + FileUpload.FileName, folderPath + newFilename);
using (StreamWriter sw = (!File.Exists(folderPathConvert + newFilename)) ? File.CreateText(folderPathConvert + newFilename) : new StreamWriter(folderPathConvert + newFilename))
{
sr = File.OpenText(folderPath + newFilename);
while (!sr.EndOfStream)
{
string[] dataSplit = sr.ReadLine().Split('\t');
string[] date = dataSplit[4].Split('-');
string[] time = dataSplit[5].Split(':');
int hour = Int32.Parse(time[0]);
sw.WriteLine("TUS" + dataSplit[0] + " " + date[0] + date[1] + date[2] + hour.ToString("00") + time[1] + time[2]);
}
sw.Close();
}
sql = "INSERT INTO FingerScanData (Company, OldFilename, Filename) VALUES (@Company, @OldFilename, @Filename);";
Database.cmd = new SqlCommand(sql, Database.conn);
Database.cmd.Parameters.AddWithValue("@Company", "TUS");
Database.cmd.Parameters.AddWithValue("@OldFilename", oldFilename);
Database.cmd.Parameters.AddWithValue("@Filename", newFilename);
Database.conn.Open();
Database.cmd.ExecuteNonQuery();
Database.conn.Close();
this.lblText.Text = "Upload Success";
LoadData();
}
catch (Exception ex)
{
this.lblText.Text = "Read Failed" + ex.Message;
}
}
else
{
this.lblText.Text = "File type incorrect!";
}
}
else
{
this.lblText.Text = "Select File";
}
Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>
Tag : .NET
ประวัติการแก้ไข 2017-05-16 13:08:34
Date :
2017-05-16 09:32:34
By :
kenghockey
View :
1049
Reply :
3
ต้อง error เพราะอะไรสักอย่างแหละ
session หมดอายุหรือเปล่า รีโหลดหน้าใหม่หา session ไม่เจอเด้งหน้าเหลืองมาแทน
Date :
2017-05-16 13:12:42
By :
tester
Debug เลยครับ รู้เลย
Date :
2017-05-16 13:39:00
By :
mr.win
ตอบความคิดเห็นที่ : 2 เขียนโดย : mr.win เมื่อวันที่ 2017-05-16 13:39:00
รายละเอียดของการตอบ ::
protected void Page_Load(object sender, EventArgs e)
{
//if(!Page.IsPostBack)
//{
// LoadData();
//}
LoadData();
}
ถ้า Comment จะไม่เป็นไร แต่มันจะมีปัญหาตรงที่ว่าในหน้าฟอร์มผมมี Gridview ถ้ากด Post ไป 1 ครั้ง
แล้วอยากคลิกหน้า 2 gridview มันจะวิ่งไปที่ LoadData(); อีกมันจะ Error
Code (C#)
public partial class Default : System.Web.UI.Page
{
StreamReader sr;
string sql;
protected void Page_Load(object sender, EventArgs e)
{
//if(!Page.IsPostBack)
//{
// LoadData();
//}
LoadData();
}
private void LoadData()
{
sql = "SELECT * FROM FingerScanData";
Database.cmd = new SqlCommand(sql, Database.conn);
Database.adap = new SqlDataAdapter(Database.cmd);
DataTable dt = new DataTable();
Database.conn.Open();
Database.adap.Fill(dt);
Database.conn.Close();
GvFileResult.DataSource = dt;
GvFileResult.DataBind();
}
protected void GvFileResult_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
string file = e.CommandArgument.ToString();
Response.ClearContent();
Response.Clear();
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", "attachment; filename=" + file + ";");
Response.TransmitFile(Server.MapPath("FilesConvert/") + file);
Response.Flush();
Response.End();
Response.Write("<script>");
Response.Write("alert('Success');");
Response.Write("</script>");
}
protected void GvResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GvFileResult.PageIndex = e.NewPageIndex;
GvFileResult.DataBind();
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (this.FileUpload.HasFile)
{
if (Path.GetExtension(FileUpload.FileName) == ".txt")
{
string folderPath = Server.MapPath("Files/");
string folderPathConvert = Server.MapPath("FilesConvert/");
string oldFilename = FileUpload.FileName;
string newFilename = "TUS" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".txt";
try
{
this.FileUpload.SaveAs(folderPath + FileUpload.FileName);
File.Move(folderPath + FileUpload.FileName, folderPath + newFilename);
using (StreamWriter sw = (!File.Exists(folderPathConvert + newFilename)) ? File.CreateText(folderPathConvert + newFilename) : new StreamWriter(folderPathConvert + newFilename))
{
sr = File.OpenText(folderPath + newFilename);
while (!sr.EndOfStream)
{
string[] dataSplit = sr.ReadLine().Split('\t');
string[] date = dataSplit[4].Split('-');
string[] time = dataSplit[5].Split(':');
int hour = Int32.Parse(time[0]);
sw.WriteLine("TUS" + dataSplit[0] + " " + date[0] + date[1] + date[2] + hour.ToString("00") + time[1] + time[2]);
}
sw.Close();
}
sql = "INSERT INTO FingerScanData (Company, OldFilename, Filename) VALUES (@Company, @OldFilename, @Filename);";
Database.cmd = new SqlCommand(sql, Database.conn);
Database.cmd.Parameters.AddWithValue("@Company", "TUS");
Database.cmd.Parameters.AddWithValue("@OldFilename", oldFilename);
Database.cmd.Parameters.AddWithValue("@Filename", newFilename);
Database.conn.Open();
Database.cmd.ExecuteNonQuery();
Database.conn.Close();
this.lblText.Text = "Upload Success";
LoadData();
}
catch (Exception ex)
{
this.lblText.Text = "Read Failed" + ex.Message;
}
}
else
{
this.lblText.Text = "File type incorrect!";
}
}
else
{
this.lblText.Text = "Select File";
}
}
}
}
ประวัติการแก้ไข 2017-05-16 13:59:23
Date :
2017-05-16 13:50:38
By :
kenghockey
Load balance : Server 03