Object Reference not set to an instance of an object VS 2008 C#

rwheeler23
rwheeler23 used Ask the Experts™
on
Why does this variable assignment keep returning the error message "Object Reference not set to an instance of an object"?

This field is on the same form attached to this code. When I type type code it it pulls it in from this portion of the project. I have even retyped in the entire project thinking perhaps the code was corrupt. I have been copying and pasting code to a dozen or so programs today and this is the only one behaving this way.
{
                txtOptType.Text = "Enter Transaction:";
            }
            catch (Exception ex)
            {
                string eMsg = "001z: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2015
Commented:
Did you initialize "txtOptType"? I assume it is a TextBox; did you add it at design time or runtime? If it is runtime allocated, make sure you at some point called:

    txtOptType = new TextBox();
Most Valuable Expert 2011
Top Expert 2015

Commented:
P.S.

It would be helpful to know which line is throwing the exception as you have 3 variable assignments demonstrated in what you posted.
rwheeler23President

Author

Commented:
I have discovered that if I try to assign a value to any of my text fields on the form it throws the exception.

txtOptType.Text = "Enter Transaction:";
There are 7 other text fields and they all throw the execption.

All I am doing is what I always do. I create the form, I drop the fields onto the form and then I assign them values.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

rwheeler23President

Author

Commented:
I added this code which indicated that this.txtOptType is null.  As the message "I am null" does not appear. It appears that somehow my text fields have become dissasociated with the form.
try
            {
                if (this.txtOptType != null)
                {
                    MessageBox.Show("I am null");
                    this.txtOptType.Text = "Enter Transaction:";
                }
            }
            catch (Exception ex)
            {
                string eMsg = "001z: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }

Open in new window

Commented:
if (this.txtOptType != null)
                {
                    MessageBox.Show("I am null");

should be MessageBox.Show("I am not null");

Nothing wrong with the codes you posted. Maybe you invalided your text boxes somewhere else.
rwheeler23President

Author

Commented:
I have been staring at this for the past 4 hours and I cannot see what is wrong. Somehow I have invalidated my text textboxes. How you do that is beyond me. This program is just copy of a template I have used dozens of times.

I am just going to recreate from strach again and hopefully won't do whatever I have done. I had tried
 txtOptType = new TextBox();

for all the text boxes which gets my code to run until the form is to appear on the screen. Then that crashes.

Commented:
The exception means "txtOptType" is null. Does it only happen when you assign a string to it? What if you try to access a member of textbox? For example, will the exception happen if you call "txtOptType.IsDisposed"?
rwheeler23President

Author

Commented:
I cannot even test that. If I put a Try-Catch around testing txtOptType.IsDisposed I get the same "Object Reference..." error message.

I have a template program that I have been copying and making minor tweaks given the program with which these programs must interface. This is the first one to give me any trouble. I know with this one I did it this way:
1) Created a brand new project
2) Added two new forms
3) Copied and pasted the code into appropriate sections
4) Then I added the text fields to the forms and matched up any events

Perhaps what I should have done was added the text fields to the forms first and then copied and pasted the code. Somehow right now there is a disconnect between the forms and the text fields. I wish there was a better way to copy VS projects so as to save all of the typing.
Commented:
may be the form is null..i can't determine it with the code you paste, but there is no evidence that it's not true...check it :)...
rwheeler23President

Author

Commented:
The form name is frmViewJobLinker. Do I do someting like if (frmViewJobLinker = Null) ?

If it is null how do I make not null?
rwheeler23President

Author

Commented:
Here is the entire code module. You will see a bunch of MessageBox 's as I tried to find where the problem was. Anytime I try write to a text box on the form is where it errors. I will test for null but if I do find it is Null, how do I make the form Not Null?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Dexterity.Bridge;
using Microsoft.Dexterity.Applications;
using Microsoft.Dexterity.Shell;
using Microsoft.Dexterity.Applications.DynamicsDictionary;

namespace PopulateJobLinkerBTE
{
    public partial class frmViewJobLinker : DexUIForm
    {
        /* Create a reference to the Bank Transaction Entry Distribution screen */
        static CmTransactionEntryForm CmTransactionEntryForm = Dynamics.Forms.CmTransactionEntry;
        static CmTransactionEntryForm.CmTransactionEntryWindow CmTransactionEntryWindow = CmTransactionEntryForm.CmTransactionEntry;

        /* Define a string variable to hold connection string */
        string ConnectionString;

        /* Set up SQL Connection Handlers */
        private System.Data.SqlClient.SqlConnection DataConnection;
        private System.Data.DataSet DataDataSet;
        private System.Data.SqlClient.SqlCommand DataCommand;
        private System.Data.SqlClient.SqlDataAdapter DataDataAdapter;
        private System.Data.DataSet TrxTypeDataSet;
        private System.Data.SqlClient.SqlCommand TrxTypeDataCommand;
        private System.Data.SqlClient.SqlDataAdapter TrxTypeDataAdapter;
        private SqlCommandBuilder _commandBuilder;

        string blank = " ";
        int zero = 0;

        /* Define variables to hold values from the document header */
        string VoucherNumber;
        string DocumentNumber;
        int DocType;
        string MasterType;
        string DistSource;
        string DocSource;
        string BachNumb;
        string BchSource;
        string MasterID;
        decimal DocAmount;
        DateTime DocDate;
        string Reference;

        /* Define variables to hold variables from the buffer */
        int SeqNumber;
        int SpecializedDistribution;
        int ControlType;
        decimal CreditAmount;
        decimal DebitAmt;
        int ActIndx;
        string TrxSource;
        string ApplyToVoucherNumber;

        /* For bank transaction, there is no distribution type */
        /* Use the following seven descriptions and pass down to SQL script */
        /* 1-Check(T), 2-Withdrawal(T),3-Increase Adjustment(T),4-Decrease Adjustment(T),5-Check(R),6-Cash(R),7-Credit Card(R) */
        int DistType;

        /* Create a stack trace flag for debugging purposes */
        static bool stackTraceWanted = true;

        public frmViewJobLinker()
        {
            MessageBox.Show("Hello World");
            /* Header Values from Purchase Order Receiving Transaction Entry Screen */
            DocumentNumber = CmTransactionEntryWindow.CmTrxNumber;
            DocType = CmTransactionEntryWindow.LocalOption;
            BachNumb = "No Batch";
            BchSource = "Bank Trx Entry";
            MasterID = CmTransactionEntryWindow.CheckbookId;
            DocAmount = CmTransactionEntryWindow.CheckbookAmount;
            DocDate = CmTransactionEntryWindow.TrxDate;
            MasterType = "BNK";
            VoucherNumber = CmTransactionEntryWindow.CmTrxNumber;
            TrxSource = "Bank Trx Entry";
            DistSource = "Bank Transaction Entry";
            DocSource = "Bank Trx";
            SpecializedDistribution = zero;
            ControlType = CmTransactionEntryWindow.LocalType;
            ApplyToVoucherNumber = blank;
            try
            {

                if (txtOptType.IsDisposed)
                {
                    MessageBox.Show("It is Disposed");
                }
            }
            catch (Exception ex)
            {
                string eMsg = "001zz: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }

            txtOptType = new TextBox();
            txtType = new TextBox();
            txtCheckbookID = new TextBox();
            txtCheckbookName = new TextBox();
            txtNumber = new TextBox();
            txtDocAmount = new TextBox();
            txtTransactionDate = new TextBox();
                                       
            this.txtOptType.Text = "Enter Transaction:";
            try
            {
                if (this.txtOptType != null)
                {
                    MessageBox.Show("I am a dummy!");
                    this.txtOptType.Text = "Enter Transaction:";
                }
            }
            catch (Exception ex)
            {
                string eMsg = "001z: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }

            MessageBox.Show("Hello World2bA");
            try
            {
                switch (CmTransactionEntryWindow.LocalOption.Value.ToString())
                {
                    case "1":
                        MessageBox.Show(CmTransactionEntryWindow.LocalType.Value.ToString());
                        MessageBox.Show("Hello World2AA");
                        txtOptType.Text = "Enter Transaction";
                        MessageBox.Show("Hello World2A");
                        switch (CmTransactionEntryWindow.LocalType.Value.ToString())
                        {
                            case "1":
                                MessageBox.Show("Hello World2B");
                                txtType.Text = "Check";
                                MessageBox.Show("Hello World2C");
                                DistType = 1;
                                break;
                            case "2":
                                txtType.Text = "Withdrawal";
                                DistType = 2;
                                break;
                            case "3":
                                txtType.Text = "Increase Adjustment";
                                DistType = 3;
                                break;
                            case "4":
                                txtType.Text = "Decrease Adjustment";
                                DistType = 4;
                                break;
                            default:
                                txtType.Text = "Unknown";
                                DistType = 0;
                                break;
                        }
                        break;
                    case "2":
                        txtOptType.Text = "Enter Receipt";
                        switch (CmTransactionEntryWindow.LocalType.Value.ToString())
                        {
                            case "1":
                                txtType.Text = "Check";
                                DistType = 5;
                                break;
                            case "2":
                                txtType.Text = "Cash";
                                DistType = 6;
                                break;
                            case "3":
                                txtType.Text = "Credit Card";
                                DistType = 7;
                                break;
                            default:
                                txtType.Text = "Unknown";
                                DistType = 0;
                                break;
                        }
                        break;
                    case "3":
                        txtOptType.Text = "Void Transaction";
                        DistType = 0;
                        break;
                    case "4":
                        txtOptType.Text = "Void Receipt";
                        txtType.Text = "";
                        DistType = 0;
                        break;
                    default:
                        txtOptType.Text = "Unknown";
                        txtType.Text = "";
                        DistType = 0;
                        break;
                }
            }
            catch (Exception ex)
            {
                string eMsg = "001A: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }
            MessageBox.Show("Hello World3");
            txtCheckbookID.Text = CmTransactionEntryWindow.CheckbookId;
            txtCheckbookName.Text = CmTransactionEntryWindow.CheckbookName;
            txtNumber.Text = CmTransactionEntryWindow.CmTrxNumber;
            txtDocAmount.Text = CmTransactionEntryWindow.CheckbookAmount.Value.ToString("c");

            /* Convert the posting date */
            txtTransactionDate.Text = CmTransactionEntryWindow.TrxDate.Value.ToString();
            try
            {

                if (txtTransactionDate.Text != "")
                {
                    txtTransactionDate.Text = Convert.ToDateTime(txtTransactionDate.Text).ToString("MM/dd/yyyy");
                }
            }
            catch (Exception ex)
            {
                string eMsg = "001: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }

            /* Delete any previous distribution records for this transaction in temp work table */
            DeleteDistSeq();
            MessageBox.Show("Hello World DeleteDistSeq");

            /* Refresh all distributions records in buffer to the temp work table*/
            UpdateDistSeq();
            MessageBox.Show("Hello World UpdateDistSeq");

            /* Delete any job linker distributions records that no longer exist */
            UpdateJobLinkerDistSeq();
            MessageBox.Show("Hello World UpdateJobLinkerDistSeq");

            /* Insert and or Update the Job Linker table with distributions in current buffer */
            UpdateJobLinker();
            MessageBox.Show("Hello World UpdateJobLinker");

            /* Display the GL Distributions from the Job Linker table per Voucher Number */
            DisplayJobLinker();
            MessageBox.Show("Hello World DisplayJobLinker");
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
            DataConnection.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _commandBuilder.GetUpdateCommand();

                DataDataAdapter.Update(DataDataSet);
            }
            catch (Exception ex)
            {
                string eMsg = "006: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }
            this.Hide();
            this.Dispose();
            DataConnection.Close();
        }

        private SqlConnection GetConnection()
        {
            TextReader tr = new StreamReader(@"C:\Program Files\Microsoft Dynamics\GP\Addins\GSE.INI");
            /* Create reader and open file containing connection string */
            try
            {
                /* Read line containing the connection string */
                ConnectionString = tr.ReadLine();
            }
            catch (Exception ex)
            {
                string eMsg = "005: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }
            finally
            {
                /* Close the stream */
                tr.Close();
            }

            DataConnection = new System.Data.SqlClient.SqlConnection(ConnectionString);
            DataConnection.Open();

            return DataConnection;
        }

        private void dgvJobLinker_DataError(object sender, DataGridViewDataErrorEventArgs anError)
        {
            /* Comment out this entire section. Use this in the future.
            DataGridViewComboBoxColumn will always throw an error, so trap it but do nothing with it
            MessageBox.Show("Error happened " + anError.Context.ToString());

            if (anError.Context == DataGridViewDataErrorContexts.Commit)
            {
                MessageBox.Show("Commit error");
            }
            if (anError.Context == DataGridViewDataErrorContexts.CurrentCellChange)
            {
                MessageBox.Show("Cell change");
            }
            if (anError.Context == DataGridViewDataErrorContexts.Parsing)
            {
                MessageBox.Show("parsing error");
            }
            if (anError.Context == DataGridViewDataErrorContexts.LeaveControl)
            {
                MessageBox.Show("leave control error");
            }

            if ((anError.Exception) is ConstraintException)
            {
                DataGridView view = (DataGridView)sender;
                view.Rows[anError.RowIndex].ErrorText = "an error";
                view.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText = "an error";

                anError.ThrowException = false;
            } */
        }

        private void DeleteDistSeq()
        {
            try
            {
                SqlConnection con = GetConnection();
                SqlCommand cmd = new SqlCommand();

                /* Delete any existing my distributions for this transaction */
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@DCMTNMBR", VoucherNumber));
                cmd.Parameters.Add(new SqlParameter("@MASTERTYPE", MasterType));

                cmd.CommandText = "rbsDeleteDistSeq";

                cmd.Connection = con;

                cmd.ExecuteNonQuery();

                con.Close();

            }
            catch (Exception ex)
            {
                string eMsg = "003A: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                if (stackTraceWanted) MessageBox.Show(eMsg);
            }
        }

        private void UpdateDistSeq()
        {
            try
            {
                SqlConnection con = GetConnection();
                SqlCommand cmd = new SqlCommand();

                /* Loop through the distribution buffer and insert any distributions */

                TableError err;
                /* Create a reference to the table */
                CmDistributionTempTable CMDistTable;
                /* CmDistributionTable CMDistTable; */
               
                /* This points to the form table buffer */
                CMDistTable = Dynamics.Forms.CmTransactionEntry.Tables.CmDistributionTemp;

                /* Read through the distributions writing a record for each distribution into temp seq table */
                err = CMDistTable.GetFirst();

                while (err != TableError.EndOfTable)
                {
                    SeqNumber = CMDistTable.DistributionSequenceNumber;

                    cmd = new SqlCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@DCMTNMBR", VoucherNumber));
                    cmd.Parameters.Add(new SqlParameter("@MASTERTYPE", MasterType));
                    cmd.Parameters.Add(new SqlParameter("@DSTSQNUM", SeqNumber));
                    cmd.CommandText = "rbsInsertDistSeq";
                    cmd.Connection = con;
                    cmd.ExecuteNonQuery();


                    /* Get the next distribution in the range */
                    err = CMDistTable.GetNext();
                }

                con.Close();
                CMDistTable.Close();

            }
            catch (Exception ex)
            {
                string eMsg = "003B: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                if (stackTraceWanted) MessageBox.Show(eMsg);
            }
        }
        private void UpdateJobLinkerDistSeq()
        {
            try
            {
                SqlConnection con = GetConnection();
                SqlCommand cmd = new SqlCommand();

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@DCMTNMBR", VoucherNumber));
                cmd.Parameters.Add(new SqlParameter("@MASTERTYPE", MasterType));

                cmd.CommandText = "rbsUpdateJobLinkerDistSeq";

                cmd.Connection = con;

                cmd.ExecuteNonQuery();

                con.Close();
            }
            catch (Exception ex)
            {
                string eMsg = "003C: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                if (stackTraceWanted) MessageBox.Show(eMsg);
            }
        }
        private void UpdateJobLinker()
        {
            try
            {
                SqlConnection con = GetConnection();
                SqlCommand cmd = new SqlCommand();
                MessageBox.Show("Hello Other World");
                TableError err;
                /* Create a reference to the table */
                CmDistributionTempTable CMDistTable;
               
                /* This points to the form table buffer */
                CMDistTable = Dynamics.Forms.CmTransactionEntry.Tables.CmDistributionTemp;
                                
                /* Read through the distributions writing a record into Job Linker for each distribution */
                err = CMDistTable.GetFirst();
                while (err != TableError.EndOfTable)
                {
                    /* Distribution values */
                    CreditAmount = CMDistTable.CreditAmount;
                    DebitAmt = CMDistTable.DebitAmount;
                    ActIndx = CMDistTable.AccountIndex;
                    Reference = CMDistTable.DistributionReference;
                    SeqNumber = CMDistTable.DistributionSequenceNumber;

                    cmd = new SqlCommand();

                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(new SqlParameter("@JOBNUMBER", blank));
                    cmd.Parameters.Add(new SqlParameter("@BACHNUMB", BachNumb));
                    cmd.Parameters.Add(new SqlParameter("@BCHSOURC", BchSource));
                    cmd.Parameters.Add(new SqlParameter("@TRANSNMBR", VoucherNumber));
                    cmd.Parameters.Add(new SqlParameter("@MASTERTYPE", MasterType));
                    cmd.Parameters.Add(new SqlParameter("@MASTERID", MasterID));
                    cmd.Parameters.Add(new SqlParameter("@DOCAMNT", DocAmount));
                    cmd.Parameters.Add(new SqlParameter("@TRXTYPE", DocType));
                    cmd.Parameters.Add(new SqlParameter("@DSTSQNUM", SeqNumber));
                    cmd.Parameters.Add(new SqlParameter("@CNTRLTYP", ControlType));
                    cmd.Parameters.Add(new SqlParameter("@APTVCHNM", ApplyToVoucherNumber));
                    cmd.Parameters.Add(new SqlParameter("@SPCLDIST", SpecializedDistribution));
                    cmd.Parameters.Add(new SqlParameter("@DOCNUMBR", DocumentNumber));
                    cmd.Parameters.Add(new SqlParameter("@DOCDATE", DocDate));
                    cmd.Parameters.Add(new SqlParameter("@ACTINDX", ActIndx));
                    cmd.Parameters.Add(new SqlParameter("@CRDTAMNT", CreditAmount));
                    cmd.Parameters.Add(new SqlParameter("@DEBITAMT", DebitAmt));
                    cmd.Parameters.Add(new SqlParameter("@DISTTYPE", DistType));
                    cmd.Parameters.Add(new SqlParameter("@DISTTRXTYPE", blank));
                    cmd.Parameters.Add(new SqlParameter("@DISTSOURCE", DistSource));
                    cmd.Parameters.Add(new SqlParameter("@DISTREF", Reference));
                    cmd.Parameters.Add(new SqlParameter("@TRXSORCE", TrxSource));
                    cmd.Parameters.Add(new SqlParameter("@DOCSOURCE", DocSource));

                    cmd.CommandText = "rbsUpdateJobLinkerBTE";

                    cmd.Connection = con;

                    cmd.ExecuteNonQuery();

                    /* Get the next distreibution in the range */
                    err = CMDistTable.GetNext();
                }

                con.Close();
                CMDistTable.Close();
            }
            catch (Exception ex)
            {
                string eMsg = "003D: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                if (stackTraceWanted) MessageBox.Show(eMsg);
            }
        }
        private void DisplayJobLinker()
        {
            try
            {
                SqlConnection DataConnection = GetConnection();

                /* Define dataset for Transaction Types */
                TrxTypeDataSet = new System.Data.DataSet();
                TrxTypeDataSet.CaseSensitive = false;

                TrxTypeDataCommand = new System.Data.SqlClient.SqlCommand();
                TrxTypeDataCommand.Connection = DataConnection;

                TrxTypeDataCommand.CommandText = "select LTRIM(DISTTRXTYPE) AS DISTTRXTYPE from JOB_TRX_TYPES where formtype='COST' ORDER BY DISTTRXTYPE";

                TrxTypeDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                TrxTypeDataAdapter.SelectCommand = TrxTypeDataCommand;

                TrxTypeDataAdapter.Fill(TrxTypeDataSet);
                /* Define dataset for GL distribution amounts */
                DataDataSet = new System.Data.DataSet();
                DataDataSet.CaseSensitive = false;

                DataCommand = new System.Data.SqlClient.SqlCommand();
                DataCommand.Connection = DataConnection;

                DataCommand.CommandText = "select JOBNUMBER,ACTNUMST,DEBITAMT,CRDTAMNT,DISTTYPE,DISTTRXTYPE,DISTREF,BCHSOURC,TRANSNMBR,TRXTYPE,DSTSQNUM,CNTRLTYP,APTVCHNM,SPCLDIST " +
                "from JOB_LINKER WHERE TRANSNMBR ='" + VoucherNumber + "' order by DSTSQNUM";

                DataDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                DataDataAdapter.SelectCommand = DataCommand;
                _commandBuilder = new SqlCommandBuilder(DataDataAdapter);

                DataDataAdapter.Fill(DataDataSet);

                dgvViewJobLinker.DataError += new DataGridViewDataErrorEventHandler(dgvJobLinker_DataError);

                dgvViewJobLinker.RowHeadersVisible = false;
                dgvViewJobLinker.DataSource = DataDataSet.Tables[0].DefaultView;
                dgvViewJobLinker.AutoGenerateColumns = true;
                dgvViewJobLinker.DefaultCellStyle.NullValue = ' ';
                dgvViewJobLinker.AllowUserToAddRows = false;

                DataGridViewButtonColumn colButton = new DataGridViewButtonColumn();
                colButton.HeaderText = "";
                colButton.Name = "btnLookupJob";
                colButton.Text = "Lookup Job";
                colButton.UseColumnTextForButtonValue = true;

                dgvViewJobLinker.Columns.Insert(0, colButton);
                dgvViewJobLinker.Columns[0].Width = 80;

                dgvViewJobLinker.Columns[1].Width = 100;
                dgvViewJobLinker.Columns[1].ReadOnly = false;
                dgvViewJobLinker.Columns[1].HeaderText = "Job Number";

                dgvViewJobLinker.Columns[2].Width = 100;
                dgvViewJobLinker.Columns[2].ReadOnly = true;
                dgvViewJobLinker.Columns[2].HeaderText = "GL Account";

                dgvViewJobLinker.Columns[3].Width = 84;
                dgvViewJobLinker.Columns[3].ReadOnly = true;
                dgvViewJobLinker.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvViewJobLinker.Columns[3].DefaultCellStyle.Format = "c";
                dgvViewJobLinker.Columns[3].HeaderText = "Debit Amount";

                dgvViewJobLinker.Columns[4].Width = 84;
                dgvViewJobLinker.Columns[4].ReadOnly = true;
                dgvViewJobLinker.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvViewJobLinker.Columns[4].DefaultCellStyle.Format = "c";
                dgvViewJobLinker.Columns[4].HeaderText = "Credit Amount";

                dgvViewJobLinker.Columns[5].Width = 100;
                dgvViewJobLinker.Columns[5].ReadOnly = true;
                dgvViewJobLinker.Columns[5].HeaderText = "Dist Type";

                /* Add a new Column (ComboBox) for the Transaction Type column */
                DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();

                colType.DefaultCellStyle.NullValue = " ";
                colType.DataSource = TrxTypeDataSet.Tables[0];
                colType.ValueMember = DataDataSet.Tables[0].Columns[5].ColumnName.ToString();
                colType.DisplayMember = colType.ValueMember;
                colType.HeaderText = "Transaction Type";
                colType.DropDownWidth = 90;
                colType.Width = 90;
                colType.MaxDropDownItems = 7;
                colType.DataPropertyName = DataDataSet.Tables[0].Columns[5].ColumnName.ToString();
                colType.Name = DataDataSet.Tables[0].Columns[5].ColumnName.ToString();
                dgvViewJobLinker.Columns.RemoveAt(6);
                dgvViewJobLinker.Columns.Insert(6, colType);

                dgvViewJobLinker.Columns[7].Width = 292;
                dgvViewJobLinker.Columns[7].ReadOnly = false;
                dgvViewJobLinker.Columns[7].HeaderText = "Dist Reference";

                dgvViewJobLinker.Columns[8].Visible = false;
                dgvViewJobLinker.Columns[9].Visible = false;
                dgvViewJobLinker.Columns[10].Visible = false;
                dgvViewJobLinker.Columns[11].Visible = false;
                dgvViewJobLinker.Columns[12].Visible = false;
                dgvViewJobLinker.Columns[13].Visible = false;
                dgvViewJobLinker.Columns[14].Visible = false;

                dgvViewJobLinker.EditMode = DataGridViewEditMode.EditOnEnter;
                dgvViewJobLinker.Focus();
            }
            catch (Exception ex)
            {
                string eMsg = "003E: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }
        }
        private void dgvViewJobLinker_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex >= 0)
            {

                using (frmLookupJob ViewAgenciesByJobsForm = new frmLookupJob())
                {
                    if (ViewAgenciesByJobsForm.ShowDialog(this) == DialogResult.OK)
                    {
                        ViewAgenciesByJobsForm.Show();
                        ViewAgenciesByJobsForm.Activate();
                        ViewAgenciesByJobsForm.Focus();
                        dgvViewJobLinker[1, dgvViewJobLinker.CurrentCell.RowIndex].Value = ViewAgenciesByJobsForm.CellValue;
                    }
                }
            }
        }
    }
}

Open in new window

Commented:
Where did you declare the textboxes? I did not see them within this file.
rwheeler23President

Author

Commented:
They were dropped in by VS after I drop them onto the form. They are not in this code but where they usually go by adding them to the form. I forget the correct terminology but I do see them there. Perhaps I should check the class they are in. They may have been dropped into the wrong class.
rwheeler23President

Author

Commented:
I had no idea what is wrong here so I recreated the project from scratch and started adding the code. This time no problem assigning values to the text fields. I am going to find some VS 2008 guru and use this project as debugging training exercise. Using breakpoints did not show me anything because the errors would occur before the breakpoint was hit.
Software Engineer cum AD Developer
Commented:
Hello rwheeler23,

After reading the whole discussion between you and other experts I reached at some conclusion.
But before that let me answer your question:

Q. They were dropped in by VS after I drop them onto the form. They are not  in this code but where they usually go by adding them to the form.

A. The declaration of controls on form are done by IDE in Form1.designer.cs where Form1 is the name of your form. For e.g. if I drop a single textbox on the form I would get code as shown in attach (Form1.designer.cs).

So what's the wrong seems to me is that the textbox control you are dropping or adding on form has been not assigned memory in designer class (somehow). So cross check if memory is assigned to those added or droped textbox on the form in Form1.desinger.cs.

And let us know the result back so that we can too know what is the culprit?

Regards,
VSS



namespace TestingOnlyDELETE
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(96, 123);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 6;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
    }
}

Open in new window

rwheeler23President

Author

Commented:
Here is the code. Can you see anything wrong here?
namespace PopulateJobLinkerBTE
{
    partial class frmViewJobLinker
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.txtCheckbookID = new System.Windows.Forms.TextBox();
            this.txtCheckbookName = new System.Windows.Forms.TextBox();
            this.txtNumber = new System.Windows.Forms.TextBox();
            this.txtOption = new System.Windows.Forms.TextBox();
            this.txtType = new System.Windows.Forms.TextBox();
            this.txtTransactionDate = new System.Windows.Forms.TextBox();
            this.txtPaidToRcvdFrom = new System.Windows.Forms.TextBox();
            this.txtDescription = new System.Windows.Forms.TextBox();
            this.txtDocAmount = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(0, 30);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(100, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Checkbook ID";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(0, 50);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(100, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Checkbook Name";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(0, 70);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(100, 13);
            this.label3.TabIndex = 2;
            this.label3.Text = "Number";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(300, 30);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(100, 13);
            this.label4.TabIndex = 3;
            this.label4.Text = "Option";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(300, 50);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(100, 13);
            this.label5.TabIndex = 4;
            this.label5.Text = "Type";
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(300, 70);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(100, 13);
            this.label6.TabIndex = 5;
            this.label6.Text = "Transaction Date";
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(580, 30);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(73, 13);
            this.label7.TabIndex = 6;
            this.label7.Text = "Rcvd From";
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(580, 50);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(73, 13);
            this.label8.TabIndex = 7;
            this.label8.Text = "Description";
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(580, 70);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(73, 13);
            this.label9.TabIndex = 8;
            this.label9.Text = "Amount";
            // 
            // txtCheckbookID
            // 
            this.txtCheckbookID.BackColor = System.Drawing.SystemColors.Window;
            this.txtCheckbookID.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtCheckbookID.Location = new System.Drawing.Point(119, 30);
            this.txtCheckbookID.Name = "txtCheckbookID";
            this.txtCheckbookID.Size = new System.Drawing.Size(175, 13);
            this.txtCheckbookID.TabIndex = 9;
            // 
            // txtCheckbookName
            // 
            this.txtCheckbookName.BackColor = System.Drawing.SystemColors.Window;
            this.txtCheckbookName.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtCheckbookName.Location = new System.Drawing.Point(119, 50);
            this.txtCheckbookName.Name = "txtCheckbookName";
            this.txtCheckbookName.Size = new System.Drawing.Size(175, 13);
            this.txtCheckbookName.TabIndex = 10;
            // 
            // txtNumber
            // 
            this.txtNumber.BackColor = System.Drawing.SystemColors.Window;
            this.txtNumber.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtNumber.Location = new System.Drawing.Point(119, 69);
            this.txtNumber.Name = "txtNumber";
            this.txtNumber.Size = new System.Drawing.Size(175, 13);
            this.txtNumber.TabIndex = 11;
            // 
            // txtOption
            // 
            this.txtOption.BackColor = System.Drawing.SystemColors.Window;
            this.txtOption.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtOption.Location = new System.Drawing.Point(410, 30);
            this.txtOption.Name = "txtOption";
            this.txtOption.Size = new System.Drawing.Size(164, 13);
            this.txtOption.TabIndex = 12;
            // 
            // txtType
            // 
            this.txtType.BackColor = System.Drawing.SystemColors.Window;
            this.txtType.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtType.Location = new System.Drawing.Point(410, 50);
            this.txtType.Name = "txtType";
            this.txtType.Size = new System.Drawing.Size(164, 13);
            this.txtType.TabIndex = 13;
            // 
            // txtTransactionDate
            // 
            this.txtTransactionDate.BackColor = System.Drawing.SystemColors.Window;
            this.txtTransactionDate.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtTransactionDate.Location = new System.Drawing.Point(410, 70);
            this.txtTransactionDate.Name = "txtTransactionDate";
            this.txtTransactionDate.Size = new System.Drawing.Size(164, 13);
            this.txtTransactionDate.TabIndex = 14;
            // 
            // txtPaidToRcvdFrom
            // 
            this.txtPaidToRcvdFrom.BackColor = System.Drawing.SystemColors.Window;
            this.txtPaidToRcvdFrom.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtPaidToRcvdFrom.Location = new System.Drawing.Point(659, 30);
            this.txtPaidToRcvdFrom.Name = "txtPaidToRcvdFrom";
            this.txtPaidToRcvdFrom.Size = new System.Drawing.Size(165, 13);
            this.txtPaidToRcvdFrom.TabIndex = 15;
            // 
            // txtDescription
            // 
            this.txtDescription.BackColor = System.Drawing.SystemColors.Window;
            this.txtDescription.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtDescription.Location = new System.Drawing.Point(659, 50);
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new System.Drawing.Size(165, 13);
            this.txtDescription.TabIndex = 16;
            // 
            // txtDocAmount
            // 
            this.txtDocAmount.BackColor = System.Drawing.SystemColors.Window;
            this.txtDocAmount.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtDocAmount.Location = new System.Drawing.Point(659, 70);
            this.txtDocAmount.Name = "txtDocAmount";
            this.txtDocAmount.Size = new System.Drawing.Size(165, 13);
            this.txtDocAmount.TabIndex = 17;
            // 
            // frmViewJobLinker
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(926, 424);
            this.Controls.Add(this.txtDocAmount);
            this.Controls.Add(this.txtDescription);
            this.Controls.Add(this.txtPaidToRcvdFrom);
            this.Controls.Add(this.txtTransactionDate);
            this.Controls.Add(this.txtType);
            this.Controls.Add(this.txtOption);
            this.Controls.Add(this.txtNumber);
            this.Controls.Add(this.txtCheckbookName);
            this.Controls.Add(this.txtCheckbookID);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Location = new System.Drawing.Point(3, 90);
            this.Name = "frmViewJobLinker";
            this.Text = "View Job Linker GL Distributions";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.TextBox txtCheckbookID;
        private System.Windows.Forms.TextBox txtCheckbookName;
        private System.Windows.Forms.TextBox txtNumber;
        private System.Windows.Forms.TextBox txtOption;
        private System.Windows.Forms.TextBox txtType;
        private System.Windows.Forms.TextBox txtTransactionDate;
        private System.Windows.Forms.TextBox txtPaidToRcvdFrom;
        private System.Windows.Forms.TextBox txtDescription;
        private System.Windows.Forms.TextBox txtDocAmount;
    }
}

Open in new window

Vikram Singh SainiSoftware Engineer cum AD Developer

Commented:
Hi,

Yes. I found something missing in your code. As you mentioned in your code snippet (in question) that you are getting error on code:

  txtOptType.Text = "Enter Transaction:";

But in your designer.cs class there is no declaration of same textbox (as shown in code snippet below from comment ID: 33928278:

  private System.Windows.Forms.TextBox txtCheckbookID;
        private System.Windows.Forms.TextBox txtCheckbookName;
        private System.Windows.Forms.TextBox txtNumber;
        private System.Windows.Forms.TextBox txtOption;
        private System.Windows.Forms.TextBox txtType;
        private System.Windows.Forms.TextBox txtTransactionDate;
        private System.Windows.Forms.TextBox txtPaidToRcvdFrom;
        private System.Windows.Forms.TextBox txtDescription;
        private System.Windows.Forms.TextBox txtDocAmount;

So I am just trying to get it resolve by that either you should declare txtOptType in designer.cs (or create a textbox with that name) or you can remove any related code of txtOptType from your application code. Probably this can create problem for the whole application. Try the suggestion and let us know back.

Regards,
VSS
Commented:
txtOptType must be declared somewhere in your files. Otherwise you would get a different error message.

Use "search in project" to see which file contain the declaration.
rwheeler23President

Author

Commented:
I remember when I first stumbled upon this issue that I thought perhaps the word 'Option' was a resevered word so I change the field name in the code to txtOptType and then I changed the field name on the form. Since when I search for txtOptType it does not find it in the designer section leads me to believe that when I changed the name on the form it did not change it throughtout the rest of the project.

Commented:
You must changed the declaration of it as well, just may not be the right one.

Why not using "search in whole solution" to dig out all the occurrence?
rwheeler23President

Author

Commented:
OK, somehow I have lost the properties panel. I right click on any object and select properties and nothing happens. All of my project now work this way. I can see it if I switch to Full Screen. How do I get the properties panel back?
rwheeler23President

Author

Commented:
Nevermind previous question. I kept dragging the solution explorer and then another box popped up and when I clicked on it there was the property window.

Anyway, I searched the entire solution for txtOption with nothing found.
I searched for txtOptType and it appeared to be all the correct places.

I really appreciated everyone's help with this but but retyping it back all is working. I will just write this off as another one of these Windows issues.

I have no problem sending someoen the complete project if someone wants to look at it. You will not able to compile it unless you have acccess to the MS Dynamics GP 10 SDK for VS.

Commented:
What happened if you hit Ctril+W,P?
rwheeler23President

Author

Commented:
If it was not in Full Screen mode, absolutely nothing. When I started dragging the SOlution Explorer around the screen, a small directional arrow appeared which allowed me to put the Property screen back.

Commented:
It's the anchor icon. You must have put it somewhere outside the screen:)
Vikram Singh SainiSoftware Engineer cum AD Developer

Commented:
Hi,

What you mean by Anyway, I searched the entire solution for txtOption with nothing found.
I  searched for txtOptType and it appeared to be all the correct places.
?

If txtOptType is appearing and it is at correct places. Then it seems to me that you are creating same dynamically.

And as you said, but retyping it back all is working . Makes us to reach to conclusion that there is something wrong with your VS behavior.

Regards,
VSS





























































rwheeler23President

Author

Commented:
Thanks to everyone for your time on this.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial