Newbie here, working on converting a little program I had in python/sqlite to C#/SQL Server. I'm having a bit of an issue with getting a SQL query to write its results to two textboxes (textBox1 and textBox2) based upon the selection of a combobox (which does populate correctly, by the way). Here's my attempted code, just need the last bit that writes the result to the textboxes:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Clear();
string selected = (this.comboBox1.SelectedIndex).ToString();
var selected2 = from branches in branchesDataSet.branches
where branches.branchCode == "selected"
select new
{
branches.branchCode
branches.branchName
};
I've tried passing it to an array, to a list, just can't seem to get it to give the result, just the identifier. Any help? Where am I going wrong?
2 Answers 2
What you have at the moment is an IQueryable<T>
for some unpronounceable anonymous-type T
. I assume, since you are using text-boxes, that you expect at most one row, so:
var row = selected2.FirstOrDefault();
if(row == null) {
textBox1.Text = textBox2.Text = "n/a";
} else {
textBox1.Text = row.branchCode;
textBox2.Text = row.branchName;
}
3 Comments
where branches.branchCode == selected
, not where branches.branchCode == "selected"
. Note the quotes.Since you use textbox this mean you query one row:
var onerow= selected2.FirstOrDefault();
if(row == null) { textBox1.Text = textBox2.Text = "n/a";}
else { textBox1.Text = onerow.branchCode; textBox2.Text = onerow.branchName; }
Hope this help you