Subsonic 3.0.0.4 ActiveRecord Template for MySQL error on inserting new record
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();
}
'this' contains
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 contains
开发者_C百科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}
Here is the stacktrace
[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
Here is the code that called the error:
student studentObject = new student();
studentObject.first_name = txtFirstName.Text;
studentObject.middle_name = txtMiddleName.Text;
studentObject.last_name = txtLastName.Text;
studentObject.Save();
That is a bit tricky. This exceptions happens with the current version of mysql.connector.net.
Normally an exception would be trown at this point, but the connector seems to have an NullReferenceException itself before it can throw the actual exception.
The "real" exception could be anything from "DUPLICATE KEY ..." to "Field XYZ does not have a default value" or "You have an error in your SQL Syntax near ..."
I haven't had time yet to figure out why this happens (debug the Connector.Net or try a newer one) but in the mean time I would suggest you do the following as I do:
Download unix tools for windows: http://unxutils.sourceforge.net/ and put them in the path (we need the tail command) Configure mysql to write a query.log to c:\temp
[mysqld]
log=c:/temp/query.log
Start a console and type
tail -f c:\temp\query.log
Now you see every statement that is executed against your local database and if you get the NullReferenceException copy the last statement form the console window to a mysql query browser instance and execute it there.
Hope that helps, I will update this post if I figured out how to fix this issue.
精彩评论