1

I have a program that has a main form with a DevExpress XtraGrid control. It has many rows with data from my DB. I have an Edit form and an edit button on the main form to edit the selected row. I was able to pull the info for the selected object into the Edit form fine, but for some reason I am having trouble doing it again when I run the UPDATE command. I'm referring to the selected row on my main form as gridView1.GetFocusedRow() and this worked perfectly for my showAttributes method, but no longer works.

My code follows. It is returning an exception because 'call' is null. Just to note a few things: I've tried doing main.gridView1.Focus() and main.gridView1.FocusRowHandle(0) if I'm just editing the first row - neither fixed the problem. This seems to tell me that the row is still focused correctly but the reference was lost somehow.

In Main.cs

    private void btnEdit_Click(object sender, EventArgs e)
    {
        //This is the key, that lets you access an attribute of the selected row. 
        Object obj = gridView1.GetFocusedRow();

        //1) Show NewEdit Form
        Edit edit = new Edit();
        edit.Show();

        //2) Display properties of this object from DB
        edit.showAttributes(obj);
    }

In Edit.cs:

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        Main main = new Main();
        Object obj = main.gridView1.GetFocusedRow();
        try
        {
            performUpdate(obj);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
    }

    public void performUpdate(Object call1)
    {
        Main main = new Main();
        CallLog call = new CallLog();
        call = call1 as CallLog;

        executeSQLUpdate(call.Oid);
        main.xpServerCollectionSource1.Reload();
    }

Here is the showAttributes code - does the same thing but works

    public void showAttributes(Object call1)
    {
        try
        {
            Main main = new Main();
            CallLog call = new CallLog();
            call = call1 as CallLog;

            txtCompany.Text = call.CompanyName;
            txtFirst.Text = call.FirstName;
            txtMiddle.Text = call.MiddleName;
            txtLast.Text = call.LastName;
            txtPhone.Text = call.PhoneNumber;
            txtFax.Text = call.Fax;
            txtEmail.Text = call.Email;
            txtAttention.Text = call.Attention;
            txtCareOf.Text = call.CareOf;
            txtAddress1.Text = call.Address1;
            txtAddress2.Text = call.Address2;
            txtCity.Text = call.City;
            txtState.Text = call.State;
            txtZip.Text = call.ZipCode;
            txtProvince.Text = call.Province;
            txtCountry.Text = call.Country;
            txtMessage.Text = call.Message;
            txtResponse.Text = call.Response;

            if (call.VIP == 1) { chkVIP.Checked = true; } else { chkVIP.Checked = false; }
            if (call.ThreatCall == 1) { chkThreat.Checked = true; } else { chkThreat.Checked = false; }
            if (call.FollowUp == 1) { chkFollowUp.Checked = true; } else { chkFollowUp.Checked = false; }
            if (call.EscalationRequired == 1) { chkEscalation.Checked = true; } else { chkEscalation.Checked = false; }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
            return;
        }
    }

Also... I have tried doing this several other ways, without parameters, in different locations, etc. The problem is main.gridView1.GetFocusedRow() returns null. Also, running it as a series of methods from the main form does not work either (gridView1.GetFocusedRow() is also null).

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Paul
  • 1,375
  • 2
  • 21
  • 55
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Apr 10 '12 at 20:21

1 Answers1

1

In Edit.cs you do

    Main main = new Main(); 
    Object obj = main.gridView1.GetFocusedRow(); 

this will create a new Main without displaying it and then try to get an obj from a gridView obviously emtpy. The same error is repeated in all the code shown.
Also, it's very confused the creation and use of CallLog instances.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • This seems to be the problem. I am creating a new form on that first line which doesn't contain anything. However I can't pass Main into the constructor for Main... Can you reference an article with a solution? I tried Googling "c# preserve main form", not many results... CallLog is a class with attributes. I use `as CallLog;` because GetFocusedRow() method returns an object, and I have to cast it to a CallLog object. – Paul Apr 10 '12 at 23:41
  • 1
    If this is the main form of your application and you know it for sure that only one instance of the main form can be instantiated within the application life-time, I suggest that you add a static public property in the Program class, wich return a main form instance that you show via the Application.Run method. This way, you will be able to access the main form instance at any time using this static property. – Uranus Apr 12 '12 at 02:36
  • Can't make a public static Main instance in Program.cs before `Application.SetCompatibleTextRenderingDefault(false);` is run; it returns an exception. If you put it after, but before App.run, can't make it public and thus useless. – Paul Apr 12 '12 at 16:12
  • Ok, I declared it before the line and instantiated it after, I didn't get the exception but `main.gridView1.GetFocusedRow()` is still null in Edit.cs which is the root of the problem. – Paul Apr 12 '12 at 16:18
  • Umm Nevermind last comment it did work. I got the object fine when I called Program.main from the edit class. My problem now is that my code is very badly structured with forms and such so I may have to start all over... oh well! – Paul Apr 12 '12 at 16:34