点击按钮上传文件前检查日期,区域

时间:2015-11-02 09:36:59

标签: c# asp.net file-upload

我有一个场景,在上传文件之前,我想检查日期和区域是否相同然后它应该提示一条javascript消息

"具有相同日期和地区的文件已经是他们的,您要覆盖它吗?"

如果用户单击“是”,则旧文件数据应进入历史记录表,新上传的文件应进入主表。

我的表名如下: -

WMS_Future_Del_Order_Hdr ---主表

WMS_Future_Del_Order_trl ---主表

历史表

WMS_Future_Del_Order_Hdr_h - 历史记录表

WMS_Future_Del_Order_trl_h - 历史记录表

这是我的HTML: -

<td valign="middle" width="20%">
                    <input id="TxtEdate" runat="server" maxlength="20" style="width: 65%; background-repeat: no-repeat;
                        background-position: bottom right; border: solid 1px #ACACAC" type="text" />
                    <cc3:Calendar ID="CalEntryDt" runat="server" DatePickerMode="true" TextBoxId="TxtEdate"
                        DatePickerImagePath="../Images/icon2.gif" CultureName="en-GB">
                    </cc3:Calendar>
                </td>
                 <td align="left" width="10%">
                    <span class="xy8">Region :</span>
                </td>
                 <td valign="left" width="40%">
                    <asp:DropDownList ID="ddlRegion" runat="server" Style="width: 50%; background-repeat: no-repeat;
                        background-position: bottom right; border: solid 1px #ACACAC; font-family: Tahoma,Arial,Helvetica,Geneva,sans-serif">
                    </asp:DropDownList>
                </td>

另见我背后的代码: -

protected void btnSave_Click(object sender, EventArgs e)
{
    DataTable dtExcel = new DataTable();
    dtExcel.Clear();
    string StrCount = String.Empty;
    string connString = "";
    HttpPostedFile File = FileUpload1.PostedFile;
    string strFileType = Path.GetExtension(FileUpload1.FileName).ToLower();
    string path = FileUpload1.PostedFile.FileName;
    string Filename = path.Substring(path.LastIndexOf("\\") + 1, path.Length - path.LastIndexOf("\\") - 1);
    path = Server.MapPath(@"~/Excels/" + "/" + Filename.ToString());

    File.SaveAs(path);
    if (strFileType.Trim() == ".xls")
    {
        connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
    }
    else if (strFileType.Trim() == ".xlsx")
    {
        connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
    }
    string query = "SELECT * FROM [Sheet 1$]";
    OleDbConnection conn = new OleDbConnection(connString);
    conn.Close();
    if (conn.State == ConnectionState.Closed)
        conn.Open();
    OleDbCommand cmd = new OleDbCommand(query, conn);
    OleDbDataAdapter daExcel = new OleDbDataAdapter(cmd);

    daExcel.Fill(dtExcel);
    conn.Close();

    DataTable DtMain = CF.ExecuteDT("select Tran_type, Order_Date from WMS_Future_Del_Order_Hdr where Tran_type = '" + CmbTypeOfAsn.SelectedValue + "' and Order_Date = convert(datetime,'" + TxtEdate.Value + "',103)");

    StringBuilder StrPubBldg = new System.Text.StringBuilder();
    XmlWriter xw = XmlWriter.Create(StrPubBldg);

    string ExcelfileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

    xw.WriteStartElement("DocumentElement");
    {
        xw.WriteStartElement("WMS_Future_Del_Order_Hdr");
        xw.WriteElementString("Comp_mkey", "0");
        xw.WriteElementString("FDepartment_ID", "W");
        xw.WriteElementString("FModule_id", "W");
        xw.WriteElementString("Tran_type", CmbTypeOfAsn.SelectedValue);
        xw.WriteElementString("Doc_no", "1");
        xw.WriteElementString("Doc_date", System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
        xw.WriteElementString("Order_Date", TxtEdate.Value);
        xw.WriteElementString("Import_Filename", Filename);
        xw.WriteElementString("Save_File_Name_Path", Server.MapPath("~/FutureDelivery/") + ExcelfileName + ".xls");
        xw.WriteElementString("Note_Remarks", txtRemarks.Text);
        xw.WriteElementString("User_id", "1");
        xw.WriteElementString("U_DateTime", System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
        xw.WriteElementString("Delete_flag", "N");
        xw.WriteEndElement();

        string STRLine_Id = "0";
        string STRItem_Mkey = "1";
        string STRItem_Code = "2";
        string STRItem_Desc = "3";
        string STRLot_Batch_No = "4";
        string STRExp_Dt = "";
        string STRBx_Bg = "6";
        string STRNet_Wgt = "7";
        string STRQty = "8";
        string STRNo_of_Pallets = "9";
        DataTable ParaDt = CF.ExecuteDT("select * from WMS_Mapping_Para_Table");
        for (int i = 0; i < ParaDt.Rows.Count; i++)
        {
            if (ParaDt.Rows[i]["Txn_Column_Name"].ToString() == "Line_Id")
            {
                STRLine_Id = ParaDt.Rows[i]["Sheet_Int_Id"].ToString();
            }
            else if (ParaDt.Rows[i]["Txn_Column_Name"].ToString() == "Item_Mkey")
            {
                STRItem_Mkey = ParaDt.Rows[i]["Sheet_Int_Id"].ToString();
            }
            else if (ParaDt.Rows[i]["Txn_Column_Name"].ToString() == "Item_Code")
            {
                STRItem_Code = ParaDt.Rows[i]["Sheet_Int_Id"].ToString();
            }
            else if (ParaDt.Rows[i]["Txn_Column_Name"].ToString() == "Item_Desc")
            {
                STRItem_Desc = ParaDt.Rows[i]["Sheet_Int_Id"].ToString();
            }
            else if (ParaDt.Rows[i]["Txn_Column_Name"].ToString() == "Lot_Batch_No")
            {
                STRLot_Batch_No = ParaDt.Rows[i]["Sheet_Int_Id"].ToString();
            }
            else if (ParaDt.Rows[i]["Txn_Column_Name"].ToString() == "Exp_Dt")
            {
                STRExp_Dt = ParaDt.Rows[i]["Sheet_Int_Id"].ToString();
            }
            else if (ParaDt.Rows[i]["Txn_Column_Name"].ToString() == "Qty")
            {
                STRQty = ParaDt.Rows[i]["Sheet_Int_Id"].ToString();
            }
            else if (ParaDt.Rows[i]["Txn_Column_Name"].ToString() == "Bx_Bg")
            {
                STRBx_Bg = ParaDt.Rows[i]["Sheet_Int_Id"].ToString();
            }
            else if (ParaDt.Rows[i]["Txn_Column_Name"].ToString() == "Net_Wgt")
            {
                STRNet_Wgt = ParaDt.Rows[i]["Sheet_Int_Id"].ToString();
            }

        }
        if (dtExcel.Rows.Count > 0)
        {
            for (int i = 0; i < dtExcel.Rows.Count; i++)
            {
                xw.WriteStartElement("WMS_Future_Del_Order_Trl");
                xw.WriteElementString("Sr_No", (i + 1).ToString());
                xw.WriteElementString("Line_Id", dtExcel.Rows[i][Convert.ToInt32(STRLine_Id)].ToString());
                xw.WriteElementString("Item_Mkey", dtExcel.Rows[i][Convert.ToInt32(STRItem_Mkey)].ToString());
                xw.WriteElementString("Item_Code", dtExcel.Rows[i][Convert.ToInt32(STRItem_Code)].ToString());
                xw.WriteElementString("Item_Desc", dtExcel.Rows[i][Convert.ToInt32(STRItem_Desc)].ToString());
                xw.WriteElementString("Lot_Batch_No", dtExcel.Rows[i][Convert.ToInt32(STRLot_Batch_No)].ToString());
                xw.WriteElementString("Exp_Dt", Convert.ToDateTime(dtExcel.Rows[i][Convert.ToInt32(STRExp_Dt)].ToString()).ToShortDateString());
                xw.WriteElementString("Qty", dtExcel.Rows[i][Convert.ToInt32(STRQty)].ToString());
                xw.WriteElementString("Bx_Bg", dtExcel.Rows[i][Convert.ToInt32(STRBx_Bg)].ToString());
                xw.WriteElementString("Net_Wgt", dtExcel.Rows[i][Convert.ToInt32(STRNet_Wgt)].ToString());
                xw.WriteElementString("No_of_Pallets", dtExcel.Rows[i][Convert.ToInt32(STRNo_of_Pallets)].ToString());
                xw.WriteElementString("Creation_Date", System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                xw.WriteElementString("Update_Date", System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                xw.WriteElementString("Created_By", "1");
                xw.WriteElementString("Updated_By", "1");
                xw.WriteElementString("User_id", "1");
                xw.WriteElementString("U_DateTime", System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                xw.WriteEndElement();
            }
        }
        xw.Close();
        String strretmkey = InsertUpdateDelete.InsertUpdateDeleteCls.InsertUpdateDelete_sql(Hid_Mode.Value, Convert.ToInt32(Hid_MKey.Value), "WMS_Future_Del_Order_Hdr", "MKEY", "FD", StrPubBldg.ToString());
        DataTable dt = CF.ExecuteDT("Select Tran_type,DATEPART(yyyy,U_DateTime) AS U_DateTime,DATEPART(mm,U_DateTime) AS U_DateTime from WMS_Future_Del_Order_Hdr where mkey ='" + strretmkey + "'");
        StrDocNo = strretmkey + '/' + dt.Rows[0][0].ToString() + '/' + dt.Rows[0][2].ToString() + dt.Rows[0][1].ToString();

        ExcelfileName = StrDocNo.ToString().Replace("/", "_");
        string strPathval = Server.MapPath("~/FutureDelivery/") + ExcelfileName + ".xls";
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/FutureDelivery/") + ExcelfileName + ".xls");

        CF.ExecuteQuerry("update WMS_Future_Del_Order_Hdr set Doc_no ='" + StrDocNo + "', Save_File_Name_Path ='" + strPathval + "' where mkey=" + strretmkey + "");

        lblDocNo.Text = StrDocNo.ToString();
        Hid_MKey.Value = strretmkey;
    }

    ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "alert('Record saved successfully.');", true);
    FunGridBind();

    btnSave.Enabled = false;
}

1 个答案:

答案 0 :(得分:0)

我认为你可以使用存储过程。

1)首先使用select

检查表中是否存在给定日期和区域

2)如果存在则意味着调用java脚本消息。否则将数据存储到主表

3)如果他们点击yes(javascript消息)

- 选择行并将选定的值传递给历史记录表写入insert查询和故事

- 然后在主表中使用Update查询并存储新的上传文件