none
How to convert string data type to Date format in C# RRS feed

  • Question

  • Here I added code for inserting record one of the date field is not inserting data because of string data type. Code as below...

                    ItemTrans Add = new ItemTrans();
                    Add.ItemTransRecVcNo = txtRecVcNo.Text;
                    Add.ItemTransRecDate = Convert.ToDateTime((maskedRecVcDate.Text).ToString()); 
                    Add.ItemTransID = cboItemRecID.Text;
                    Add.ItemTransName = txtItemNameRec.Text;
                    Add.ItemTransRecUnit = txtItemRecUnit.Text;
                    Add.ItemTransRecQty = Convert.ToDecimal(txtItemRecQty.Text);
                    Add.ItemTransRecRate = Convert.ToDecimal(txtItemRecRate.Text);
                    Add.ItemTransRecAmount = Convert.ToDecimal(txtItemRecAmount.Text);
    
                    InventoryHandler AddHandler = new InventoryHandler();
                    if (AddHandler.AddItemTrans(Add)==true)
                    {
                        MessageBox.Show("Record Inserted Successfully !", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        cboItemRecID.Focus();
                        GridRefresh();
                    }

    Saturday, November 3, 2012 6:34 AM

Answers

  • Thanks for your reply. May I expect that you will write code for me on above my code. Thanks

    Below is an example of the TryParse method.

                DateTime parsedMaskedRecVcDate;
                bool validDate = DateTime.TryParse(maskedRecVcDate.Text, out parsedMaskedRecVcDate);
                if (!validDate)
                {
                    MessageBox.Show("Record insertion failed!"
                        , string.Format("Invaid maskedRecVcDate \"{0}\" specified", maskedRecVcDate.Text)
                        , MessageBoxButtons.OK
                        , MessageBoxIcon.Warning);
                    return;
                }
                Add.ItemTransRecDate = parsedMaskedRecVcDate; 
    



    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/

    Sunday, November 4, 2012 3:25 PM
  • Thanks for reply continue connect with me. Here I added solution it's working.

        /// Start Converting string to DateTime format ///
                    
    string X = "dd/MM/yyyy";
    string Y = "yyyy/MM/dd";
    DateTime Z = DateTime.ParseExact(maskedRecVcDate.Text, X, null);
    Add.ItemTransRecDate = Z.ToString(Y);
                    
        /// End Converting string to DateTime format ///
                    

    Expecting for next suggestion.

    Tuesday, November 6, 2012 3:56 AM

All replies

  •                 Add.ItemTransRecDate = Convert.ToDateTime((maskedRecVcDate.Text).ToString()); 
    

    Convert.ToDateTime method returns a DateTime after parsing the string.  Are you getting an error because the string isn't in a valid format?  You could use DateTime.TryParse to handle an improper date format.


    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/


    Saturday, November 3, 2012 2:31 PM
  • Hi Dan

    Thanks for your reply. May I expect that you will write code for me on above my code. Thanks

    Sunday, November 4, 2012 7:08 AM
  • Thanks for your reply. May I expect that you will write code for me on above my code. Thanks

    Below is an example of the TryParse method.

                DateTime parsedMaskedRecVcDate;
                bool validDate = DateTime.TryParse(maskedRecVcDate.Text, out parsedMaskedRecVcDate);
                if (!validDate)
                {
                    MessageBox.Show("Record insertion failed!"
                        , string.Format("Invaid maskedRecVcDate \"{0}\" specified", maskedRecVcDate.Text)
                        , MessageBoxButtons.OK
                        , MessageBoxIcon.Warning);
                    return;
                }
                Add.ItemTransRecDate = parsedMaskedRecVcDate; 
    



    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/

    Sunday, November 4, 2012 3:25 PM
  • Thanks for reply. Your code is ok for MM/dd/yyyy Date format. But I want dd/MM/yyyy format. When I was trying to push value dd/MM/yyyy format then exception coming. How I could resolve?

    Thanks

    Monday, November 5, 2012 6:12 AM
  • Thanks for reply. Your code is ok for MM/dd/yyyy Date format. But I want dd/MM/yyyy format. When I was trying to push value dd/MM/yyyy format then exception coming. How I could resolve?

    In that case, you can use the TryParseExact method, specifying the desired format string:

    bool validDate = DateTime.TryParseExact(
        maskedRecVcDate.Text
        , "dd/MM/yyyy"
        , new System.Globalization.CultureInfo("en-us")
        , System.Globalization.DateTimeStyles.None
        , out parsedMaskedRecVcDate);


    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/

    Tuesday, November 6, 2012 1:21 AM
  • Thanks for reply continue connect with me. Here I added solution it's working.

        /// Start Converting string to DateTime format ///
                    
    string X = "dd/MM/yyyy";
    string Y = "yyyy/MM/dd";
    DateTime Z = DateTime.ParseExact(maskedRecVcDate.Text, X, null);
    Add.ItemTransRecDate = Z.ToString(Y);
                    
        /// End Converting string to DateTime format ///
                    

    Expecting for next suggestion.

    Tuesday, November 6, 2012 3:56 AM