locked
sql date range syntax RRS feed

  • Question

  • User-1387630685 posted

    I got this sql query statement actually working with ASP.net w/ a MS Access db:

    WHERE (Date Between #" & TextBox1.Text & "# AND #" & TextBox2.Text & "#)

    Question is, is it possible to do it somewhat like the following?

    -=-=

    WHERE (Date Between @start AND @end)

    da1.SelectCommand.Parameters.AddWithValue("@start", TextBox1.Text)
    da1.SelectCommand.Parameters.AddWithValue("@end", TextBox2.Text)

    -=-=

    Not sure what difference it makes, but trying to use Parameters.AddWithValue  throws errors on me every which way I have tried to do it. I tried putting in the # marks, etc., in different places, too.

    Biggest part of my SQL statements are done using Parameters.AddWithValue, so I was just trying to stay consistent. But I also don't want to waste a lot of time if I'm trying to do something that is not possible.

    Did some searching, but found nothing. Will bow to the more experienced on this one.


    Thanks



    Friday, December 17, 2010 10:50 AM

Answers

  • User-1083922933 posted

    Hi


    try :

    da1.SelectCommand.Parameters.AddWithValue("@start", (Object)(Convert.ToDateTime(TextBox1.Text)))

    instead of :

    da1.SelectCommand.Parameters.AddWithValue("@start", TextBox1.Text)


    if this helps please mark as an Answer


    good luck

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 17, 2010 12:10 PM

All replies

  • User-1083922933 posted

    Hi


    try :

    da1.SelectCommand.Parameters.AddWithValue("@start", (Object)(Convert.ToDateTime(TextBox1.Text)))

    instead of :

    da1.SelectCommand.Parameters.AddWithValue("@start", TextBox1.Text)


    if this helps please mark as an Answer


    good luck

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 17, 2010 12:10 PM
  • User-1387630685 posted

    Ok, it actually works the way I had it or your way. I'm an idiot and I've managed to do this stupid things twice in about a month..

    I accidentally got the parameters in the wrong order, and that's what the error was from.

    Stupid, stupid, stupid.

    But thanks for your help.




    Friday, December 17, 2010 4:02 PM