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"
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