我是Entity Framework和ObjectDataSource的新手。我试图让我的网格更新项目,但得到以下错误。我做错了什么?
Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] GalleryApp.Logic.FileActions.UpdateFile(GalleryItemFile newFile) +377 [TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +92 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +108 System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +487 System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method) +39 System.Web.UI.WebControls.ObjectDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +1830 System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +87 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1210 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +738 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9642610 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18056
这是我的ASPX代码。
<asp:GridView runat="server" DataSourceID="dsFiles" ID="gvFiles"
EmptyDataText="No Files Associated with this gallery item yet."
DataKeyNames="fileID,galleryItem" AutoGenerateColumns="false" CssClass="formtable" OnRowUpdating="gvFiles_RowUpdating">
<Columns>
<asp:BoundField HeaderText="filename" DataField="filename" />
<asp:TemplateField HeaderText="File Type" SortExpression="fileType">
<ItemTemplate>
<%# Eval("FileType.fileType") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="cboFileType" DataSourceID="dsFileTypes" DataTextField="fileType" DataValueField="fileTypeID" SelectedValue='<%# Eval("FileType.fileTypeID") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList runat="server" ID="cboFileType" DataSourceID="dsFileTypes" DataTextField="fileType" DataValueField="fileTypeID" SelectedValue='<%# Bind("FileType.fileTypeID") %>' />
</InsertItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Active" DataField="active" />
<asp:BoundField HeaderText="Resolution" DataField="resolution" />
<asp:CommandField ShowDeleteButton="true" ShowEditButton="true" ShowHeader="true" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource runat="server" ID="dsFiles"
TypeName="GalleryApp.Logic.FileActions"
DataObjectTypeName="GalleryApp.Models.GalleryItemFile"
SelectMethod="GetFiles"
InsertMethod="AddFile"
UpdateMethod="UpdateFile"
DeleteMethod="DeleteFile" OnUpdating="dsFiles_Updating">
<SelectParameters>
<asp:ControlParameter Name="galleryItemID" ControlID="hidGalleryItemID" Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter Name="galleryItemID" ControlID="hidGalleryItemID" Type="Int32" />
<asp:Parameter Name="fileTypeID" Type="Int32" />
<asp:Parameter Name="filename" Type="String" />
<asp:Parameter Name="active" Type="Boolean" />
<asp:CookieParameter Name="userCommonName" Type="String" />
<asp:Parameter Name="resolution" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="fileID" Type="Int32" />
<asp:ControlParameter Name="galleryItemID" ControlID="hidGalleryItemID" Type="Int32" />
<asp:Parameter Name="fileTypeID" Type="Int32" />
<asp:Parameter Name="filename" Type="String" />
<asp:Parameter Name="active" Type="Boolean" />
<asp:CookieParameter Name="userCommonName" Type="String" />
<asp:Parameter Name="resolution" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="fileID" Type="Int32" />
</DeleteParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource runat="server" ID="dsFileTypes"
TypeName="GalleryApp.Logic.FileTypeActions"
DataObjectTypeName="GalleryApp.Models.FileType"
SelectMethod="GetFileTypes">
</asp:ObjectDataSource>
FileActions是我的Logic类。我将所有方法都切换为静态,因为我在一个例子中看到了它,但它没有改变任何东西。我假设它一定是在谈论GalleryItemFile对象,它是其中一个实体。
网格加载点击编辑然后更改文件名并单击保存,我得到黄色屏幕。
编辑: 这是被调用的函数。例外情况发生在第一个“else”之后的行。
public static GalleryItemFile UpdateFile(GalleryItemFile newFile)
{
GalleryContext _db = new GalleryContext();
_db.Database.Log = HttpContext.Current.Response.Write; //Used for Tracing SQL
int saved = 0;
GalleryItemFile origFile = _db.GalleryItemFiles.FirstOrDefault(g => g.fileID == newFile.fileID);
HttpContext.Current.Response.Write(origFile.filename);
if (origFile == null)
{
//ITEM NOT FOUND
}
else
{
origFile.galleryItem.galleryItemID = newFile.galleryItem.galleryItemID;
origFile.fileType.fileTypeID = newFile.fileType.fileTypeID;
origFile.filename = newFile.filename;
origFile.active = newFile.active;
origFile.createdOn = DateTime.Now;
origFile.createdBy = newFile.createdBy;
origFile.resolution = newFile.resolution;
saved = _db.SaveChanges();
}
if (saved > 0)
{
return origFile;
}
else { return null; }
}
调试时我验证了origFile是否正确检索。它是从ObjectDataSource发送的newFile,似乎生成了空引用。
编辑: 这是GalleryItemFile的模型。
我再次是Entity Framework的新手,但我认为它会自动加载galleryItem。
namespace GalleryApp.Models
{
[Serializable]
public class GalleryItemFile
{
[Key]
public int fileID { get; set; }
public virtual GalleryItem galleryItem { get; set; }
public virtual FileType fileType { get; set; }
[MaxLength(75)] public string filename { get; set; }
public bool active { get; set; }
public DateTime? createdOn { get; set; }
[MaxLength(150)] public string createdBy { get; set; }
[MaxLength(50)] public string resolution { get; set; }
}
}
namespace GalleryApp.Models
{
[Serializable]
public class FileType
{
[Key]
public int fileTypeID { get; set; }
[MaxLength(50)] public string fileType { get; set; }
[MaxLength(15)] public string extension { get; set; }
[MaxLength(250)] public string icon { get; set; }
[MaxLength(400)] public string description { get; set; }
[MaxLength(400)] public string warnings { get; set; }
}
}
Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 160: { Line 161: origFile.galleryItem.galleryItemID = newFile.galleryItem.galleryItemID; Line 162: origFile.fileType.fileTypeID = newFile.fileType.fileTypeID; Line 163: origFile.filename = newFile.filename; Line 164: origFile.active = newFile.active; Source File: c:\Users\holcombelr\SkyDrive\Public\Projects\UHV\GalleryApp\Logic\GalleryItemFileActions.cs Line: 162 Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] GalleryApp.Logic.FileActions.UpdateFile(GalleryItemFile newFile) in c:\Users\holcombelr\SkyDrive\Public\Projects\UHV\GalleryApp\Logic\GalleryItemFileActions.cs:162
以下是更多信息。添加serializable让我更近了一步,但现在错误出现在下一行,我现在已经为所有对象添加了serializable。
编辑:有没有人对此有答案?我还有问题。答案 0 :(得分:0)
我想我明白了。显然,当您有这样的子对象时,您必须将对象包含在gridview的DataKeyNames中。