Luke Litherland Luke Litherland - 2 months ago 10
C# Question

Populate TextBox with Text from DataTable - Combo Box

I have a Data Table (Two DataTables but we will just imagine there is one for now) that have been declared when my window loads, this DataTable is populated with:

  • NoteID

  • NoteName

  • Note

NoteID: The First Column in my SQL Database giving the Note and ID

NoteName: The Second Column in my SQL Database giving the note a Name

Note: This is the third and final column giving the note its Text (Could be quite long its a VarChar(MAX))

Currently my DataTable populates no problem, however i want to fill my TextBox(Named: textResult) with the "Note" column associated with the "NoteName" that is selected in the Combo Box.

//Setup connection to server
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "";
builder.InitialCatalog = "DiscoverThePlanet";
builder.UserID = "TestPerm";
builder.Password = "Test321";

string connectionString = builder.ConnectionString;

DataTable dtNotes = new DataTable();
DataTable dtTemplateNotes = new DataTable();

using (SqlConnection conn = new SqlConnection(connectionString))

SqlCommand cmdNotes = new SqlCommand("SELECT NoteID, NoteName, Note FROM Notes", conn);
SqlCommand cmdTemplateNotes = new SqlCommand("SELECT TemplateNoteID, TemplateNoteName, TemplateNote FROM TemplateNotes", conn);

SqlDataReader readerNotes = cmdNotes.ExecuteReader();

dtNotes.Columns.Add("NoteID", typeof(string));
dtNotes.Columns.Add("NoteName", typeof(string));
dtNotes.Columns.Add("Note", typeof(string));

SqlDataReader readerTemplateNotes = cmdTemplateNotes.ExecuteReader();

dtTemplateNotes.Columns.Add("TemplateNoteID", typeof(string));
dtTemplateNotes.Columns.Add("TemplateNoteName", typeof(string));
dtTemplateNotes.Columns.Add("TemplateNote", typeof(string));

// Temporary loop to see if the DataTable (dt) has any data?!?
//foreach (DataRow thisRow in dt.Rows)
// MessageBox.Show(thisRow["NoteName"].ToString());

// Define the columns BEFORE setting the item source
noteNamesList.SelectedValuePath = "NoteID";
noteNamesList.DisplayMemberPath = "NoteName";

templateNoteNamesList.SelectedValuePath = "TemplateNoteID";
templateNoteNamesList.DisplayMemberPath = "TemplateNoteName";

// Set the ItemSource to my fully loaded data table!
noteNamesList.ItemsSource = dtNotes.DefaultView;
templateNoteNamesList.ItemsSource = dtTemplateNotes.DefaultView;



Like is said i have to different Drop Downs but lets just use the one called "dtNotes" for now

I'm pretty new to this and if i try and reference "dtNotes" anywhere in my code it can't find it?

My textResult XAML if you need it:

<TextBox x:Name="textResult" HorizontalAlignment="Left" Height="760" Margin="42,141,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="1494" AcceptsReturn="True" TextChanged="textResult_TextChanged" BorderBrush="#FF00A9CF" FontFamily="DengXian" FontSize="14.667" Background="#FFF3FFFE" Grid.ColumnSpan="2"/>

Combo Box if you need it:

<ComboBox x:Name="noteNamesList" HorizontalAlignment="Left" Height="28" Margin="42,101,0,0" VerticalAlignment="Top" Width="240" BorderBrush="White" Opacity="0.985" FontFamily="DengXian" FontSize="13.333"></ComboBox>

Hopefully i have provided enough information for you, any help is much appreciated.

PS This is on WPF


Looks like you might be declaring your datatables as local variables for that method.

Place the DataTable declaration in the space between Namespace declaration and the class constructor

Namespace MyProject
    public sealed partial class MyClass : Page
        //Class wide variables go here

        DataTable mytable = new DataTable();

        class MyClass()


After that you should be able to reference your DataTable within the ComboBox.SelectionChanged event to assign your value to the TextBox


Within the ComboBox.SelectionChanged event use

TextBox.Text = ComboBox.SelectedValue.ToString();

where TextBox is the name of the TextBox to assign to, and ComboBox is the name of the ComboBox you want to get the value from.

or, if like in your case, its a third value the combobox can't handle directly

TextBox.Text = DataTable.Rows[ComboBox.SelectedIndex]["Note"].ToString()