如何在不上传文件的情况下更新表单

时间:2017-07-18 08:01:19

标签: c# asp.net ado

我想更新一个带有文本框和文件的表单但是当我尝试更新时我必须更新文件我想要如果我不选择文件我可以在asp.net中更新它

 FileInfo fi = new FileInfo(FileUpload1.FileName);
 byte[] DocumentContent = FileUpload1.FileBytes;
 string name = fi.Name;
 string extn = fi.Extension;
 using (SqlConnection cn = new SqlConnection(@" server = DESKTOP-IJCGFFC ; initial catalog = gaf_application ; integrated security =true"))
  {
      SqlCommand cmd = new SqlCommand("ModifierFournisseur", cn);
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("@nom", SqlDbType.VarChar).Value = name;
      cmd.Parameters.Add("@ContentDoc", SqlDbType.VarBinary).Value = DocumentContent
      cmd.Parameters.Add("@extDoc", SqlDbType.VarChar).Value = extn;
      cmd.Parameters.Add("@Ref_fourn", SqlDbType.VarChar).Value = DropDownList1.SelectedValue;
      cmd.Parameters.Add("@Adress", SqlDbType.VarChar).Value = TextBox2.Text;
      cmd.Parameters.Add("@Article_rat", SqlDbType.VarChar).Value = TextBox3.Text;
      cmd.Parameters.Add("@Prix_achat", SqlDbType.Float).Value = TextBox4.Text;
      cmd.Parameters.Add("@date_fac", SqlDbType.Date).Value = TextBox5.Text;
      cn.Open();

      lblmsg.Visible = true;
      lblmsg.Text = "la modification bien fait!";

      cmd.ExecuteNonQuery();
      DropDownList1.SelectedIndex = 0;
      TextBox2.Text = "";
      TextBox3.Text = "";
      TextBox4.Text = "";
      TextBox5.Text = "";
  }

我可以更新文件

2 个答案:

答案 0 :(得分:1)

这是你需要做的。

string name = string.Empty;
string extn = string.Empty;

if (FileUpload1.HasFile)
{
    FileInfo fi = new FileInfo(FileUpload1.FileName);
    byte[] DocumentContent = FileUpload1.FileBytes;
    name = fi.Name;
    extn = fi.Extension;      
}

答案 1 :(得分:0)

只需在表单中添加一个复选框,然后根据复选框的Checked值上传(或不要):

<asp:CheckBox runat="server" ID="chkUpload" Text="Upload File" />
if (chkUpload.Checked)
{
    // The checkbox was checked, so upload the file
}

// Update the database regardless of whether or not the checkbox was checked.