Visual Studio C# Date Formats

I have selected data and have placed in on the form. I cannot quite get the syntax for the date format correct. I want it to be short fomat MM/DD/YY. I have guides for the various formats but I cannot quite get the syntax correct for VS 2008 C#.
 
LVL 1
rwheeler23Asked:
Who is Participating?
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.

silemoneCommented:
use DateTimeObject.ToShortDateString()
0
silemoneCommented:
see the link:

use DateTimeObject.ToShortDateString()
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

silemoneCommented:
see your duplicate link
0
rwheeler23Author Commented:
How do I enter the entire line? I tried several combinations of what is below and nothing works.

this.txtDateOfQuote.Text = this.txtDateOfQuote.ToShortDateString();
0
silemoneCommented:

this.txtDateOfQuote.Text = new DateTime(this.txtDateOfQuote.Text.ToString()).ToShortDateString();

0
silemoneCommented:

this.txtDateOfQuote.Text = (new DateTime(this.txtDateOfQuote.Text.ToString()).ToShortDateString());
0
rwheeler23Author Commented:
When I do this I get these two error messages.

Error      1      The best overloaded method match for 'System.DateTime.DateTime(long)' has some invalid arguments      C:\VSProjects\ViewQuote\ViewQuote\ViewQuote.cs      67      41      ViewQuote
Error      2      Argument '1': cannot convert from 'string' to 'long'      C:\VSProjects\ViewQuote\ViewQuote\ViewQuote.cs      67      54      ViewQuote
0
rajapandian_81Commented:
Hi,

Try below code

txtDateOfQuote.Text = Convert.ToDateTime(txtDateOfQuote.Text).ToString("MM/dd/yy");
0
rwheeler23Author Commented:
This is strange. I insert this line, it builds successfully but when I execute the code, the screen on which this code belongs no longer appears. If I comment out this line it does appear.
0
rajapandian_81Commented:
Hi,

Are you getting any error when executing?
0
rwheeler23Author Commented:
I can see what is happening but I do not understand it. I can see the date on the screen in long format but by the time it gets to the code to convert it, the date field is blank. What am I missing here?

this.txtDateOfQuote.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "DATE_OF_QUOTE_OEQH");
           
            if (txtDateOfQuote.Text != "" )
            {
                txtDateOfQuote.Text = Convert.ToDateTime(txtDateOfQuote.Text).ToString("MM/dd/yy");
            }
0
rajapandian_81Commented:
Hi,

What format of date you are getting before convert?

Can you post the exact date here?
0
rwheeler23Author Commented:
Here is the entire section of code. The date on this particular order is 12/28/09 12:00:00 AM.
I think I am still stuck in my old 'C' top down world. I have data for the header and data for the grid but I am using the same DataAdapter variable. As I said, I see the date on the screen but my code sees it as blank. Is my second use of DataAdapter wiping out the date?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using Microsoft.Dexterity.Bridge;
using Microsoft.Dexterity.Applications;
using Microsoft.Dexterity.Applications.DynamicsDictionary;
using Microsoft.Dexterity.Shell;

namespace ViewQuote
{
    public partial class ViewQuote : DexUIForm
    {
        public static string myAppName = "View Quote Program";
        /* Setup SOP form so you can extract value off SOP_ENTRY form */
        static SopEntryForm sopEntryForm = Dynamics.Forms.SopEntry;
        static SopEntryForm.SopEntryWindow sopEntryWindow = sopEntryForm.SopEntry;
        
        /* Setup SQL comnnections for Quote Header and Quote Detail */
        private System.Data.SqlClient.SqlConnection qdConnection;
        private System.Data.SqlClient.SqlConnection qhConnection;
        private System.Data.DataSet QuoteHeaderDataSet;
        private System.Data.DataSet QuoteDetailDataSet;
        private System.Data.SqlClient.SqlCommand qdCommand;
        private System.Data.SqlClient.SqlCommand qhCommand;
        private System.Data.SqlClient.SqlDataAdapter DataAdapter;

        /* Define connection string */
        string connectionString = "Data Source=VISTAPOWMATRICH;Initial Catalog=POWMATQP;User Id=sa;Password=223;Connection Timeout=10";
             
        public ViewQuote()
        {
            InitializeComponent();
            
            /* Populate text field with sales order number */
            txtSOPNUMBE.Text = sopEntryWindow.SopNumber.Value;

            /* Define the dataset for the Quote Header Information */
            qhConnection = new System.Data.SqlClient.SqlConnection(connectionString);
            qhConnection.Open();

            QuoteHeaderDataSet = new System.Data.DataSet();
            QuoteHeaderDataSet.CaseSensitive = false;

            qhCommand = new System.Data.SqlClient.SqlCommand();
            qhCommand.Connection = qhConnection;

            qhCommand.CommandText = "select top 1 QUOTE_NUMBER_OEQH,SEQUENCE_NUMBER_OEQH,REVALIDATE_NUMB_OEQH,CUSTOMER_NUMBER_OEQH,SALES_AGENT_OEQH,DATE_OF_QUOTE_OEQH,CUSTOMER_NAME_OEQH,CUSTOMR_CONTACT_OEQH,CUSTOMER_PO_#_OEQH,AMRKCODE_OEQH from SOPLINES s1, CSTQUTHD s2 where s1.QUOTE_NUMBER_SOPL=s2.QUOTE_NUMBER_OEQH and s1.SEQUENCE_NUMBER_SOPL=s2.SEQUENCE_NUMBER_OEQH and s1.REVALIDATE_NUMBER_SOPL=s2.REVALIDATE_NUMB_OEQH AND s1.sopnumbe_sopl = '" + sopEntryWindow.SopNumber.Value +"'"; 

            DataAdapter = new System.Data.SqlClient.SqlDataAdapter();
            DataAdapter.SelectCommand = qhCommand;
            DataAdapter.TableMappings.Add("Table", "QuoteHeader");
            
            DataAdapter.Fill(QuoteHeaderDataSet);
            
            this.txtQuoteNumber.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "QUOTE_NUMBER_OEQH");
            this.txtSequenceNumber.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "SEQUENCE_NUMBER_OEQH");
            this.txtRevalidateNumber.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "REVALIDATE_NUMB_OEQH");
            this.txtCustomerNumber.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "CUSTOMER_NUMBER_OEQH");
            this.txtSalesAgent.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "SALES_AGENT_OEQH");
            this.txtDateOfQuote.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "DATE_OF_QUOTE_OEQH");
            
            if (txtDateOfQuote.Text != "" )
            {
                txtDateOfQuote.Text = Convert.ToDateTime(txtDateOfQuote.Text).ToString("MM/dd/yy"); 
            }
           

            this.txtCustomerName.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "CUSTOMER_NAME_OEQH");
            this.txtCustomerContact.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "CUSTOMR_CONTACT_OEQH");
            this.txtCustRefNumber.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "CUSTOMER_PO_#_OEQH");
            this.txtMarket.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "AMRKCODE_OEQH");
                                            
            /* Make sure a quote was found for this sales order */
           
            /* Define the dataset for the Quote Detail Information */
            qdConnection = new System.Data.SqlClient.SqlConnection(connectionString);
            qdConnection.Open();

            QuoteDetailDataSet = new System.Data.DataSet();
            QuoteDetailDataSet.CaseSensitive = false;

            qdCommand = new System.Data.SqlClient.SqlCommand();
            qdCommand.Connection = qdConnection;

            qdCommand.CommandText = "SELECT LINE_NUMBER_OEQL,PART_NUMBER_OEQL,QUOTE_QUANTITY_OEQL,PART_DESCRPTION_OEQL,UNIT_OF_MEASURE_OEQL,VENDOR_QUOT_CST_OEQL,VENDOR_QUOT_QTY_OEQL,VENDOR_QT_DATE_OEQL,VENDOR_#_QUOTE_OEQL,QUOTE_SELL_PRIC_OEQL,DELIVERY_TEXT_OEQL,QT_LINE_COMMENT_OEQL from SOPLINES s1, CSTQUTLN s2 where s1.QUOTE_NUMBER_SOPL=s2.QUOTE_NUMBER_OEQL and s1.SEQUENCE_NUMBER_SOPL=s2.SEQUENCE_NUMBER_OEQL and s1.REVALIDATE_NUMBER_SOPL=s2.REVALIDATE_NUMB_OEQL AND S1.LINE_NUMBER_SOPL=S2.LINE_NUMBER_OEQL AND s1.sopnumbe_sopl = '" + sopEntryWindow.SopNumber.Value + "' ORDER BY S2.LINE_NUMBER_OEQL";

            DataAdapter = new System.Data.SqlClient.SqlDataAdapter();
            DataAdapter.SelectCommand = qdCommand;
            DataAdapter.TableMappings.Add("Table", "QuoteDetail");

            DataAdapter.Fill(QuoteDetailDataSet);

            datagridQuoteDetail.ReadOnly = true;
            datagridQuoteDetail.RowHeadersVisible = false;
            datagridQuoteDetail.DataSource = QuoteDetailDataSet.Tables["QuoteDetail"].DefaultView;
            datagridQuoteDetail.Columns[0].Width = 40;
            datagridQuoteDetail.Columns[0].DefaultCellStyle.Format = "N2";
            datagridQuoteDetail.Columns[0].HeaderText = "Line";
            datagridQuoteDetail.Columns[1].Width = 110;
            datagridQuoteDetail.Columns[1].HeaderText = "Part Number";
            datagridQuoteDetail.Columns[2].Width = 40;
            datagridQuoteDetail.Columns[2].HeaderText = "QTY";
            datagridQuoteDetail.Columns[3].Width = 200;
            datagridQuoteDetail.Columns[3].HeaderText = "Part Description";
            datagridQuoteDetail.Columns[4].Width = 40;
            datagridQuoteDetail.Columns[4].HeaderText = "U/M";
            datagridQuoteDetail.Columns[5].Width = 60;
            datagridQuoteDetail.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            datagridQuoteDetail.Columns[5].DefaultCellStyle.Format = "c";
            datagridQuoteDetail.Columns[5].HeaderText = "Vendor Cost";
            datagridQuoteDetail.Columns[6].Width = 50;
            datagridQuoteDetail.Columns[6].HeaderText = "Vendor Quantity";
            datagridQuoteDetail.Columns[7].Width = 60;
            datagridQuoteDetail.Columns[7].HeaderText = "Quote Date";
            datagridQuoteDetail.Columns[8].Width = 60;
            datagridQuoteDetail.Columns[8].HeaderText = "Vendor Number"; 
            datagridQuoteDetail.Columns[9].Width = 60;
            datagridQuoteDetail.Columns[9].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            datagridQuoteDetail.Columns[9].DefaultCellStyle.Format = "c"; 
            datagridQuoteDetail.Columns[9].HeaderText = "Sell Price";
            datagridQuoteDetail.Columns[10].Width = 100;
            datagridQuoteDetail.Columns[10].HeaderText = "Delivery";
            datagridQuoteDetail.Columns[11].Width = 400;
            datagridQuoteDetail.Columns[11].HeaderText = "Comment"; 
        

            /* Close the detail connection */
            try
            {
                qdConnection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
                  
            /* Close the header connection */
            try
            {

                qhConnection.Close();

            }

            catch (Exception e)
            {

                Console.WriteLine(e.ToString());

            }

        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
        }

        private void txtSOPNUMBE_TextChanged(object sender, EventArgs e)
        {
            

        }

        private void txtMarket_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

    }
}

Open in new window

0
rajapandian_81Commented:
Hi,

Change the line
this.txtDateOfQuote.DataBindings.Add("Text", QuoteHeaderDataSet.Tables["QuoteHeader"], "DATE_OF_QUOTE_OEQH");

like below
txtDateOfQuote.Text = QuoteHeaderDataSet.Tables["QuoteHeader"].Rows[0]["DATE_OF_QUOTE_OEQH"].ToString();
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
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.

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.