I have a code that upload files by an fileuploader, as well as a InsertCommand which inserts a data, belonging to that file.
That works fine, but if the string "maxId" has no result, he runs the button-code twice, upload the file twice and finally insert the data twice.
Any Ideas? See my following code:
if (Bericht_Hochladen.HasFile)
{
SqlCommand commandMaxActionDocID = new SqlCommand("SELECT MAX(actiondoc_id) FROM tbl_action WHERE SUBSTRING(actiondoc_nr, 2, 7) ='" + newNumber.Substring(newNumber.Length - 7) + "'", conn);
string maxId = commandMaxActionDocID.ExecuteScalar().ToString();
if (maxId == "")
{
neue_nr = newNumber.ToString() + "_" + DateTime.Now.ToString("yyyy-MM-dd", ci) + "_" + docart + "_DOC001";
}
else
{
SqlCommand commandMaxActionDocNR = new SqlCommand("SELECT actiondoc_nr FROM tbl_action_1100 WHERE actiondoc_id = @maxId", conn);
commandMaxActionDocNR.Parameters.AddWithValue("@maxId", maxId.ToString());
string NrMitT = commandMaxActionDocNR.ExecuteScalar().ToString();
string count_str = NrMitT.Substring(NrMitT.Length - 3, 3);
int count_int = Int32.Parse(count_str);
count_int = count_int + 1;
count_str = count_int.ToString("000");
neue_nr = newNumber.ToString() + "_" + DateTime.Now.ToString("yyyy-MM-dd", ci) + "_" + docart + "_DOC" + count_str;
}
string dateiendung = Path.GetExtension(Bericht_Hochladen.FileName);
string todaydate = DateTime.Today.ToString("yyyy-MM-dd");
string dokumentname = neue_nr;
#region [Datenupload WebDAV]
Bericht_Hochladen.PostedFile.SaveAs(HttpContext.Current.Server.MapPath("~/TemporaryUploadedFiles/") + dokumentname + dateiendung);
SqlCommand commandVerweis = new SqlCommand("SELECT pfad_adresse FROM tbl_sys_pfad WHERE pfad_nr = 102", conn);
string verweis = (string)commandVerweis.ExecuteScalar();
//temporäreres Speichern auf dem WebServer
FileStream fstream = new FileStream(HttpContext.Current.Server.MapPath("~/TemporaryUploadedFiles/") + dokumentname + dateiendung, FileMode.OpenOrCreate, FileAccess.Read);
HttpWebRequest request_folder = (HttpWebRequest)WebRequest.Create(filepath);
request_folder.Credentials = new NetworkCredential(szUsername, szPassword);
try
{
// Specify the MKCOL method.
request_folder.Method = "MKCOL";
HttpWebResponse response_folder = (HttpWebResponse)request_folder.GetResponse();
Response.Close();
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(filepath + dokumentname + dateiendung);
request.Credentials = new NetworkCredential(szUsername, szPassword);
try
{
request.Method = @"PUT";
request.ContentLength = fstream.Length;
request.AllowWriteStreamBuffering = true;
request.SendChunked = false;
request.KeepAlive = false;
Stream request_stream = request.GetRequestStream();
byte[] indata = new byte[1024];
int bytes_read = fstream.Read(indata, 0, indata.Length);
while (bytes_read > 0)
{
request_stream.Write(indata, 0, indata.Length);
bytes_read = fstream.Read(indata, 0, indata.Length);
}
fstream.Close();
request_stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created)
{
//MessageBox.Show("Couldn't upload file");
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
string del_URL = HttpContext.Current.Server.MapPath("~/TemporaryUploadedFiles/") + dokumentname + dateiendung;
File.Delete(del_URL);
#endregion
SqlCommand commandDocUpload = new SqlCommand("INSERT INTO tbl_action(actiondoc_action_id, actiondoc_nr, actiondoc_art, actiondoc_pfad, actiondoc_datum, actiondoc_endung, actiondoc_bem, sys_crt_user, sys_crt_timestamp, sys_deleted) VALUES (@actiondoc_action_id, @actiondoc_nr, @actiondoc_art, @actiondoc_pfad, @actiondoc_datum, @actiondoc_endung, @actiondoc_bem, @sys_crt_user, @sys_crt_timestamp, @sys_deleted)", conn);
commandDocUpload.Parameters.Add(new SqlParameter("@actiondoc_action_id", (string)Session["action_id"].ToString()));
commandDocUpload.Parameters.Add(new SqlParameter("@actiondoc_nr", neue_nr.ToString()));
commandDocUpload.Parameters.Add(new SqlParameter("@actiondoc_art", Session["dokument_art_volltext"].ToString()));
commandDocUpload.Parameters.Add(new SqlParameter("@actiondoc_pfad", neue_nr.ToString() + dateiendung.ToString()));
commandDocUpload.Parameters.Add(new SqlParameter("@actiondoc_datum", DateTime.Now.ToString("yyyy-MM-dd", ci)));
commandDocUpload.Parameters.Add(new SqlParameter("@actiondoc_endung", dateiendung.ToString()));
commandDocUpload.Parameters.Add(new SqlParameter("@actiondoc_bem", txtBeschreibungDoc.Text));
commandDocUpload.Parameters.Add(new SqlParameter("@sys_crt_user", (string)Session["login"].ToString()));
commandDocUpload.Parameters.Add(new SqlParameter("@sys_crt_timestamp", DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss", ci)));
commandDocUpload.Parameters.Add(new SqlParameter("@sys_deleted", "0"));
commandDocUpload.ExecuteNonQuery();
conn.Close();
if (IsPostBack)
{
//Do Something
return;
}
}