VS C# Event Timing

I have this field called txtDate which is pulled from the query field called PADOCDT. I want to convert it to MM/DD/YYY. When I run the code the conversion never happens and it never happens because the MessageBox.Show command shows the field has no value however however on the screen I am seeing the long date value. What am I missing here?

        private void GetDocumentJobNumberInfo()
        {
            /* Code to get the job number from the transaction, if any */
            try
            {
                GPDataSet = new System.Data.DataSet();
                GPDataSet.CaseSensitive = false;

                GPDataCommand = new System.Data.SqlClient.SqlCommand();
                GPDataCommand.Connection = GPDataConnection;

                GPDataCommand.CommandText = "SELECT TRXTYPE,PADocnumber20,PADOCDT,BACHNUMB,CUSTNMBR,CUSTNAME,CSTPONBR FROM rbsPADocNumber WHERE PADocnumber20 = '" + PADocNumber + "'";

                GPDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                GPDataAdapter.SelectCommand = GPDataCommand;
                GPDataAdapter.TableMappings.Add("Table", "DOCNUMBER");

                GPDataAdapter.Fill(GPDataSet);
                if (GPDataSet == null || (GPDataSet.Tables.Count == 0) || (GPDataSet.Tables[0].Rows.Count == 0))
                {
                    MessageBox.Show("No documents found for this job");
                    /* Dispose of the connection */
                    GPConnObj = null;

                    /* Shutdown the connection */
                    resp = GPConnection.Shutdown();
                    GPDataConnection.Close();
                    this.Hide();
                    this.Dispose(); 
                }

                this.txtTrxType.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"TRXTYPE").ToString();
                this.txtTrxType.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtDocNumber.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"PADocnumber20").ToString();
                this.txtDocNumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtDate.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"], "PADOCDT").ToString();
                this.txtDate.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
                MessageBox.Show(this.txtDate.Text);

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

                MessageBox.Show(this.txtDate.Text);

                this.txtBatchID.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"BACHNUMB").ToString();
                this.txtBatchID.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtCustID.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"CUSTNMBR").ToString();
                this.txtCustID.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtCustName.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"CUSTNAME").ToString();
                this.txtCustName.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;

                this.txtCustPONumber.DataBindings.Add("Text", GPDataSet.Tables["DOCNUMBER"],"CSTPONBR").ToString();
                this.txtCustPONumber.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;


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

Open in new window

LVL 1
rwheeler23Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
try
                {
                    if (this.txtDate.Text != "")
                    {
                        this.txtDate.Text = Convert.ToDateTime(this.txtDate.Text).ToString("MM/dd/yyyy");
                    }
                }


that looks wrong to me.   Shouldn't that be if (string.IsnullOrEmpty(txtDate.text))??


Also not sure if you need to add the databinding . . . you can just say
txtDate.Text = Convert.ToDateTime(this.txtDate.Text).ToString("MM/dd/yyyy");
0
rwheeler23Author Commented:
If the string is empty I do not want it to try to convert it because the conversion will fail. The query is reading from a view so my fix right now is to do the conversion in the view.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
What if you just checked the data table directly . . . assuming there is only one row?  If not then you'll need to do it during the databind.

                try
                {
                    if (!string.IsNullOrEmpty(x.Tables["DOCNUMBER"].Rows[0]["PADOCDT"].ToString()))
            txtDate.Text = Convert.ToDateTime(x.Tables["DOCNUMBER"].Rows[0]["PADOCDT"].ToString()).ToString("MM/dd/yyyy");
        
                }
                catch (Exception ex)
                {
                    string eMsg = "001: ERROR: " + ex.Message;
                    if (StackTraceWanted) eMsg += "\n" + ex.StackTrace;
                    MessageBox.Show(eMsg);
                }

Open in new window


Are there any events about like databinding or onDataBound?
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

rwheeler23Author Commented:
There are no other events. What I have to do is locate one record and pull out back a few values. This one date field just refused to format properly. Maybe I should put this code in the load form event so the values will be there before the form appears on the screen.,
0
Kyle AbrahamsSenior .Net DeveloperCommented:
If you're only loading up the values the one time sure . . . but if you require updated values based on data changing (eg: looking something up) then you're going to need a different approach.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwheeler23Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.