• 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
https://www.neowin.net/forum/topic/1118457-c-listbox-to-linq-method-help/
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]

  • 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.

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

    • No registered users viewing this page.
  • Posts

    • No kidding, Tim, we've been running low on memory for a while now... get your head out of the Apple pixie dust cloud and come back down to earth with us peons.
    • It's really pathetic that an MMA video game triggers your political rage...
    • Nvidia GeForce NOW gains support for seven more games as discounts continue by Pulasthi Ariyasinghe There's a brand-new update rolling out to Nvidia's GeForce NOW streaming service, and like every week, that means more games have received support on the platform. This week's drop has additions like Aphelion and Pro Cycling Manager 26 attached to it. Don't forget that the GeForce NOW summer sale is still active too. This limited-time offer drops the 12-month Performance membership from $99.99 to $64.99, saving members $35. At the same time, the 12-month Ultimate membership is currently going for $129.99, dropping the price by $70 from the original $199.99. Moreover, Nvidia reiterated that support for GOG single sign-in and game library is incoming this summer, joining stores like Steam, Ubisoft Connect, Battle.net, and Xbox. "Connect supported game store accounts and stream titles with GeForce RTX power. Games that include cloud-save functionality help keep progress intact across devices," added the company. "Start a game on one screen, pick up where playtime left off on another, and spend less time managing installs and storage space." Here are the games joining GeForce NOW's supported list this week: Embers of the Uncrowned Demo (New release on Steam, available 13) Pro Cycling Manager 26 (New release on Steam, available June 15) Aphelion (Steam) Citizen Sleeper (Epic Game Store, Free from June 18-25) Megastore Simulator (Steam) OPERATOR (Steam) Super Meat Boy 3D (Xbox, available on Game Pass) Keep in mind that, unlike subscription services like Game Pass or EA Play, a copy of a game must be owned by the GeForce NOW member (or at least have a license via PC Game Pass) to start playing via Nvidia's cloud servers. There is also a limit to how many hours subscribers can use the service per month, with extra time being purchasable in chunks.
  • Recent Achievements

    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
    • One Month Later
      eurospharma62 earned a badge
      One Month Later
    • Week One Done
      With What earned a badge
      Week One Done
    • Week One Done
      Harris Gilbert earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      532
    2. 2
      +Edouard
      167
    3. 3
      PsYcHoKiLLa
      72
    4. 4
      neufuse
      64
    5. 5
      ATLien_0
      63
  • Tell a friend

    Love Neowin? Tell a friend!