Sign in to follow this  
Followers 0

C# ListBox to LINQ method help

4 posts in this topic

Posted

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.

[CODE]
// 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]

Share this post


Link to post
Share on other sites

Posted

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:

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

Share this post


Link to post
Share on other sites

Posted

Alrit, I am bit drunk riht now (Scotch Fridy) i'll tey it taomroorw.

Share this post


Link to post
Share on other sites

Posted

[quote name='Dr_Asik' timestamp='1352517962' post='595310003']
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:

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

That helped out a lot thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.