Wednesday 15 June 2011

c# - DataRow - 'Object reference not set to an instance of an object' -


Two forms and different classes (datacons) for data access in my project. The Form 2 button opens Form 2 and calls the GetData method to call it (child) form from the Datacon class, to populate the Form 2 text box with a name. There is a button to retrieve the next record in Form 2, which is calling the Recording Recording method in the Datacon class. The Navigator method is called from within GetData to populate the textbox in Form 2, when it loads, but also Form2รข ???? S is also asked to retrieve the next record from the next event. When NavigateRecords are called from the Form2 Next button event, the error is not set for an instance of the object reference object "????" on this line: myDR = myDS.Tables ["People"]. Rows [Increment]; Navigational Definitions Why does this not happen when the same line executes on the Form2 load event? If I warn of DataRow 'System.Data.DataRow (System.Data.DataRowBuilder) is inaccessible due to security level of the warning. < P> Datacon class:

  public class datacon {Private Dataset Meridas; Private Datarov myDR; Private Inds Maxrows = 0; Private Ent Engrment = 0; Name of Private String; Public Ent Maxro {Found {Return Maxro;}} Public Int Increment {Return Salary {{; Receive;} Set {Increment = value;}} Public string name {get {return name;}} Public Zero GetData () {// class variable string connection string = @ "provider = Microsoft.ACE.OLEDB.12.0; The use of everything 'created' in the use of data source = C: \ Temp \ test \ TestDatabase.accdb "; // is scope / destroy at the end of the block (OleDbConnection myConn = New OleDbConnection (connectionString)) Using {string SQL = "SELECT * Test from"; OleDbDataAdapter myDA = New OleDbDataAdapter (SQL, myConn); IDS = New Dataset (); {MyConn.Open (); Try MyDA.Fill (IDS, "People "); NavigateRecords (); // NewFormMyMerox = myDS.Tables [" People "] are asked to populate textboxes. Rows.Count;} Hold (A) Exclamation: {message box} Show (ex.Message);} {myConn.Close ();}}} Public Zero Navigator () {try {myDR = myDS.Tables ["People"]. Rows [increase] ; // Exception name = myDR.ItemArray.GetValue (1) .ostring ();} hold (exception pre) {message box. Show (ex.Message);} Finally {}}}}   

Form 2 loads and Next_click events:

  Private zeros fmNewForm_Load (Object Sender, EventArgs e) {DataConn theConn = New DataCon (); TheConn.GetData (); TxtName.Text = theConn.Name; } Private Zero btnNext_Click (Object Sender, EventArgs e) {DataConn theConn = New DataCon (); If (theConn.Increment! = TheCon.MaxRows - 1) {theCon.Increment ++; TheConn.NavigateRecords (); FillTextBox (); }   

Edit: I have created a new project and have just included all within the Form Class. Datarow is initially null and when NavigateRecords method is set to the system.Data.DataRow is set and it remains open until the form is open. When the door is again called from the next event of Form 2C in my original project, it gets nullified and the exception is thrown. Why does it matter that it is null when it was null to start?

Try it out:

  DataConn theCon; Private Zero fmNewForm_Load (Object Sender, EventArgs e) {theConn = New DataCon (); TheConn.GetData (); TxtName.Text = theConn.Name; } Private Zero btnNext_Click (Object Sender, EventArgs e) {If (theConn.Increment! = TheCon.MaxRows - 1) {theCon.Increment ++; TheConn.NavigateRecords (); FillTextBox (); }}    

No comments:

Post a Comment