Converting Linq to Sql Code (SOLVED)
-
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.
-
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.
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 -
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 -
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 -
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 -
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 theText
field of thenewTotal
item, which is most likely a field on the form.