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

PLEASE HELP

Scheduled Pinned Locked Moved C#
databasesql-serversysadminhelpquestion
8 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.
  • I Offline
    I Offline
    innocent73
    wrote on last edited by
    #1

    Hello Folks, I really need help to solve a real stupid situation I have a SQL Server database table named MAINT_START.Columns of this table are: PlateNumber char(11)--------> PK MaintEntryDate smalldatetime---> PK MaintReason char(20) I want to update this table's MaintReason column via a SQL String. My code is like this: /********************************************************************** string strUpdate="UPDATE MAINT_START SET MaintReason='" + cMaintReason.Text.TrimEnd() + "'," + " MaintCompany='" + cMaintCompany.Text.TrimEnd() + "'," + " MaintCompanyTel='" + cMaintCompanyTel.Text.TrimEnd() + "'," + " MaintCompanyAddress='" + cMaintCompanyAddress.Text.TrimEnd() + "'" + " WHERE PlateNumber='" + cPlateNumber.Text + "' AND" + " MaintEntryDate =Convert.ToDateTime(cMaintEntry.Text)"; SqlCommand aCommand=new SqlCommand(strUpdate,Myconnection); /************************************************************************ Dear friends is it really impossible to insert or update a smalldatetime column? Please Help..

    C N 2 Replies Last reply
    0
    • I innocent73

      Hello Folks, I really need help to solve a real stupid situation I have a SQL Server database table named MAINT_START.Columns of this table are: PlateNumber char(11)--------> PK MaintEntryDate smalldatetime---> PK MaintReason char(20) I want to update this table's MaintReason column via a SQL String. My code is like this: /********************************************************************** string strUpdate="UPDATE MAINT_START SET MaintReason='" + cMaintReason.Text.TrimEnd() + "'," + " MaintCompany='" + cMaintCompany.Text.TrimEnd() + "'," + " MaintCompanyTel='" + cMaintCompanyTel.Text.TrimEnd() + "'," + " MaintCompanyAddress='" + cMaintCompanyAddress.Text.TrimEnd() + "'" + " WHERE PlateNumber='" + cPlateNumber.Text + "' AND" + " MaintEntryDate =Convert.ToDateTime(cMaintEntry.Text)"; SqlCommand aCommand=new SqlCommand(strUpdate,Myconnection); /************************************************************************ Dear friends is it really impossible to insert or update a smalldatetime column? Please Help..

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      innocent73 wrote:

      Dear friends is it really impossible to insert or update a smalldatetime column?

      It is perfectly possible - But first you may want to address some deeper concerns. You should use parameterised queries for a number of reasons the main one is that it is more secure and less susceptable to a SQL Injection Attack (your code is highly suscetable to a SQL Injection Attack). Also it makes things easier to read. See: SQL Injection Attacks and Tips on How to Prevent Them[^]

      innocent73 wrote:

      " MaintEntryDate =Convert.ToDateTime(cMaintEntry.Text)";

      SQL server knows nothing of Convert.ToDateTime(cMaintEntry.Text) because that is C# code. If you continue in the way you have written your code so far, you just need to inject the text without conversion (but you have to assume that the text is in the correct format already). Alternatively you can pass a date in as a parameter (see the above linked article to see how) and secure your system at the same time.


      Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

      I 1 Reply Last reply
      0
      • C Colin Angus Mackay

        innocent73 wrote:

        Dear friends is it really impossible to insert or update a smalldatetime column?

        It is perfectly possible - But first you may want to address some deeper concerns. You should use parameterised queries for a number of reasons the main one is that it is more secure and less susceptable to a SQL Injection Attack (your code is highly suscetable to a SQL Injection Attack). Also it makes things easier to read. See: SQL Injection Attacks and Tips on How to Prevent Them[^]

        innocent73 wrote:

        " MaintEntryDate =Convert.ToDateTime(cMaintEntry.Text)";

        SQL server knows nothing of Convert.ToDateTime(cMaintEntry.Text) because that is C# code. If you continue in the way you have written your code so far, you just need to inject the text without conversion (but you have to assume that the text is in the correct format already). Alternatively you can pass a date in as a parameter (see the above linked article to see how) and secure your system at the same time.


        Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

        I Offline
        I Offline
        innocent73
        wrote on last edited by
        #3

        Firstly I would like to say thank you for you attention. I already tried to get success result via a stored procedure. And unfortunately the result was not ok.Here is my stored procedure: /**************************************************************** CREATE PROCEDURE [MaintStartUpdate] (@PlateNumber_1 [char], @MaintEntryDate_2 [smalldatetime], @MaintReason_5 [char](20), @MaintCompany_6 [char](20), @MaintCompanyTel_7 [char](11), @MaintCompanyAddress_8 [nvarchar](50)) AS UPDATE [CollLogistics].[dbo].[MAINT_START] SET [MaintReason] = @MaintReason_5, [MaintCompany] = @MaintCompany_6, [MaintCompanyTel] = @MaintCompanyTel_7, [MaintCompanyAddress] = @MaintCompanyAddress_8 WHERE PlateNumber = @PlateNumber_1 AND [MaintEntryDate]= @MaintEntryDate_2 GO /************************************************************** I really want to crash my computer right now. Because this is a very stupid and easy SQL update...And I still could not do anything!

        C 1 Reply Last reply
        0
        • I innocent73

          Firstly I would like to say thank you for you attention. I already tried to get success result via a stored procedure. And unfortunately the result was not ok.Here is my stored procedure: /**************************************************************** CREATE PROCEDURE [MaintStartUpdate] (@PlateNumber_1 [char], @MaintEntryDate_2 [smalldatetime], @MaintReason_5 [char](20), @MaintCompany_6 [char](20), @MaintCompanyTel_7 [char](11), @MaintCompanyAddress_8 [nvarchar](50)) AS UPDATE [CollLogistics].[dbo].[MAINT_START] SET [MaintReason] = @MaintReason_5, [MaintCompany] = @MaintCompany_6, [MaintCompanyTel] = @MaintCompanyTel_7, [MaintCompanyAddress] = @MaintCompanyAddress_8 WHERE PlateNumber = @PlateNumber_1 AND [MaintEntryDate]= @MaintEntryDate_2 GO /************************************************************** I really want to crash my computer right now. Because this is a very stupid and easy SQL update...And I still could not do anything!

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          innocent73 wrote:

          And unfortunately the result was not ok.

          What does that mean? How was the result "not ok"? What should it do and what does it do? Some possible area to check:

          innocent73 wrote:

          (@PlateNumber_1 [char],

          Be explicit - how many characters are in this parameter?

          innocent73 wrote:

          [char](20),

          Are you sure a char(20) is what you want? If you supply a value like "hello" it will append 15 spaces to make up to the full size.


          Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

          I 1 Reply Last reply
          0
          • C Colin Angus Mackay

            innocent73 wrote:

            And unfortunately the result was not ok.

            What does that mean? How was the result "not ok"? What should it do and what does it do? Some possible area to check:

            innocent73 wrote:

            (@PlateNumber_1 [char],

            Be explicit - how many characters are in this parameter?

            innocent73 wrote:

            [char](20),

            Are you sure a char(20) is what you want? If you supply a value like "hello" it will append 15 spaces to make up to the full size.


            Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

            I Offline
            I Offline
            innocent73
            wrote on last edited by
            #5

            I am really sorry for my bad english.. The question is I think very simple? How can I send a smalldate parameter from c# to SQL server. That's all!

            C 1 Reply Last reply
            0
            • I innocent73

              Hello Folks, I really need help to solve a real stupid situation I have a SQL Server database table named MAINT_START.Columns of this table are: PlateNumber char(11)--------> PK MaintEntryDate smalldatetime---> PK MaintReason char(20) I want to update this table's MaintReason column via a SQL String. My code is like this: /********************************************************************** string strUpdate="UPDATE MAINT_START SET MaintReason='" + cMaintReason.Text.TrimEnd() + "'," + " MaintCompany='" + cMaintCompany.Text.TrimEnd() + "'," + " MaintCompanyTel='" + cMaintCompanyTel.Text.TrimEnd() + "'," + " MaintCompanyAddress='" + cMaintCompanyAddress.Text.TrimEnd() + "'" + " WHERE PlateNumber='" + cPlateNumber.Text + "' AND" + " MaintEntryDate =Convert.ToDateTime(cMaintEntry.Text)"; SqlCommand aCommand=new SqlCommand(strUpdate,Myconnection); /************************************************************************ Dear friends is it really impossible to insert or update a smalldatetime column? Please Help..

              N Offline
              N Offline
              Nader Elshehabi
              wrote on last edited by
              #6

              I'm not really that experienced in SQL, but... We all know that queries are only text, so if you want to pass a date to SQL server, you should pass it as a text. And why not? I believe it would be something like: string strUpdate="UPDATE MAINT_START SET MaintReason='" + cMaintReason.Text.TrimEnd() + "'," + " MaintCompany='" + cMaintCompany.Text.TrimEnd() + "'," + " MaintCompanyTel='" + cMaintCompanyTel.Text.TrimEnd() + "'," + " MaintCompanyAddress='" + cMaintCompanyAddress.Text.TrimEnd() + "'" + " WHERE PlateNumber='" + cPlateNumber.Text + "' AND" + " MaintEntryDate =" DateTime.Parse(cMaintEntry.Text).ToString(); Did you notice the Parse-ToString way? Another way if you really want to pass it as a datetime -I really can't see why the persistence:)- perhaps you should consider using Typed datasets rather than SQLCommands?? Just a suggestion. I hope that was close enough;)

              C 1 Reply Last reply
              0
              • I innocent73

                I am really sorry for my bad english.. The question is I think very simple? How can I send a smalldate parameter from c# to SQL server. That's all!

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                innocent73 wrote:

                How can I send a smalldate parameter from c# to SQL server

                DateTime theDate = new DateTime(2006,06,19);
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "SELECT * FROM MyTable Where [date] = @someDate";
                cmd.Parameters.Add("@someDate", theDate);
                ...


                Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                1 Reply Last reply
                0
                • N Nader Elshehabi

                  I'm not really that experienced in SQL, but... We all know that queries are only text, so if you want to pass a date to SQL server, you should pass it as a text. And why not? I believe it would be something like: string strUpdate="UPDATE MAINT_START SET MaintReason='" + cMaintReason.Text.TrimEnd() + "'," + " MaintCompany='" + cMaintCompany.Text.TrimEnd() + "'," + " MaintCompanyTel='" + cMaintCompanyTel.Text.TrimEnd() + "'," + " MaintCompanyAddress='" + cMaintCompanyAddress.Text.TrimEnd() + "'" + " WHERE PlateNumber='" + cPlateNumber.Text + "' AND" + " MaintEntryDate =" DateTime.Parse(cMaintEntry.Text).ToString(); Did you notice the Parse-ToString way? Another way if you really want to pass it as a datetime -I really can't see why the persistence:)- perhaps you should consider using Typed datasets rather than SQLCommands?? Just a suggestion. I hope that was close enough;)

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  Nader Elshehabi wrote:

                  We all know that queries are only text, so if you want to pass a date to SQL server, you should pass it as a text. And why not?

                  I'll tell you why not: Because you open up the application to explotation by attackers. When you start injecting bits of text into SQL Strings you give the attacker an opening that they can use to create a bigger hole so that they can gain access to your system. See the article I refered to in my initial reply: SQL Injection Attacks and Tips on How To Prevent Them[^]


                  Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                  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