Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. string is not recognized

string is not recognized

Scheduled Pinned Locked Moved C#
databasecsharphelp
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rabia_Arif

    hello, I am using Visual c# 2005 and sql server2005 In my project I have to use the next date from the current system and have to use it in the where clause of sql query string for that i did the following:

    string day = (DateTime.Now.AddDays(1).Day.ToString());
    string month = (DateTime.Now.Month.ToString());
    string year = (DateTime.Now.Year.ToString());
    string date_nextDay = month + '/' + day + '/' + year;

    then in sql query my code looks like this:

    strQueryString = "select * from view_reservation where date =" + date_nextDay.ToString();

    but the problem is sql is not recognizing it although there are some rows in database and whenever I just write the next day date in single quotes by myself it works correctly.Dont kno what to do

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #2
    1. You need apostrophes if you do it that way. 1) Why not use System.DateTime.Now.AddDays(1).ToString( "MM'/'dd'/'yyyy" ) ; ? 2) Use a parameterized query and pass in the DateTime rather than making a string that will then have to be parsed.
    1 Reply Last reply
    0
    • R Rabia_Arif

      hello, I am using Visual c# 2005 and sql server2005 In my project I have to use the next date from the current system and have to use it in the where clause of sql query string for that i did the following:

      string day = (DateTime.Now.AddDays(1).Day.ToString());
      string month = (DateTime.Now.Month.ToString());
      string year = (DateTime.Now.Year.ToString());
      string date_nextDay = month + '/' + day + '/' + year;

      then in sql query my code looks like this:

      strQueryString = "select * from view_reservation where date =" + date_nextDay.ToString();

      but the problem is sql is not recognizing it although there are some rows in database and whenever I just write the next day date in single quotes by myself it works correctly.Dont kno what to do

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #3

      strQueryString = string.Format("select * from view_reservation where date = '{0:M/d/yy}'", DateTime.Today.AddDays(1)); Note that the {0:...} part is using curly brackets... string.Format will replace that part with the first additional parameter (Parameter #0), using the format specified. So basically, it'll take Today + 1, format it in the "M/d/yy" form (ex. 9/23/09), and stick it into the string in the right spot. Also note the single quotes around it... Most databases will require you to put dates inside single quotes.

      Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

      P 1 Reply Last reply
      0
      • R Rabia_Arif

        hello, I am using Visual c# 2005 and sql server2005 In my project I have to use the next date from the current system and have to use it in the where clause of sql query string for that i did the following:

        string day = (DateTime.Now.AddDays(1).Day.ToString());
        string month = (DateTime.Now.Month.ToString());
        string year = (DateTime.Now.Year.ToString());
        string date_nextDay = month + '/' + day + '/' + year;

        then in sql query my code looks like this:

        strQueryString = "select * from view_reservation where date =" + date_nextDay.ToString();

        but the problem is sql is not recognizing it although there are some rows in database and whenever I just write the next day date in single quotes by myself it works correctly.Dont kno what to do

        A Offline
        A Offline
        Ashfield
        wrote on last edited by
        #4

        You have alread had the correct answers, about using quotes or better still parameterised query, but you will not always get tomorrow - you add 1 to the day part only. What happens on the last day of the month (or year) - 31st December 2009 will become 1st December 2009 using your code.

        Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

        D 1 Reply Last reply
        0
        • A Ashfield

          You have alread had the correct answers, about using quotes or better still parameterised query, but you will not always get tomorrow - you add 1 to the day part only. What happens on the last day of the month (or year) - 31st December 2009 will become 1st December 2009 using your code.

          Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #5

          He's using the AddDays method so that's taken care of by the framework - so long as he doesn't try to add to DateTime.MaxValue he'll be OK.

          Dave
          Generic BackgroundWorker - My latest article!
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          P A 2 Replies Last reply
          0
          • D DaveyM69

            He's using the AddDays method so that's taken care of by the framework - so long as he doesn't try to add to DateTime.MaxValue he'll be OK.

            Dave
            Generic BackgroundWorker - My latest article!
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #6

            Only for the day; not for the month and year.

            L 1 Reply Last reply
            0
            • I Ian Shlasko

              strQueryString = string.Format("select * from view_reservation where date = '{0:M/d/yy}'", DateTime.Today.AddDays(1)); Note that the {0:...} part is using curly brackets... string.Format will replace that part with the first additional parameter (Parameter #0), using the format specified. So basically, it'll take Today + 1, format it in the "M/d/yy" form (ex. 9/23/09), and stick it into the string in the right spot. Also note the single quotes around it... Most databases will require you to put dates inside single quotes.

              Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #7

              In the format for a DateTime, the '/' doesn't mean '/', it means to use the date separator that's set in the operating system, which could be '-' (as it is on my system) or even '.'. ISO 8601 specifies the use of '-'. Control panel | Regional and Language Options | Regional Options | Customize | Date | Date separator

              1 Reply Last reply
              0
              • P PIEBALDconsult

                Only for the day; not for the month and year.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                :confused: right now DateTime.Now.AddDays(365+30+1).ToString() returns 25-Oct-2010 1:11:42 on my system.

                Luc Pattyn


                Local announcement (Antwerp region): Lange Wapper? Neen!


                P 1 Reply Last reply
                0
                • L Luc Pattyn

                  :confused: right now DateTime.Now.AddDays(365+30+1).ToString() returns 25-Oct-2010 1:11:42 on my system.

                  Luc Pattyn


                  Local announcement (Antwerp region): Lange Wapper? Neen!


                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #9

                  The OP added a day when accessing the day, but not when accessing the month and year.

                  L 1 Reply Last reply
                  0
                  • D DaveyM69

                    He's using the AddDays method so that's taken care of by the framework - so long as he doesn't try to add to DateTime.MaxValue he'll be OK.

                    Dave
                    Generic BackgroundWorker - My latest article!
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Why are you using VB6? Do you hate yourself? (Christian Graus)

                    A Offline
                    A Offline
                    Ashfield
                    wrote on last edited by
                    #10

                    But he is only adding to the day variable:

                    // assume today is 30 Sep 2009 (30/09/2009)
                    string day = (DateTime.Now.AddDays(1).Day.ToString()); // day = 1
                    string month = (DateTime.Now.Month.ToString()); // month = 9
                    string year = (DateTime.Now.Year.ToString()); // year = 2009
                    string date_nextDay = month + '/' + day + '/' + year; // gives 9/1/2009

                    Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                    D 1 Reply Last reply
                    0
                    • A Ashfield

                      But he is only adding to the day variable:

                      // assume today is 30 Sep 2009 (30/09/2009)
                      string day = (DateTime.Now.AddDays(1).Day.ToString()); // day = 1
                      string month = (DateTime.Now.Month.ToString()); // month = 9
                      string year = (DateTime.Now.Year.ToString()); // year = 2009
                      string date_nextDay = month + '/' + day + '/' + year; // gives 9/1/2009

                      Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #11

                      Ah... I saw the AddDays method and assumed that he would be using the returned DateTime instance so didn't look any more at the code. Using three DateTime instances to create the string representations of a date's constituent parts is a WTF that I wasn't expecting!

                      Dave
                      Generic BackgroundWorker - My latest article!
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                      Why are you using VB6? Do you hate yourself? (Christian Graus)

                      L A 2 Replies Last reply
                      0
                      • P PIEBALDconsult

                        The OP added a day when accessing the day, but not when accessing the month and year.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #12

                        You're right of course. And he shouldn't use multiple DateTime.Now calls anyway, it may fail around midnight. :)

                        Luc Pattyn


                        Local announcement (Antwerp region): Lange Wapper? Neen!


                        1 Reply Last reply
                        0
                        • D DaveyM69

                          Ah... I saw the AddDays method and assumed that he would be using the returned DateTime instance so didn't look any more at the code. Using three DateTime instances to create the string representations of a date's constituent parts is a WTF that I wasn't expecting!

                          Dave
                          Generic BackgroundWorker - My latest article!
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                          Why are you using VB6? Do you hate yourself? (Christian Graus)

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #13

                          DaveyM69 wrote:

                          DateTime instances

                          WTF!

                          Luc Pattyn


                          Local announcement (Antwerp region): Lange Wapper? Neen!


                          1 Reply Last reply
                          0
                          • D DaveyM69

                            Ah... I saw the AddDays method and assumed that he would be using the returned DateTime instance so didn't look any more at the code. Using three DateTime instances to create the string representations of a date's constituent parts is a WTF that I wasn't expecting!

                            Dave
                            Generic BackgroundWorker - My latest article!
                            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                            Why are you using VB6? Do you hate yourself? (Christian Graus)

                            A Offline
                            A Offline
                            Ashfield
                            wrote on last edited by
                            #14

                            DaveyM69 wrote:

                            I saw the AddDays method and assumed that he would be using the returned DateTime instance

                            So did I the first time I read it - just because thats the simple way to get it right :)

                            Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                            R 1 Reply Last reply
                            0
                            • A Ashfield

                              DaveyM69 wrote:

                              I saw the AddDays method and assumed that he would be using the returned DateTime instance

                              So did I the first time I read it - just because thats the simple way to get it right :)

                              Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                              R Offline
                              R Offline
                              Rabia_Arif
                              wrote on last edited by
                              #15

                              Oh thankyou all 4 sharing your ideas.My problem is solved:) thanks a lot

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups