0

I need help with my comboboxes. An application to the value selected in the first combo imported dates in the second and the third combo combo. Only the dates look like this:2009-01-01 12:00:00AM. I want to cut 12:00:00AM. My query is:

string command2 = "select God_MinQ,God_AverQ,God_MaxQ,min(Dat) from hydgod where station='"
            + comboBox1.SelectedItem.ToString() + "' and Dat between '" 
            + comboBox2.SelectedItem.ToString() + "' and '" + comboBox3.SelectedItem.ToString() 
            + "' group by year(dat),month(Dat)";

Where can I format string in combobox2 and combobox3?

Another thing I wanted to ask you. When the user chooses from combo2 starting date to date as 2009-01-01 to 2010-01-01 Mesagebox out in year 2010, and he has chosen for the year 2009. How can I fix this. Here is a link to the screenshot: https://s30.postimg.org/60nuoocdd/Untitled.jpg

  • 1
    Please use [sql parameters](http://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements) in your query – Renatas M. Jan 09 '17 at 09:56
  • You can use `DateTime.ParseExact(comboBox.SelectedItem, "yyyy-mm-dd", null).ToString()` and use parameterized queries instead. – Mohit S Jan 09 '17 at 10:00
  • Cannot convert from object to string CS1503 – Blagovest Pizhev Jan 09 '17 at 10:05
  • You can split it by space then take second elemant. Like that `string[] words = comboBox2.SelectedItem.ToString().Split(' ');` – Aya Aboud Jan 09 '17 at 11:46

1 Answers1

0

If your comboboxes containe DateTime-Values you can format them like this:

DateTime value = (DateTime)comboBox2.SelectedItem;
String valueString = value.ToString("MMMM dd, yyyy");

If you need a differen formationg check out the MSDN Entry

You should really consider using Sql Parameter like @Reniuz suggested. This prevents SQL Injection attack and helps in general by better readability of your code and easier parsing of values

Community
  • 1
  • 1
MadddinTribleD
  • 385
  • 5
  • 13