-1

In a MyViewModel I have a string property (SearchBox) that is binded to a textbox in my view. When I click the searchbutton A command get send to my datalayer class to get the searchresult (images) from the dB.

My problem is how to pass the string from the searchBox along with the method to use in the datalayer class.

public class MyViewModel : NotifyUIBase
{
    private string _searchBox;
    public string SearchBox    // the Name property
    {
        get { return this._searchBox; }
        set { this._searchBox = value; RaisePropertyChanged(); }
    }  

    public MyViewModel()
    {
        FindImageCommand = new RelayCommand(FindImage);
    }

    public RelayCommand FindImageCommand { get; private set; }
    private void FindImage()
    {
        var dbFunctions = new DatabaseFunctions();
        FindVisualReferences = dbFunctions.FindVisualReferences();
    }
}

In the DataLayer class I need to use the string from the SearchBox in the query to search the dB.

public class DataLayer 
{

    public ObservableCollection<Image> FindVisualReferences()
    {
        var FindVisualReferences = new ObservableCollection<Image>();

        String dbConnectionString = @"Data Source =mydB.sqlite";

        SQLiteConnection cnn = new SQLiteConnection(dbConnectionString);
        cnn.Open();

 ====>  string Query = "Select* from images where title = '" + SearchBox.ToUpper() + "'";

        SQLiteDataAdapter sda = new SQLiteDataAdapter(Query, cnn);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        // rest of dB method
    }
}

How do I get the string from SearchBox in MyViewModel into the query to search the database in the DataLayer Class?

Phil
  • 561
  • 2
  • 16
  • 29
  • 1
    Please explain what you've tried, what that did, and how it was different from what you wanted it to do. Be sure to include [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) illustrating all of that. In particular, please explain clearly why you can't just pass the value to the `FindVisualReferences()` method, just as one would normally do when they want some method being called to have access to some specific value it needs. – Peter Duniho Jul 08 '15 at 05:42
  • 2
    As @PeterDuniho said, just pass dbFunctions.FindVisualReferences(SearchBox). Also do not concat an sql query like that, it opens it up for sql injection. Use parameters! Use an SQLiteCommand, and add parameters. – Floris Jul 08 '15 at 05:53

1 Answers1

0

Firstly, change your method to take in a string paramter:

public ObservableCollection<Image> FindVisualReferences(string search)

Now, you can simply pass in the SearchBox string when you call this method, like so:

FindVisualReferences = dbFunctions.FindVisualReferences(SearchBox);

You can then rewrite the query to reference the parameter, like this:

string Query = "Select* from images where title = '" + search.ToUpper() + "'";

Now, @Floris made a good point to use parameters instead of string concatenation, I would recommend looking into this, as it has been answered already.

Community
  • 1
  • 1
Mike Eason
  • 9,525
  • 2
  • 38
  • 63