如何获取值(draft.DraftId = new int();)并在变量中使用

时间:2013-01-01 13:25:15

标签: asp.net linq

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

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

它是null,而我在顶部使用_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,

Aspx.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();

}
}

1 个答案:

答案 0 :(得分:0)

快速查看来源,我建议重新安排SelectedIndexChanged方法,以便在<{em> SubmitChanges之后分配ID 。只有在保存之后,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;
    }
}

另外,作为旁注,我会考虑a)某些错误处理在上述代码段中是否有用,以及b)是否找不到更好的解决方案来替换用于存储的全局变量{{1 }}

HTH。