我正在尝试一个简单的程序来尝试ObjectDataSource但是在浏览器中,表单不断抛出异常。
这是我的代码 - >
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ODSD;
namespace ODSD.App_Code.Biz
{
public class Bal
{
public List<int?> GetTerritories()
{
try
{
using (var context = new ODSD.App_Code.Data.NorthWindDataContext())
{
var ter = (from c in context.Customers
select c.TerritoryID).Distinct();
return ter.ToList();
}
}
catch (Exception)
{
throw;
}
}
}
}
由于它在DataBinding中出错,我检查了NorthWind.dbml代码,但似乎没问题。
这是我的webconfig文件 - 。
<?xml version="1.0"?>
<configuration>
<connectionStrings>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
也许版本或httpruntime是问题? 因为在visual studio中它显示了正确的成员等,但是在浏览器中它将'c'解析为int32 ...(从最初的异常判断System.Int32没有名为TerritoryID的成员')
我搜索了很多,但是数据绑定错误的上下文似乎有很多不同(并且比这个更先进。)
编辑:
NorthWind.dbml.layout-&GT;
NorthWind.designer.cs
#pragma warning disable 1591
namespace ODSD.App_Code.Data
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="AdventureWorks2012_Data")]
public partial class NorthWindDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertCustomer(Customer instance);
partial void UpdateCustomer(Customer instance);
partial void DeleteCustomer(Customer instance);
#endregion
public NorthWindDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorks2012_DataConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
public NorthWindDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public NorthWindDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public NorthWindDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public NorthWindDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<Customer> Customers
{
get
{
return this.GetTable<Customer>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="Sales.Customer")]
public partial class Customer : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _CustomerID;
private System.Nullable<int> _PersonID;
private System.Nullable<int> _StoreID;
private System.Nullable<int> _TerritoryID;
private string _AccountNumber;
private System.Guid _rowguid;
private System.DateTime _ModifiedDate;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnCustomerIDChanging(int value);
partial void OnCustomerIDChanged();
partial void OnPersonIDChanging(System.Nullable<int> value);
partial void OnPersonIDChanged();
partial void OnStoreIDChanging(System.Nullable<int> value);
partial void OnStoreIDChanged();
partial void OnTerritoryIDChanging(System.Nullable<int> value);
partial void OnTerritoryIDChanged();
partial void OnAccountNumberChanging(string value);
partial void OnAccountNumberChanged();
partial void OnrowguidChanging(System.Guid value);
partial void OnrowguidChanged();
partial void OnModifiedDateChanging(System.DateTime value);
partial void OnModifiedDateChanged();
#endregion
public Customer()
{
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CustomerID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int CustomerID
{
get
{
return this._CustomerID;
}
set
{
if ((this._CustomerID != value))
{
this.OnCustomerIDChanging(value);
this.SendPropertyChanging();
this._CustomerID = value;
this.SendPropertyChanged("CustomerID");
this.OnCustomerIDChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_PersonID", DbType="Int")]
public System.Nullable<int> PersonID
{
get
{
return this._PersonID;
}
set
{
if ((this._PersonID != value))
{
this.OnPersonIDChanging(value);
this.SendPropertyChanging();
this._PersonID = value;
this.SendPropertyChanged("PersonID");
this.OnPersonIDChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_StoreID", DbType="Int")]
public System.Nullable<int> StoreID
{
get
{
return this._StoreID;
}
set
{
if ((this._StoreID != value))
{
this.OnStoreIDChanging(value);
this.SendPropertyChanging();
this._StoreID = value;
this.SendPropertyChanged("StoreID");
this.OnStoreIDChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_TerritoryID", DbType="Int")]
public System.Nullable<int> TerritoryID
{
get
{
return this._TerritoryID;
}
set
{
if ((this._TerritoryID != value))
{
this.OnTerritoryIDChanging(value);
this.SendPropertyChanging();
this._TerritoryID = value;
this.SendPropertyChanged("TerritoryID");
this.OnTerritoryIDChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_AccountNumber", AutoSync=AutoSync.Always, DbType="VarChar(10) NOT NULL", CanBeNull=false, IsDbGenerated=true, UpdateCheck=UpdateCheck.Never)]
public string AccountNumber
{
get
{
return this._AccountNumber;
}
set
{
if ((this._AccountNumber != value))
{
this.OnAccountNumberChanging(value);
this.SendPropertyChanging();
this._AccountNumber = value;
this.SendPropertyChanged("AccountNumber");
this.OnAccountNumberChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_rowguid", DbType="UniqueIdentifier NOT NULL")]
public System.Guid rowguid
{
get
{
return this._rowguid;
}
set
{
if ((this._rowguid != value))
{
this.OnrowguidChanging(value);
this.SendPropertyChanging();
this._rowguid = value;
this.SendPropertyChanged("rowguid");
this.OnrowguidChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ModifiedDate", DbType="DateTime NOT NULL")]
public System.DateTime ModifiedDate
{
get
{
return this._ModifiedDate;
}
set
{
if ((this._ModifiedDate != value))
{
this.OnModifiedDateChanging(value);
this.SendPropertyChanging();
this._ModifiedDate = value;
this.SendPropertyChanged("ModifiedDate");
this.OnModifiedDateChanged();
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
#pragma warning restore 1591
http://s1244.photobucket.com/user/Aearombereth/media/Capture.png.html
编辑:完成错误消息
DataBinding: 'System.Int32' does not contain a property with the name 'HasValue'.
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.Web.HttpException: DataBinding: 'System.Int32' does not contain a property with the name 'HasValue'.
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:
[HttpException (0x80004005): DataBinding: 'System.Int32' does not contain a property with the name 'HasValue'.]
System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +9805821
System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format) +8
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +9754705
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +113
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105
System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +23
System.Web.UI.Control.PreRenderRecursiveInternal() +83
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446
用作DropDownList的数据源
http://s1244.photobucket.com/user/Aearombereth/media/Capture2.png.html
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo1.aspx.cs" Inherits="ODSD.Demo1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:DropDownList ID="TerritoryDD" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="ObjectDataSource1" DataValueField="HasValue" DataTextField="HasValue" >
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetTerritories" TypeName="ODSD.App_Code.Biz.Bal" ></asp:ObjectDataSource>
<p>
</p>
<p>
</p>
<asp:GridView ID="Customers" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<p>
</p>
<asp:DetailsView ID="CustomerDetails" runat="server" AutoGenerateRows="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Height="50px" Width="125px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<EditRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
</asp:DetailsView>
</form>