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. Converting Linq to Sql Code (SOLVED)

Converting Linq to Sql Code (SOLVED)

Scheduled Pinned Locked Moved C#
csharpdatabasesql-serverdotnet
6 Posts 3 Posters 1 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.
  • S Offline
    S Offline
    samflex
    wrote on last edited by
    #1

    Greetings experts, The following is a linq to sql code that I am trying to convert to for lack of a better work, regular .net framework code:

    protected void updateSal_Click(object sender, EventArgs e)
    {
    newTotal.Text = theLinq.Salary.Single(s => s.Salary_Year == DateTime.Now.Year).Total_Salary.ToString();
    }

    '//This is my attempted conversion to vb.net

        Protected Sub updateSal\_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim rowsAffected As Integer
            Dim currYear As Integer = Date.Now.Year
                Using con As New SqlConnection("server=.;database=Test;integrated security=true")
             Using cmd As New SqlCommand("UPDATE Salary Set  Total\_Salary=@totsal WHERE Salary\_Year = @currentYear", con)
                   cmd.Parameters.Add("@totsal", SqlDbType.Decimal).Value = newTotal.Text
                   cmd.Parameters.Add("@currentYear", SqlDbType.Integer).Value = currYear
                   con.Open()
                   rowsAffected = cmd.ExecuteNonQuery()
                End Using
               End Using
         End Sub
    

    My result is coming up empty even though the update code works when I run it in SSMS. Am I doing the conversion incorrectly.

    D 1 Reply Last reply
    0
    • S samflex

      Greetings experts, The following is a linq to sql code that I am trying to convert to for lack of a better work, regular .net framework code:

      protected void updateSal_Click(object sender, EventArgs e)
      {
      newTotal.Text = theLinq.Salary.Single(s => s.Salary_Year == DateTime.Now.Year).Total_Salary.ToString();
      }

      '//This is my attempted conversion to vb.net

          Protected Sub updateSal\_Click(ByVal sender As Object, ByVal e As EventArgs)
              Dim rowsAffected As Integer
              Dim currYear As Integer = Date.Now.Year
                  Using con As New SqlConnection("server=.;database=Test;integrated security=true")
               Using cmd As New SqlCommand("UPDATE Salary Set  Total\_Salary=@totsal WHERE Salary\_Year = @currentYear", con)
                     cmd.Parameters.Add("@totsal", SqlDbType.Decimal).Value = newTotal.Text
                     cmd.Parameters.Add("@currentYear", SqlDbType.Integer).Value = currYear
                     con.Open()
                     rowsAffected = cmd.ExecuteNonQuery()
                  End Using
                 End Using
           End Sub
      

      My result is coming up empty even though the update code works when I run it in SSMS. Am I doing the conversion incorrectly.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      The LINQ code is not an UPDATE query. It's a SELECT, and one that doesn't make much sense. The LINQ query is selecting a single record from a possible list of records where Salary_Year matches the current year. The returned record better have a Total_Salary field to return a string for.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      S 1 Reply Last reply
      0
      • D Dave Kreskowiak

        The LINQ code is not an UPDATE query. It's a SELECT, and one that doesn't make much sense. The LINQ query is selecting a single record from a possible list of records where Salary_Year matches the current year. The returned record better have a Total_Salary field to return a string for.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        S Offline
        S Offline
        samflex
        wrote on last edited by
        #3

        WOW! But you can understand why I was confused. The method says Update(...) Thank you.

        D L 2 Replies Last reply
        0
        • S samflex

          WOW! But you can understand why I was confused. The method says Update(...) Thank you.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          The thing is, what is the method updating? In this case, it looks like it's updating the UI, not the database.

          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
          Dave Kreskowiak

          S 1 Reply Last reply
          0
          • D Dave Kreskowiak

            The thing is, what is the method updating? In this case, it looks like it's updating the UI, not the database.

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
            Dave Kreskowiak

            S Offline
            S Offline
            samflex
            wrote on last edited by
            #5

            Thank you so very much Dave for the prompt response.

            1 Reply Last reply
            0
            • S samflex

              WOW! But you can understand why I was confused. The method says Update(...) Thank you.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              samflex wrote:

              The method says Update(...)

              No, it says updateSal_Click(object sender, EventArgs e), which is clearly an event handler that is called when the user clicks a button or other control. And the result of the LINQ query is returned in the Text field of the newTotal item, which is most likely a field on the form.

              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