Kalint Posted November 9, 2012 Share Posted November 9, 2012 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 More sharing options...
0 Andre S. Veteran Posted November 10, 2012 Veteran Share Posted November 10, 2012 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 More sharing options...
0 Kalint Posted November 10, 2012 Author Share Posted November 10, 2012 Alrit, I am bit drunk riht now (Scotch Fridy) i'll tey it taomroorw. Link to comment Share on other sites More sharing options...
0 Kalint Posted November 11, 2012 Author Share Posted November 11, 2012 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 More sharing options...
Question
Kalint
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.
Accessor.cs
// 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