3

I'm trying to get value out of a textbox to update my database with.

This is my XAML code:

<Window x:Class="Banken.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:Banken.viewmodel"
        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="Bank" Height="350" Width="525">
    <Window.DataContext>
        <vm:AccountVM/>
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ListBox Margin="8" ItemsSource="{Binding Accounts}" SelectedItem="{Binding SelectedAcc}" DisplayMemberPath="AccountHolder"/>
        <StackPanel Margin="8" Grid.Column="1">
            <Label Content="Accountholder:"/>
            <Label FontWeight="Bold" FontSize="10" Height="25px" Content="{Binding SelectedAcc.AccountHolder}"/>
            <Label Content="Accountnumber:"/>
            <Label FontWeight="Bold" FontSize="10" Height="25px" Content="{Binding SelectedAcc.AccountNumber}"/>
            <Label Content="Balance:"/>
            <Label FontWeight="Bold" FontSize="10" Height="25px" Content="{Binding SelectedAcc.Balance}"/>
            <Label Content="Transfer to:"/>
            <ComboBox ItemsSource="{Binding Path=Accounts}" DisplayMemberPath="AccountHolder" SelectedValuePath="AccountHolder" SelectedValue="{Binding Path=Accounts}" />
            <Label Content="Amount:"/>
            <TextBox InputScope="Number" Name="Amount"></TextBox>
            <StackPanel Orientation="Horizontal" Margin="0,16,0,0">
                <Button Content="Transfer money" Command="{Binding UpdateAccount}" Width="250"/>
            </StackPanel>
        </StackPanel>
    </Grid>

</Window>

Now I want to get the value from the textbox named "Amount", so I can update my database like this:

 public ICommand UpdateAccount
 {
     get { return new RelayCommand<string>(UpdateAccount1); }
 }

 public void UpdateAccount1(string name)
 {
     Console.WriteLine(name);
     Account.UpdateAccount(SelectedAcc, *AMOUNT HERE*);
 }

And then I pass it into my db like this:

public static void UpdateAccount(Account a, int Amount)
{
    string sql = "UPDATE Accounts SET Saldo=Saldo-@Amount WHERE ID=@ID";
    DbParameter par1 = Database.AddParameter("@Amount", Amount);
    DbParameter par2 = Database.AddParameter("@ID", a.ID);
    Database.ModifyData(sql, par1, par2);

    Console.WriteLine(a.ID + " was edited");
}

This all works perfectly when I hard-code the values in it, but I can't seem to find a way to get that textbox value in there?

Kasun Kodagoda
  • 3,956
  • 5
  • 31
  • 54
user3208179
  • 367
  • 1
  • 6
  • 12

2 Answers2

1

Try this

    <Button Content="Transfer money" Command="{Binding UpdateAccount}" CommandParameter="{Binding Text, ElementName=Amount}" Width="250"/>

Here we are Binding CommandParameter to the Text of TextBox .

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • This works! Afterwards I just did: `public void UpdateAccount1(string Amount) {Account.UpdateAccount(SelectedAcc,Convert.ToInt32(Amount));}` – user3208179 Aug 23 '14 at 21:05
0

Either bind the command parameter on your button to the text property of your text box...

{Binding Text, elementname=...}

... or bind the text property of your textbox to a value in your viewmodel. Then pick it up in your command action.

kidshaw
  • 3,423
  • 2
  • 16
  • 28