• 0

C# ListBox to LINQ method help


Question

Trying something new here.

I have a form where the user selects an item from the ListBox. It polls the database based on the ID and returns the correct values to the textboxes on the form.

I've tried a few different ways, all of which returns a null value except the ID.


// Select Mode
private void LstBuildersSelectedIndexChanged(object sender, EventArgs e)
{
var mId = LstBuilders.SelectedIndex + 1;
Accessor.SelectedBuilder(mId, MBuilder, MAddress, MCity, MState, MPhoneNumber, MFaxNumber, MEmailAddress);
TxtBuilderName.Text = MBuilder;
TxtAddress.Text = MAddress;
TxtCity.Text = MCity;
TxtState.Text = MState;
TxtPhoneNumber.Text = MPhoneNumber;
TxtFaxNumber.Text = MFaxNumber;
TxtEmailAddress.Text = MEmailAddress;
}
[/CODE]

Accessor.cs

[CODE]
// Selected Builder
public static void SelectedBuilder (int mId, string mBuilder, string mAddress, string mCity, string mState, string mPhoneNumber, string mFaxNumber, string mEmailAddress)
{
var dataConnect = new PxLinqSqlDataContext();
return (from b in dataConnect.GetTable<TblBuilders>()
where (b.IdBuilder == mId)
select // SOMETHING HERE TO MAKE THIS MAGIC WORK
mBuilder = b.BName,
mAddress = b.BAddress,
mCity = b.BCity,
mState = b.BState,
mPhoneNumber = b.BPhoneNumber,
mFaxNumber = b.BFaxNumber,
mEmailAddress = b.BEmailAddress)
)
}
[/CODE]

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

You can't do that directly. A LINQ query returns an IEnumerable, i.e. a collection. Here it looks like you're only interested in one result, so you could do:


var table = dataConnect.GetTable<TblBuilders>().FirstOrDefault(t => t.IdBuilder == mId);
if (table != null) {
mBuilder = table.BName;
mCity = table.BAddress;
// etc
}
[/CODE]

Link to comment
Share on other sites

  • 0

You can't do that directly. A LINQ query returns an IEnumerable, i.e. a collection. Here it looks like you're only interested in one result, so you could do:


var table = dataConnect.GetTable<TblBuilders>().FirstOrDefault(t => t.IdBuilder == mId);
if (table != null) {
mBuilder = table.BName;
mCity = table.BAddress;
// etc
}
[/CODE]

That helped out a lot thanks.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.