2010-11-07 56 views
1
public void Add(IDataProvider provider){ 


    var key=KeyValue(); 
    if(key==null){ 
     var newKey=_repo.Add(this,provider); 
     this.SetKeyValue(newKey); 
    }else{ 
     _repo.Add(this,provider); //NullReferenceException was unhandled by user code 
    } 
    SetIsNew(false); 
    OnSaved(); 
} 

'' 包含亞音速3.0.0.4的ActiveRecord模板MySQL錯誤上插入新記錄

this 
{Geocine} 
    _age: null 
    _birthday: null 
    _church_id: null 
    _db: {Seminary.Data.SeminaryDB} 
    _dirtyColumns: Count = 0 
    _finalgrade: null 
    _finalgrade_equivalent: null 
    _first_name: "Geocine" 
    _gender: null 
    _isLoaded: false 
    _isNew: true 
    _last_name: "Cruz" 
    _level: null 
    _middle_name: "Reilly" 
    _repo: {SubSonic.Repository.SubSonicRepository<Seminary.Data.student>} 
    _semester_enrolled: null 
    _seminary_id: null 
    _student_id: 0 
    _year_enrolled: null 
    age: null 
    birthday: null 
    church_id: null 
    Columns: Count = 14 
    finalgrade: null 
    finalgrade_equivalent: null 
    first_name: "Geocine" 
    gender: null 
    last_name: "Cruz" 
    level: null 
    middle_name: "Reilly" 
    semester_enrolled: null 
    seminary_id: null 
    student_id: 0 
    tbl: {student} 
    TestMode: false 
    year_enrolled: null 

提供商包含

provider 
{SubSonic.DataProviders.DbDataProvider} 
    [SubSonic.DataProviders.DbDataProvider]: {SubSonic.DataProviders.DbDataProvider} 
    Client: MySqlClient 
    ConnectionString: "Data Source=localhost;Database=school;User Id=root;Password=123456;Port=3306;" 
    CurrentSharedConnection: null 
    DbDataProviderName: "MySql.Data.MySqlClient" 
    Factory: {MySql.Data.MySqlClient.MySqlClientFactory} 
    Log: null 
    Name: "MySql.Data.MySqlClient" 
    ParameterPrefix: "@" 
    Schema: {SubSonic.Schema.DatabaseSchema} 
    SchemaGenerator: {SubSonic.SqlGeneration.Schema.MySqlSchema} 

這裏是堆棧跟蹤

[NullReferenceException: Object reference not set to an instance of an object.] 
    MySql.Data.MySqlClient.MySqlConnection.get_ServerThread() +6 
    MySql.Data.MySqlClient.MySqlConnection.Abort() +54 
    MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) +839 
    MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +4 
    System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10 
    SubSonic.DataProviders.DbDataProvider.ExecuteReader(QueryCommand qry) +175 
    SubSonic.Repository.SubSonicRepository`1.Add(T item, IDataProvider provider) +165 
    Seminary.Data.student.Add(IDataProvider provider) in C:\Users\SomeUser\Documents\Visual Studio 2008\Projects\Seminary\ActiveRecord\ActiveRecord.cs:2490 
    Seminary.Data.student.Save(IDataProvider provider) in C:\Users\SomeUser\Documents\Visual Studio 2008\Projects\Seminary\ActiveRecord\ActiveRecord.cs:2505 
    Seminary.Data.student.Save() in C:\Users\SomeUser\Documents\Visual Studio 2008\Projects\Seminary\ActiveRecord\ActiveRecord.cs:2499 
    Seminary.Default.btnAdd_Click(Object sender, EventArgs e) in C:\Users\SomeUser\Documents\Visual Studio 2008\Projects\Seminary\Default.aspx.cs:34 
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 
    System.Web.UI.WebControls.Button.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) +36 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 

這裏是一個叫錯誤代碼:

student studentObject = new student(); 
    studentObject.first_name = txtFirstName.Text; 
    studentObject.middle_name = txtMiddleName.Text; 
    studentObject.last_name = txtLastName.Text; 
    studentObject.Save(); 

回答

1

這是一個有點棘手。這個例外發生在當前版本的mysql.connector.net上。

此時通常會拋出一個異常,但連接器在它可以拋出實際異常之前似乎本身有一個NullReferenceException。

「真實」的例外可能是從什麼「重複鍵...」來「現場XYZ沒有默認值」或「你在你的SQL語法附近有一個錯誤......」

我還沒有時間弄清楚爲什麼會發生這種情況(調試Connector.Net或嘗試更新的),但同時我建議你像我一樣做以下事情:

下載unix tools for windows :http://unxutils.sourceforge.net/並將它們放在路徑中(我們需要尾部命令) 配置mysql將query.log寫入c:\ temp

[mysqld] 
log=c:/temp/query.log 

啓動控制檯,然後輸入

tail -f c:\temp\query.log 

現在你看到每一個被對本地數據庫執行,並聲明,如果你得到了NullReferenceException異常複製的最後一條語句形成控制檯窗口的MySQL查詢瀏覽器實例,並執行它在那裏。

希望有幫助,我會更新這篇文章,如果我想出如何解決這個問題。

+0

嗨,謝謝你給我的希望。 http://unxutils.sourceforge.net/UnxUtils.zip - >這似乎是一個死鏈接:(在這裏找到它,http://sourceforge.net/projects/unxutils/files/unxutils/current/UnxUtils。 zip/download會嘗試安裝它 – 2010-11-10 15:38:39

+0

那麼,作爲一種替代方法,您可以在您最喜歡的文本瀏覽器中打開query.log,但由於文件在我的開發機器上快速增長到1GB以上(因爲每一個查詢都被記錄),我更喜歡'tail -f'方法,因爲我立即看到新的查詢。Btw。我不會建議在生產服務器上激活它;) – 2010-11-10 16:10:53

+0

我正在使用mysql gui工具來查看一般查詢日誌,我有一列是空的那需要有價值。 :) – 2010-11-10 16:15:38