I have a date column in my database. I have two datetimepickers( past and present) three radio buttons called, retailer, sub dealer and dealer. I want to display all records between the two dates in my datagrid. But first I will set the two dates and pick one radio button and click the search button. I have solved my problem in radiobuttons. I just put "OR" in my query so that it will still even if m problem in getting date between two dates wont. I didn't use datime because I used varchar as the datatype for date in my database. I cant changed it to datetime because thats what my teacher has given me.
Here's my code. Thank u very much.
public static List<Retailer> GetDataByType(string type, string past, string present)
{
List<Retailer> data = new List<Retailer>();
MySqlConnection con = DBConnection.ConnectDatabase();
try
{ // AND
MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + tablename + " WHERE date BETWEEN '" + past + "' AND '" + present + "'" + "' OR type LIKE '%" + type + "%'", con);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Retailer rawData = new Retailer();
rawData.Date = reader.GetString(0);
rawData.Walletid = reader.GetString(1);
rawData.Fname = reader.GetString(2);
rawData.Lname = reader.GetString(3);
rawData.Birthdate = reader.GetString(4);
rawData.Address = reader.GetString(5);
rawData.Province = reader.GetString(6);
rawData.City = reader.GetString(7);
rawData.Balance = reader.GetDouble(8);
rawData.Frozen = reader.GetDouble(9);
rawData.Sponsor_id = reader.GetString(10);
rawData.Share = reader.GetDecimal(11);
rawData.Email = reader.GetString(12);
rawData.Password = reader.GetString(13);
rawData.Type = reader.GetInt32(14);
data.Add(rawData);
MessageBox.Show(rawData.Date);
}
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
return data;
}
}
pastandpresentis the key point here. You need to have them be in the default format of MySQL. However, the optimal solution is to use Parameters and specify the type to be DateTime.