2

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?

asked Oct 27, 2014 at 8:30

2 Answers 2

3

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;
}
answered Oct 27, 2014 at 8:31

3 Comments

Thanks, modified my code, including an error I had with the selection (string selected = comboBox1.Text;), but I keep getting a n/a return. I know there's data, I just entered the data, and a SQL query gives me the data. Definitely on the right track though.
@b0b heh; you probably mean: where branches.branchCode == selected, not where branches.branchCode == "selected". Note the quotes.
You sir, are a genius. Something so simple! Comes from trying to translate from one language to another without the required experience.
0

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

answered Oct 27, 2014 at 11:56

1 Comment

Thank you for just verbatimely repeating what Marc said.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.