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
  1. Home
  2. General Programming
  3. C#
  4. Can anyone help me take a look at my code?

Can anyone help me take a look at my code?

Scheduled Pinned Locked Moved C#
questioncsharpdatabasehelp
10 Posts 3 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.
  • M Offline
    M Offline
    momoo
    wrote on last edited by
    #1

    Hi.. I would like to ask about C# web application.I have created a calendar. I would like to ask that if i click the date on the calendar,how can i retrieve the database ?? I have tried butit seem like is not working. Below is the code, could anyone help me with it as i'm totally lost?? Thanks!! The code: private void Radcalendar2_SelectionChanged(object sender, System.EventArgs e) { TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); } I just want to retrieve(view) the user's login/logout time of date we have click on the calendar. Anyone who can help me..You can also edit from here or a new code... Thanks!! :-D Cheers!

    V J 2 Replies Last reply
    0
    • M momoo

      Hi.. I would like to ask about C# web application.I have created a calendar. I would like to ask that if i click the date on the calendar,how can i retrieve the database ?? I have tried butit seem like is not working. Below is the code, could anyone help me with it as i'm totally lost?? Thanks!! The code: private void Radcalendar2_SelectionChanged(object sender, System.EventArgs e) { TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); } I just want to retrieve(view) the user's login/logout time of date we have click on the calendar. Anyone who can help me..You can also edit from here or a new code... Thanks!! :-D Cheers!

      V Offline
      V Offline
      Vikram A Punathambekar
      wrote on last edited by
      #2

      I'm sorry to say this, but your question is not very clear. Is English not your native language? Here's what I *think* the problem is: If you have a DateTime value, do NOT use the ToLongDateString() method as it formats the date depending on the user's Windows settings. Rather, use DateTime.ToString("yyyy-MM-dd") or whatever format the database accepts the date string in. HTH. :) Cheers, Vikram.


      "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.

      M 1 Reply Last reply
      0
      • V Vikram A Punathambekar

        I'm sorry to say this, but your question is not very clear. Is English not your native language? Here's what I *think* the problem is: If you have a DateTime value, do NOT use the ToLongDateString() method as it formats the date depending on the user's Windows settings. Rather, use DateTime.ToString("yyyy-MM-dd") or whatever format the database accepts the date string in. HTH. :) Cheers, Vikram.


        "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.

        M Offline
        M Offline
        momoo
        wrote on last edited by
        #3

        Hi, Vikram.. Thanks for replying.. Actually, maybe what i explained is not very clear to you.. But actually, what i wants to do is that i've create a calendar and what i need to do now is that when i click on the date i've select it will show out the database of the user.. Is it clearer for you. As you've mention earlier on that i shouldn't write the ToLongDateString() if i using DateTime.ToString().. Can i ask you, then now how should i write the code so that it'll show out the database when i click on the date. Thanks!! Cheers!

        V 1 Reply Last reply
        0
        • M momoo

          Hi, Vikram.. Thanks for replying.. Actually, maybe what i explained is not very clear to you.. But actually, what i wants to do is that i've create a calendar and what i need to do now is that when i click on the date i've select it will show out the database of the user.. Is it clearer for you. As you've mention earlier on that i shouldn't write the ToLongDateString() if i using DateTime.ToString().. Can i ask you, then now how should i write the code so that it'll show out the database when i click on the date. Thanks!! Cheers!

          V Offline
          V Offline
          Vikram A Punathambekar
          wrote on last edited by
          #4

          saddies wrote:

          what i need to do now is that when i click on the date i've select it will show out the database of the user

          What do you mean by 'database of the user'? Let's say you want to retrieve all the details of employees who joined on a certain date, from a table Employee. Assume that the user chose the date 01 Sept 2004, and that it is contained in the DateTime value userDate. Your query should look like:

          string selectQuery = "SELECT * FROM Employees WHERE JoiningDate='" + userDate.ToString("yyyy-MM-dd") + "'";

          Of course, the details will differ in your application, but you should get the general idea. :) Cheers, Vikram.


          "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.

          J 1 Reply Last reply
          0
          • V Vikram A Punathambekar

            saddies wrote:

            what i need to do now is that when i click on the date i've select it will show out the database of the user

            What do you mean by 'database of the user'? Let's say you want to retrieve all the details of employees who joined on a certain date, from a table Employee. Assume that the user chose the date 01 Sept 2004, and that it is contained in the DateTime value userDate. Your query should look like:

            string selectQuery = "SELECT * FROM Employees WHERE JoiningDate='" + userDate.ToString("yyyy-MM-dd") + "'";

            Of course, the details will differ in your application, but you should get the general idea. :) Cheers, Vikram.


            "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Well done, the OP was actually using paramaterized queries and you just taught him how to introduce Sql Injection attacks to his code.

            V 1 Reply Last reply
            0
            • M momoo

              Hi.. I would like to ask about C# web application.I have created a calendar. I would like to ask that if i click the date on the calendar,how can i retrieve the database ?? I have tried butit seem like is not working. Below is the code, could anyone help me with it as i'm totally lost?? Thanks!! The code: private void Radcalendar2_SelectionChanged(object sender, System.EventArgs e) { TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); } I just want to retrieve(view) the user's login/logout time of date we have click on the calendar. Anyone who can help me..You can also edit from here or a new code... Thanks!! :-D Cheers!

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              Hi, I can see from your code that when a user selects a date, you display it to him apsolutely 100% perfectly!! It will be formatted according to HIS/HER windows settings This line is perfect! TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); However, take 1 step backwards: DateTime selectedDateTime = CAL1.SelectedDate; Without the .ToLongDateString() call, you have a DateTime Object. The only place you went wrong below is that you tried to parse the string back into a Date, which at best is unnecessary and at worst will not work. SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); The beauty of parameters (especially date ones) is that the ADO.NET provider will take care of any conversion for you... so change the last line to: cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = CAL1.SelectedDate; simple, huh?

              M 1 Reply Last reply
              0
              • J J4amieC

                Well done, the OP was actually using paramaterized queries and you just taught him how to introduce Sql Injection attacks to his code.

                V Offline
                V Offline
                Vikram A Punathambekar
                wrote on last edited by
                #7

                Jamie, I took a look at your reply below and it's interesting - I didn't know that parameters are automatically converted at runtime. Thanks! But what do you mean by 'introducing SQL injection attacks'? :confused: Cheers, Vikram.


                "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.

                J 1 Reply Last reply
                0
                • V Vikram A Punathambekar

                  Jamie, I took a look at your reply below and it's interesting - I didn't know that parameters are automatically converted at runtime. Thanks! But what do you mean by 'introducing SQL injection attacks'? :confused: Cheers, Vikram.


                  "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  Sorry, how stoopid of me to not link - especially with a good article right here http://www.codeproject.com/cs/database/SqlInjectionAttacks.asp[^]

                  V 1 Reply Last reply
                  0
                  • J J4amieC

                    Sorry, how stoopid of me to not link - especially with a good article right here http://www.codeproject.com/cs/database/SqlInjectionAttacks.asp[^]

                    V Offline
                    V Offline
                    Vikram A Punathambekar
                    wrote on last edited by
                    #9

                    Thanks, Jamie. Glad to have learnt something useful today. :) Cheers, Vikram.


                    "When I read in books about a "base class", I figured this was the class that was at the bottom of the inheritence tree. It's the "base", right? Like the base of a pyramid." - Marc Clifton.

                    1 Reply Last reply
                    0
                    • J J4amieC

                      Hi, I can see from your code that when a user selects a date, you display it to him apsolutely 100% perfectly!! It will be formatted according to HIS/HER windows settings This line is perfect! TBDate1.Text = CAL1.SelectedDate.ToLongDateString(); However, take 1 step backwards: DateTime selectedDateTime = CAL1.SelectedDate; Without the .ToLongDateString() call, you have a DateTime Object. The only place you went wrong below is that you tried to parse the string back into a Date, which at best is unnecessary and at worst will not work. SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(TBDate1.Text); The beauty of parameters (especially date ones) is that the ADO.NET provider will take care of any conversion for you... so change the last line to: cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = CAL1.SelectedDate; simple, huh?

                      M Offline
                      M Offline
                      momoo
                      wrote on last edited by
                      #10

                      Hi,J4amieC.. Below is my code which i have changed.. But somehow when i run the program, the database does not show out when i click on the date. Is there's anything wrong or i still missing on some part?? DateTime selectedDateTime = CAL1.SelectedDate; SqlConnection conn=new SqlConnection("my connection string"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Particulars WHERE DateField = @Date"; cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = CAL1.SelectedDate; Thanks for your help!!:) Regards

                      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