Question

What I'm trying to achieve is to get one parameter from user (userID) which be stored in TextBox. User enter an example UserID in TextBox, click "View" button, some records from appropriate columns will be displayed in textBoxes placed on the screen. I don't want to use DataGrid because it always looks like table :).

The problem is that I dont know how to put user input into my DB2Command and then display some values from select in textboxes placed on the screen :/.

Here is my code:

View Button

        iDB2Connection nc = new iDB2Connection();
        nc.ConnectionString = "DataSource=vf05.it.volvo.se; DefaultCollection=VF05;";
        nc.Open();

        iDB2Command cmd = new iDB2Command("Select AAUSER, AANODE, AAORGN from     library.file where AAUSER = @UserID ", nc);
        cmd.Parameters.AddWithValue("UserID", UserInput.Text);

        iDB2DataReader dr = cmd.ExecuteReader();

What code should I add to UserInput textBox and how I can display select result in textbox? I could even user DataGrid and then display some values from DataGrid in textboxes but I don't know how to display ExecuteReader display in DataGrid. I probably should read some articles about data binding...

P.S

I was trying to put result of select statement into DataGrid using this

iDB2DataReader dr = cmd.ExecuteReader();
dataGrid1.ItemsSource = dr;

where cmd was:

iDB2Command cmd = new iDB2Command("Select AAUSER, AANODE, AAORGN from T0T02PD.T0F001 where AAUSER = 'A0390944' ", nc);

This seems to work properly but anyway I want to put data from chosen columns into TextBoxes. How to achieve this?

Was it helpful?

Solution

You can make a TextBox with a Binding on a property in your ViewModel and put the binding in mode=TwoWay for update your ViewModel value from the user interface.

In your ViewModel:

public int UserID { get; set; }

In your Xaml:

<TextBox Text="{Binding UserID, mode=TwoWay}"/>

After that you can store yours resulting items in a List and Bind it to a ItemsSource Property in a ListBox.

In your ViewModel:

public List<YourResultType> Items { get; private set; }

In your Xaml:

<ListBox ItemsSource="{Binding Items}"/>

I hope it will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top