CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    46

    Select date from database

    Code:
     //DateTime date= DateTime.Parse(maskedSearch.Text);
                                sqlConnection.Open();
                                sqlDataAdapter = new SqlDataAdapter("SELECT * FROM Transactions WHERE date='" +Convert.ToDateTime(maskedSearch.Text) + "'", sqlConnection);
                                dataTable = new DataTable();
                                dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
                                sqlDataAdapter.Fill(dataTable);
                                dataGridViewSearch.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
                                dataGridViewSearch.ReadOnly = true;
                                dataGridViewSearch.DataSource = dataTable;
                                dataGridViewSearch.Visible = true;
    Please i want to select based on the date entered into a maskedTextbox but it's showing error, what can I do to achieve this. Thanks. The error message is:
    "Conversion failed when converting date and / or time from character string"

  2. #2
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: Select date from database

    What is the masked text pulling in when you debug your code? I'm assuming you are doing some sort of globalization/localizing. Convert.ToDateTime naturally assumes the date format is in "mm/dd/yyyy". You can either do DateTime.TryParse, and utilize the date format you are entering, or set the thread of your current culture to invariant right before the sqldataadapter, and see if that works using your convert.todatetime.

    Let me know if this works.

    If this resolves your issue, please mark as resolved and rate up the post.

    Regards,
    Quinn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured