Home » c# » c# – Using neo4j as data source for a WPF Autocomplete Box-Exceptionshub

c# – Using neo4j as data source for a WPF Autocomplete Box-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I try to get a Autocomplete TextBox running with neo4j as Data Source, for this I need to get all founded elements returned as a List where I can “Search” inside.

I have a List and I have a connection to query neo4j DB to get all persons by their name, a MessageBox is displaying ,just for testing, if we receive data but if I enter a letter in my Textbox my App just freezes.

Code behind:

public partial class UserControlQueryDB : UserControl
{
    public UserControlQueryDB()
    {
        InitializeComponent();
        GetSuggestionsFromNeoAsync();
    }

    private async Task<List<string>> GetSuggestionsFromNeoAsync() {

IDriver driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic("neo4j", "neo4j"));
IAsyncSession session = driver.AsyncSession(o => o.WithDatabase("neo4j"));

            var query2Neo = "MATCH (n:Person) RETURN n.Name";

            IResultCursor cursor = await session.RunAsync(query2Neo);
            List<string> found = await cursor.ToListAsync(record => record["name"].As<string>());

            await cursor.ConsumeAsync();
            await session.CloseAsync();
            await driver.CloseAsync();

        return found;
    }

    private void TextBox_KeyUp(object sender, KeyEventArgs e)
    {
        bool found = false;
        var border = (resultStack.Parent as ScrollViewer).Parent as Border;
        var data = GetSuggestionsFromNeoAsync();

        MessageBox.Show(data);

        string query = (sender as TextBox).Text;

        if (query.Length == 0)
        {
            // Clear
            resultStack.Children.Clear();
            border.Visibility = Visibility.Collapsed;
        }
        else
        {
            border.Visibility = Visibility.Visible;
        }

        // Clear the list
        resultStack.Children.Clear();

        // Add the result
        foreach (var obj in data.Result)
        {
            if (obj.ToLower().StartsWith(query.ToLower()))
            {
                // The word starts with this... Autocomplete must work
                addItem(obj);
                found = true;
            }
        }

        if (!found)
        {
            resultStack.Children.Add(new TextBlock() { Text = "No results found." });
        }
    }

    private void addItem(string text)
    {
        TextBlock block = new TextBlock();

        // Add the text
        block.Text = text;

        // A little style...
        block.Margin = new Thickness(2, 3, 2, 3);
        block.Cursor = Cursors.Hand;

        // Mouse events
        block.MouseLeftButtonUp += (sender, e) =>
        {
            textBox.Text = (sender as TextBlock).Text;
        };

        block.MouseEnter += (sender, e) =>
        {
            TextBlock b = sender as TextBlock;
            b.Background = Brushes.PeachPuff;
        };

        block.MouseLeave += (sender, e) =>
        {
            TextBlock b = sender as TextBlock;
            b.Background = Brushes.Transparent;
        };


    }

}

Thats the Xaml:

        <StackPanel Orientation="Horizontal">
        <TextBox Width="300" Padding="5, 3, 5, 3" KeyUp="TextBox_KeyUp" Name="textBox" />
        <Border Width="298" Height="150" BorderBrush="Black" BorderThickness="1">
            <ScrollViewer VerticalScrollBarVisibility="Auto">
                <StackPanel Name="resultStack"></StackPanel>
            </ScrollViewer>
        </Border>
    </StackPanel>
How to&Answers:

Don’t forget to await your async method in the event handler (and add the async keyword to the event handler’s signature):

private async void TextBox_KeyUp(object sender, KeyEventArgs e)
{
    bool found = false;
    var border = (resultStack.Parent as ScrollViewer).Parent as Border;
    var data = await GetSuggestionsFromNeoAsync(); //<--
...