0

Im new to C# and MySQL as well.
In this answer of my previous question of how to Show messageBox that contain data from SQL he mentioned about:

pass in parameter the rowIndex and stringToSearch, when calling Execute method.

and I still don't understand how it works.
Below here is my ViewModel

public class VModel
{
    public ICommand Clicked { get; set; }
    public DataView Library { get; private set; }
    public VModel()
    {
        DataTable dt = new DataTable();
        using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
        {
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = new MySqlCommand("Select * from movie_list where id_movie = @id_movie", connection);
            adapter.Fill(dt);

        }

        var Library = dt.DefaultView;
        var Clicked = new ClickedCommand(this);
    }

}

Click

 internal class ClickedCommand : ICommand
{
    private VModel _vModel;
    public ClickedCommand(VModel vModel)
    {
        _vModel = vModel;
    }
    public event EventHandler CanExecuteChanged { add { } remove { } }
    public bool CanExecute(object parameter)
    {
            return true;
    }
    public void Execute(object parameter)
    {
        var rowIndex = (string)parameter[0];
        var stringToSearch = (string)parameter[1];
        MessageBox.Show(_vModel.Library.[rowIndex][stringToSearch]);
    }

}

Xaml

<ScrollViewer>
        <ItemsControl Background="#191919" ItemsSource="{Binding Path=Library}" Margin="130,0,2,0" BorderThickness="0">
            <ItemsControl.ItemTemplate>
            <DataTemplate DataType="viewModels:Card">
                <Button Margin="0,2,0,0" Command="{Binding Path=DataContext.Clicked, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
                    <StackPanel Margin="0,0,0,0">
                            <Grid Margin="5,5,5,5">
                                <Rectangle RadiusX="5" RadiusY="5" Width="150" Height="230">
                                    <Rectangle.Fill>
                                        <ImageBrush  x:Name="myImage" ImageSource="{Binding Path=cover}"/>
                                    </Rectangle.Fill>
                                </Rectangle>
                                <Label Background="#CC000000" Margin="107,10,0,193" Content="{Binding Path=year}" HorizontalContentAlignment="right" Foreground="White"/>
                                <Label Width="150" Background="#CC000000" Margin="0,203,0,0" Content="{Binding Path=title}" HorizontalContentAlignment="Center" Foreground="White"/>
                            </Grid>

                        <Label Visibility="Visible"  Name="lbl" HorizontalContentAlignment="Center" Tag="labl"/>
                    </StackPanel>
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
    </ItemsControl>
    </ScrollViewer>

On var rowIndex = (string)parameter[0]; and var stringToSearch = (string)parameter[1]; it shows this:

cannot apply indexing with [] to an expression of type "object"


And when I change my [rowIndex] into [0] it says:

cannot convert from "object" to "string"

Hans
  • 25
  • 1
  • 8

1 Answers1

0

I think you need load all movies in the DataView :

public class VModel
{
    public ICommand Clicked { get; set; }
    public DataView Library { get; private set; }
    public VModel()
    {
        DataTable dt = new DataTable();
        using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
        {
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = new MySqlCommand("select * from movie_list", connection);
            adapter.Fill(dt);

        }

        var Library = dt.DefaultView;
        var Clicked = new ClickedCommand(this);
    }
}

And when you click, you display the title :

internal class ClickedCommand : ICommand
{
    private VModel _vModel;
    public ClickedCommand(VModel vModel)
    {
        _vModel = vModel;
    }
    public event EventHandler CanExecuteChanged { add { } remove { } }
    public bool CanExecute(object parameter)
    {
            return true;
    }
    public void Execute(object parameter)
    {
        var id_movie = (int)parameter;
        var rowIndex = id_movie - 1;
        MessageBox.Show(_vModel.Library[rowIndex]["title"].ToString());
    }
}

You can replace "title" by other column name of table movie_list.

This solution don't use Sql parameter. Not sure is good.

vernou
  • 6,818
  • 5
  • 30
  • 58