0

在我的 aspx 页面中,当我选择值“草稿文档”时,我有一个下拉列表,它会变成新的DraftID(身份)。在此下拉列表下方,我有上传控件,并且在我附加时显示文件的 gridview。我已使用以下查询来查询 gridview 的数据源,但值有问题(_DraftId)

var query = from at in _DataContext.tblAttachments
                where at.DraftID == _DraftId
                select at;

它是空的,而我在顶部使用过_DraftId = (int)draft.DraftId;。实际上我不知道如何获取值draft.DraftId = new int();并在变量中使用它并在其他代码位置使用。

Tbldraft
DraftID BIGINT IDENTITY(1,1) PRIMARY KEY
FromEmailID 

Tblattachement:
 AttachmentID BIGINT  IDENTITY(1,1) PRIMARY KEY,
Draftid BIGINT NOT NULL REFERENCES tblDraft(DraftID),
FileName varchar(40)  NOT NULL,

ASP.cs

  public partial class Draft : System.Web.UI.Page
  {
  private EDMSDataContext _DataContext;
  private int _DraftId;
  private string _filePath;
  private string _filename;
  private string _filename_Ext;

protected void Page_Load(object sender, EventArgs e)
{
}

protected void ddlDraft_SelectedIndexChanged(object sender, EventArgs e)
{
    _DataContext = new EDMSDataContext();
    if (ddlDraft.SelectedValue == "Draft Document")
    {

        tblDraft draft = new tblDraft();
        draft.DraftId = new int();
        _DraftId = (int)draft.DraftId;
        draft.FromEmailId = (Guid)Membership.GetUser().ProviderUserKey;

        _DataContext.tblDrafts.InsertOnSubmit(draft);
        _DataContext.SubmitChanges();
    }
}

protected void btnUpload_Click(object sender, EventArgs e)
{
    string filePath = FileUpload1.PostedFile.FileName;
    string fullAddress = Path.GetFullPath(filePath);
    _filePath = fullAddress;

    string filename = Path.GetFileName(filePath);

    _filename = filename.Split('.')[0];
    _filename_Ext = filename;

    string ext = Path.GetExtension(filename);

    string contenttype = String.Empty;


    //Set the contenttype based on File Extension

    switch (ext)
    {

        case ".doc":

            contenttype = "application/vnd.ms-word";

            break;

        case ".docx":

            contenttype = "application/vnd.ms-word";

            break;

        case ".xls":

            contenttype = "application/vnd.ms-excel";

            break;

        case ".xlsx":

            contenttype = "application/vnd.ms-excel";

            break;

        case ".jpg":

            contenttype = "image/jpg";

            break;

        case ".png":

            contenttype = "image/png";

            break;

        case ".gif":

            contenttype = "image/gif";

            break;

        case ".pdf":

            contenttype = "application/pdf";

            break;

    }

   // if (contenttype != String.Empty)
  //  {


        Stream fs = FileUpload1.PostedFile.InputStream;

        BinaryReader br = new BinaryReader(fs);

        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
        //insert the file into database
 //   }

    tblAttachment attach = new tblAttachment();
    attach.DraftID = _DraftId;
    attach.FileName = filename;
    attach.ContentType = contenttype;
    attach.Data = bytes;
    attach.Exten = ext;
    _DataContext.tblAttachments.InsertOnSubmit(attach);
    _DataContext.SubmitChanges();
    //doctranscon.TransmitToConid = Convert.ToInt32(ddlTransmittaltoCon.SelectedValue);
    var query = from at in _DataContext.tblAttachments
                where at.DraftID == _DraftId
                select at;
    GridViewEfile.DataSource = query;
    GridViewEfile.DataBind();

}
}
4

1 回答 1

0

快速查看源代码,我建议重新SelectedIndexChanged安排. SubmitChanges只有在保存之后,id 才会可用;在此之前,它不存在。

protected void ddlDraft_SelectedIndexChanged(object sender, EventArgs e)
{
    _DataContext = new EDMSDataContext();
    if (ddlDraft.SelectedValue == "Draft Document")
    {

        tblDraft draft = new tblDraft();
        draft.FromEmailId = (Guid)Membership.GetUser().ProviderUserKey;
        _DataContext.tblDrafts.InsertOnSubmit(draft);
        _DataContext.SubmitChanges();
        _DraftId = (int)draft.DraftId;
    }
}

Also, as a side-note, I'd consider whether a) some error handling might be useful in the above snippet, and b) whether a better solution might not be found to replace the global variable for storing _DraftId.

HTH.

于 2013-01-01T19:45:09.847 回答