CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Aug 2011

    Question datetimepicker and sql

    .NET 4.0

    I have a simple between two datetimepicker query I am trying to implement. Could someone explain to me very simply without a parametrized query debate, how to make a datetimepicker's value appear in my query?

    SELECT punches.Number, punches.Punchdate, employees.Name
    FROM employees CROSS JOIN
    WHERE punches.PunchDate BETWEEN dateTimePicker1.Value AND dateTimePicker2.Value

    I realize that this is not the correct formatting.. I have tried quotes, @'s, .ToSmallDateString,DateTime.Parse(), and pretty much everything that I have been able to find on google... yet still I still get errors.. I realize how noob of a question this is...

    I also realize I cancel the rules of gravity for not using parametrized input on even the simplest of things.. However, I need baby steps first so I can get a better grasp of how to handle things first... Any input would be appreciated..
    Last edited by HackedGadget; August 24th, 2011 at 01:50 AM.

  2. #2
    Join Date
    Aug 2011

    Re: datetimepicker and sql

    nobody will touch this with a ten foot pole I see... lol oh well, on to the books again for me...

  3. #3
    Join Date
    May 2011
    Washington State

    Re: datetimepicker and sql


    While I have not used the BETWEEN or been in SQL (oracle dates act a little differently)... this is one way to handle query string manipulation... to construct your query, you could use the String.Format() method and 'insert' the date values this way...

                    // query text without the date parameters, just place holders...
                    string rawQuery = "SELECT punches.Number, punches.Punchdate, employees.Name FROM employees CROSS JOIN punches WHERE punches.PunchDate BETWEEN '{0}' AND '{1}'";
                    // throw your date values into the placeholders to produce your executing query...
                    string filledQuery = String.Format(rawQuery, DateTime.Today.ToShortDateString(), DateTime.Today.ToShortDateString());
    (of course, using yor datepicker values rather than DateTime.Today....)

  4. #4
    Join Date
    Aug 2011

    Re: datetimepicker and sql

    Thanks! I found several problems I had with my approach. First was that I was using the Query builder tool.. Which appears to insert all the quotes itself, so I was not able to isolate the values to concatenate with the string, second was a big duh. I had no "using system.data.sqlClient" because the query builder did not need it. that settled most of the problems.. It looks like I am running into format problems, which I will situate today. I appreciate that you where willing to give the answer to the question asked, without a lecture on security... Once I get the minor details worked out and fully understand what I am working with, and what both languages want.. I can then upgrade to parametrized statements. Thanks again!
    ill update with a result once I get that far.

    Current Problem... the path is not of legal form...

    Any books anyone knows of that deal with SQL and C# together?

    after further investigation... it looks like I overwrote my DB somehow? leave it to me to screw it all up this bad... and I cant get rid of all the linking to start over... what a nightmare...

    Last edited by HackedGadget; August 24th, 2011 at 03:41 PM.

  5. #5
    Join Date
    Aug 2011

    Re: datetimepicker and sql

    How about any books on C# that will give me the knowledge to put the two together?

    I have read: Sam's Learn SQL in 10 minutes, which actually takes a few hours.
    C# for dummies, which is pretty accurate for me lmao, however I assume that it leaves alot of important things out

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: datetimepicker and sql

    Here's what I do to get the SQL things working.

    1) I get the query working in Sql Management studio.
    2) I then form the same query in C#.

    I use breakpoints and step through my code in the debugger (press F5 to start debugging).

    That way I can look at the exact sql string and see if it's formed correctly (and matches the one that I tested in the sql management console).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.