Use the following query select sum(Team1) from T_Score;
and instead of cmd.ExecuteNonQuery();
use cmd.ExecuteScalar();
and use the return value of the method ExecuteScalar
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executescalar
Like this:
private void WriteToLabel()
{
using (SqlConnection conne = new SqlConnection(@"Data Source=LAPTOP-S2J1U9SJ\SQLEXPRESS;Initial Catalog=Unit4_IT;Integrated Security=True"))
{
conne.Open();
string selectQuery = "select sum(Team1) from T_Score;";
using (SqlCommand com = new SqlCommand(selectQuery, conne))
label1.Content = Convert.ToString(com.ExecuteScalar());
}
}
The line
label1.Content = Convert.ToString(com.ExecuteScalar());
Will require a Label
called label1
for it to work.
It tries to write the result of Convert.ToString(com.ExecuteScalar())
into the Content
property of said Label
.
You can also use a TextBlock
and than use the Text
Property, so this line:
textBlock1.Text = Convert.ToString(com.ExecuteScalar());
Notice instead of calling conne.Close();
manually, I wrapped the SqlConnection conne
into a using
Statement, this should always be done with objects that inherit from IDisposable
.
You can read about IDisposable
and the using
statement here:
If you have the values already in a IEnumerable
object eg. List<int>
than you can just do the following
With a simple loop:
var myValues = new List<int>() { 1, 2, 3, 4, 5, 6 }; //<- list with temporary data
var sumOfValue = 0;
foreach (var myValue in myValues)
sumOfValue += myValue;
With LINQ
:
var myValues = new List<int>() { 1, 2, 3, 4, 5, 6 }; //<- list with temporary data
var sumOfValue = myValues.Sum();
If you don't have an IEnumerable
of int
but instead a class
than your code will look like this
public class MyValue
{
public int Value { get; set; }
//other properties
}
var myValues = new List<MyValue>()
{
new MyValue() { Value = 1 },
new MyValue() { Value = 2 },
new MyValue() { Value = 3 },
new MyValue() { Value = 4 },
new MyValue() { Value = 5 },
new MyValue() { Value = 6 },
}; //<- list with temporary data
With a simple loop:
var sumOfValue = 0;
foreach (var myValue in myValues)
sumOfValue += myValue.Value;
With LINQ
:
var sumOfValue = myValues.Sum(x => x.Value);
In all those cases you will have to write sumOfValue
into your Label
or TextBlock
like this
//Label
label1.Content = sumOfValue.ToString();
//or TextBlock
textBlock1.Text = sumOfValue.ToString();
To pass a value to a different page, all you would need to do is the following.
Search for your page in code behind eg. LeaderBoard
.
It should look something like this
public partial class LeaderBoard : Page
{
//stuff...
}
Add a new Property to this class
public partial class LeaderBoard : Page
{
public int MyProperty { get; set; }
//stuff...
}
When you initialize the LeaderBoard
eg. here
this.NavigationService.Navigate(new LeaderBoard());
Change the initialization to this:
this.NavigationService.Navigate(new LeaderBoard() { MyProperty = 7187, });
7187
is a random number this needs to be filled with what ever you need.
With this you have "transfered" data to LeaderBoard
and the new property with its value can be accessed in LeaderBoard
eg. there could be a method like this
public void Foo()
{
textBlock1.Text = Convert.ToString(this.MyProperty);
}
The class would than look like this:
public partial class LeaderBoard : Page
{
public int MyProperty { get; set; }
//stuff...
public void Foo()
{
textBlock1.Text = Convert.ToString(this.MyProperty);
}
}
If you need to change the MyProperty
from a different place while the LeaderBoard
is open keep the reference you have created.
To keep the reference change this line
this.NavigationService.Navigate(new LeaderBoard() { MyProperty = 7187, });
To this
_leaderBoard = new LeaderBoard() { MyProperty = 7187, };
this.NavigationService.Navigate(_leaderBoard);
And create a Field
in the outer scope like this
private LeaderBoard _leaderBoard; //<- needs to be outside the method
private void Btn_LeaderB_Click(object sender, RoutedEventArgs e)
{
_leaderBoard = new LeaderBoard() { MyProperty = 7187, };
this.NavigationService.Navigate(_leaderBoard);
}
If Btn_LeaderB_Click
gets called multiple times but you only want to create 1 LeaderBoard you can do this:
private void Btn_LeaderB_Click(object sender, RoutedEventArgs e)
{
if (_leaderBoard == null)
_leaderBoard = new LeaderBoard() { MyProperty = 7187, };
this.NavigationService.Navigate(_leaderBoard);
}
With that the _leaderBoard
field will only get initialized once.
If you want to increase the value MyProperty
everytime the Btn_LeaderB_Click
method gets called you can further extend it to this:
private void Btn_LeaderB_Click(object sender, RoutedEventArgs e)
{
if (_leaderBoard == null)
_leaderBoard = new LeaderBoard() { MyProperty = 7187, };
else
_leaderBoard.MyProperty += int.Parse(txt_score4_tm1.Text);
this.NavigationService.Navigate(_leaderBoard);
}