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

Thread: datetimepicker and sql

  1. #1
    Join Date
    Aug 2011
    Posts
    9

    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
    punches
    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
    Posts
    9

    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
    Location
    Washington State
    Posts
    220

    Re: datetimepicker and sql

    Greetings,

    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...

    Code:
                    // 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
    Posts
    9

    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...

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

  5. #5
    Join Date
    Aug 2011
    Posts
    9

    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 online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    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).

  7. #7
    Join Date
    Aug 2011
    Posts
    9

    Re: datetimepicker and sql

    I'll have to give that a shot when I get home from work tomorrow. Its not all bad that I have to start over, because I need the practice making everything and jiving it yet... I have a hunch the earlier suggestion for getting the most basic functionality of my query was working, but I had already done the dammage.... therefor negated it.

    so much for jumping right into it and figuring out what seemed to be something petty...

    I truely am looking forward to understanding sql a lot farther than I am now, I see so many potential uses.... I have issues with working on code at night.... which is when I work.. if I try and solve a problem while I am working, possibly the distractions are too much, or its just a bad location all together.. generally everything I do gets re-written the next morning when I wake up. not to mention how frustrated I get, and then I make matters worse by studying harder on things I dont fully understand yet.... vicious cycle... thanks for hangin in with me.

    -ben

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center