Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi am a newbie to this and looking for assistance.

the following code applies to a where clause to a query whereby a post request exists.
Everything is ok up until filtering by date.
the field date_deliv_required is set to datetime in the database. variable datereq is a string in format yyyy-mm-dd.
date_deliv_required outputs as yyyy-mm-dd h:i:s (I think)

How would one this type of SQL query

SQL
select * from table where date_deliv_required = 'yyyy-mm-dd' 


with the below code:


C#
public ActionResult Index(string zone, string datereq)
        {
            var deliverylists = db.deliverylists.Include(d => d.branch);

            if (!String.IsNullOrEmpty(zone))
            {
                deliverylists = deliverylists.Where(d => d.id_sales_zone.Equals(zone));
            }
           
            //problem zone start
            if (!String.IsNullOrEmpty(datereq))
            {

                deliverylists = deliverylists.Where(d => d.date_deliv_required.Equals(datereq));
            }
            //problem zone end
            
            return View(deliverylists.ToList());
        }
Posted

Personally, I'd use one of the technologies .Net offers for passing parameters into database queries. Use of parameters ensures that things like date formats and strings are handled correctly; they are also essential for preventing SQL injection attacks when your filter values are captured as text from a user.

Here's an old-school approach, Linq and Entity Framework offer ways to do it that are more OO but can hide what's going on between your app and the database (not always a problem, but it doesn't help when you're trying to learn concepts):

C#
public static void DoStuff()
{
    const string CONNECTION = "write me - the connection string";
    const string SQL = "SELECT * "+
                        " FROM table "+
                        " WHERE date_deliv_required = @DateDelivered";

    var dateDelivered = Convert.ToDateTime("01/08/2008");

    DataTable dt = new DataTable();
    using (var conn = new SqlConnection(CONNECTION))
    {
        using (var cmd = new SqlCommand(SQL, conn))
        {
            conn.Open();
            cmd.Parameters.AddWithValue("@DateDelivered", dateDelivered);
            cmd.CommandType = CommandType.Text;
            using (var ada = new SqlDataAdapter(cmd))
            {
                ada.Fill(dt);
            }
        }
    }

    // Do stuff with the rows in the data table dt...
}
 
Share this answer
 
string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date);
int month =dt.month;
int year=dt.year;
int days=dt.days;

SQL
select * from table where MONTH(date_deliv_required)=month and YEAR(date_deliv_required)=year and DAY(date_deliv_required)=days


Try this,
 
Share this answer
 
v2
Compare by converting into Datetime with the required format.

var dateToCompare = DateTime.ParseExact(datereq, "yyyy-MM-dd", CultureInfo.InvariantCulture);


deliverylists = deliverylists.Where(d => DateTime.Compare(d.date_deliv_required.Date, dateToCompare .Date)  <= 0);


Hope this helps...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900