Reputation: 45
I encountered some problem whereby when the user selects the selected name inside the combo box, the data linked with the names selected will show out in the list box. I have problem making into this method. The error falls here.
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue);
Error: Unable to cast object of type'<>f_AnonymousType2'2 [System.String.System.Int32]'to type 'System.IConvertible'.
private void cbLocStation_SelectedIndexChanged(object sender, EventArgs e)
{
using (satsEntities Setupctx = new satsEntities())
{
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue);
var query = (from db in Setupctx.requiredtimings
join timing t in Setupctx.timings on db.RequiredTimingID equals t.TimingID
where db.RequiredLocationStationID == selectLocStation
select new
{
t.Time2
}).ToList();
List<TimeSpan> lstSelectedTime = new List<TimeSpan>();
foreach (var a in query)
{
lstSelectedTime.Add((TimeSpan)a.Time2);
}
lstTime.DataSource = lstSelectedTime;
}
}
This is what I do to store data inside the combo box.
using (satsEntities Setupctx = new satsEntities())
{
var DeleteRT = (from DelRT in Setupctx.requiredtimings
join locationstationname ls in Setupctx.locationstationnames on DelRT.RequiredLocationStationID equals ls.locationstationID
select new { ls.locStatname, DelRT.RequiredLocationStationID }).Distinct().ToList();
cbLocStation.DataSource = DeleteRT.ToList();
cbLocStation.DisplayMember = "locStatname";
cbLocStation.ValueMember = "RequiredLocationStationID";
Any help will be greatly appreciated.
Upvotes: 0
Views: 1825
Reputation: 45
private void Edit_TS_Load(object sender, EventArgs e)
{
using (satsEntities Setupctx = new satsEntities())
{
var DeleteRT = (from DelRT in Setupctx.requiredtimings
join locationstationname ls in Setupctx.locationstationnames on DelRT.RequiredLocationStationID equals ls.locationstationID
select new {ls.locStatname, DelRT.RequiredLocationStationID}).Distinct().ToList();
cbLocStation.DataSource = DeleteRT.ToList();
cbLocStation.DisplayMember = "locStatname";
cbLocStation.ValueMember = "RequiredLocationStationID";
}
}
Answer is here!
Upvotes: 1
Reputation: 13599
if to string is available I would try this
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue.ToString());
or this
int selectLocStation =Int32.Parse(cbLocStation.SelectedValue.ToString());
you can also try this
DataRowView drow = (DataRowView)cbLocStation.SelectedItem;
string str = drow.Row.ItemArray[0].ToString();
check if string has numbers and the convert it to int
you can also add databind
BindingContext oBC = new BindingContext();
cbLocStation.BindingContext = oBC;
cbLocStation.DataBindings.Add(new Binding("SelectedValue", DeleteRT, "RequiredLocationStationID", false, DataSourceUpdateMode.OnPropertyChanged));
Upvotes: 0
Reputation: 1052
you are trying to convert the anonymous type from the query into timespan which is not possible since it doesn't implement the IConvertible. you can write a wrapper class and select that class in the query.
var query = (from db in Setupctx.requiredtimings
join timing t in Setupctx.timings on db.RequiredTimingID equals t.TimingID
where db.RequiredLocationStationID == selectLocStation
select new WrapperClass
{
Time = t.Time2
}).ToList();
public class WrapperClass
{
public DateTime Time { get; set; }
}
where time is a DateTime you have defined in the wrapper class
Upvotes: 0