• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 45
  • Last Modified:

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

0
rwheeler23
Asked:
rwheeler23
  • 3
  • 3
1 Solution
 
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
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
 
rwheeler23Author Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now