簡體   English   中英

在asp.net中編輯表單時,從SQL數據庫中檢索保存的文件

[英]Retrieved saved file from SQL database when editing a form in asp.net

我有一個表單,用戶可以在其中捕獲有關發票的信息。 作為該表格的一部分,用戶可以上載該發票的pdf文件副本並將其保存到數據庫中。

我正在使用相同的表單來保存新發票並編輯已捕獲的發票。

我的問題是,當用戶編輯一個已經存在的發票並保存其更改時,pdf文件發票會被覆蓋為空白。

我不確定如何解決此問題並檢索已保存的pdf文件發票,以便在用戶保存更改時pdf文件發票不會被空白覆蓋。

這是我到目前為止的內容:

public void PopulateForm()
{

    SqlConnection con = new SqlConnection(
    WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
    con.Open();
    SqlDataReader myReader = null;
    SqlCommand cmd = new SqlCommand("sp_displayVsoftInvoice", con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("VSoftInvoiceID", txtID.Text);
    myReader = cmd.ExecuteReader();

    while (myReader.Read())
    {
        txtInvoiceNo.Text = (myReader["InvoiceNo"].ToString());
        ddlInvoiceType.SelectedValue = (myReader["EventTypeID"].ToString());
        txtDateSent.Text = (myReader["DateSent"].ToString());
        txtDatePaid.Text = (myReader["DatePaid"].ToString());
        string InvSent = (myReader["InvoiceSent"].ToString());
        string InvPaid = (myReader["InvoicePaid"].ToString());

        if (InvSent == "True")
        {
            cbxInvSent.Checked = true;
        }
        if (InvPaid == "True")
        {
            cbxInvPaid.Checked = true;
        }

    }
    con.Close();
}


protected void btnSaveInvoice_Click(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string contentType = FileUpload1.PostedFile.ContentType;
        using (Stream fs = FileUpload1.PostedFile.InputStream)
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                byte[] bytes = br.ReadBytes((Int32)fs.Length);
                SqlConnection con = new SqlConnection(
                WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
                con.Open();
                SqlCommand cmd = new SqlCommand("sp_insertUpdateVsoftInvoice", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("VSoftInvoiceID", txtID.Text);
                cmd.Parameters.AddWithValue("CenterID", ddlCenter.SelectedItem.Value);
                cmd.Parameters.AddWithValue("InvoiceTypeID", ddlInvoiceType.SelectedItem.Value);
                cmd.Parameters.AddWithValue("InvoiceNo", txtInvoiceNo.Text);
                cmd.Parameters.AddWithValue("InvoiceSent", this.cbxInvSent.Checked ? "1" : "0");
                cmd.Parameters.AddWithValue("InvoicePaid", this.cbxInvPaid.Checked ? "1" : "0");
                cmd.Parameters.AddWithValue("DateSent", txtDateSent.Text);
                cmd.Parameters.AddWithValue("DatePaid", txtDatePaid.Text);
                cmd.Parameters.AddWithValue("InvoiceName", filename);
                cmd.Parameters.AddWithValue("ContentType", contentType);
                cmd.Parameters.AddWithValue("Data", bytes);
                cmd.Parameters.AddWithValue("ModifiedBy", txtUser.Text);
                cmd.Parameters.AddWithValue("DateModified", DateTime.Now);

                cmd.ExecuteNonQuery();
                con.Close();
                Response.Redirect("VsoftInvoice.aspx");
            }
        }
    }

請告訴我如何解決此問題的建議,以使我已經保存的pdf發票不會被空白覆蓋。 提前致謝!

我們需要澄清兩件事之一:1-是在不上傳文檔的情況下進行編輯嗎?如果是,那么我認為您僅在文件上傳后才需要保存文件(修改btnSaveInvoice_Click方法)2-如果還有其他問題然后再分享代碼祝您好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM