?
Solved

Visual Studio C# Date Formats

Posted on 2010-01-12
15
Medium Priority
?
289 Views
Last Modified: 2012-05-08
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#.
 
0
Comment
Question by:rwheeler23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
15 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 26296123
use DateTimeObject.ToShortDateString()
0
 
LVL 21

Expert Comment

by:silemone
ID: 26296131
see the link:

use DateTimeObject.ToShortDateString()
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:silemone
ID: 26296175
see your duplicate link
0
 

Author Comment

by:rwheeler23
ID: 26297906
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
 
LVL 21

Expert Comment

by:silemone
ID: 26297931

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

0
 
LVL 21

Expert Comment

by:silemone
ID: 26297936

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

Author Comment

by:rwheeler23
ID: 26298478
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
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 26302621
Hi,

Try below code

txtDateOfQuote.Text = Convert.ToDateTime(txtDateOfQuote.Text).ToString("MM/dd/yy");
0
 

Author Comment

by:rwheeler23
ID: 26304594
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
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 26304769
Hi,

Are you getting any error when executing?
0
 

Author Comment

by:rwheeler23
ID: 26304776
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
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 26305002
Hi,

What format of date you are getting before convert?

Can you post the exact date here?
0
 

Author Comment

by:rwheeler23
ID: 26305116
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
 
LVL 12

Accepted Solution

by:
rajapandian_81 earned 2000 total points
ID: 26305715
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question