datasource binding to a dropdownlist with Entity object - asp.net

I am using Entity Data model in my project, and using Dropdownlists in several pages. I am
assigning datasources to all the Dropdownlists and using entity objects. Everything was fine until now, and every Dropdownlist is getting value from entity object.
But problem is that it`s only first index value that is selected even though I selected "select any index value" from dropdownlist.
Here is the code snippet:
PAYROLLEntities pe = new PAYROLLEntities();
drplist_fromDept.DataTextField = "DEPARTMENT_NAME";
drplist_fromDept.DataValueField = "DEPARTMENT_ID";
drplist_fromDept.DataSource = pe.HR_DEPARTMENT;
drplist_fromDept.DataBind();
Here is the code for Button "FindEmployee":
GDView_Emplist.DataSource = trans.GetEmployee(drplist_fromDept.SelectedItem.Text,txt_empName.Text);
GDView_Emplist.DataBind();
After compilation, even if I select the last indexed "department" and click on find button, dropdownlist.selectedItem.text is only returning the first index value.

Related

Ax2012 - get the value of accountNum in the selected row in a grid

In the form cutslistepage , i want to get the value of accountNum of the selected row in grid and passe it to another form
i have try that :
int64 recordsCount;
recordsCount = CustTable_ds.recordsMarked().lastIndex();
// CustTable = CustTable_ds.getFirst(1);
If you want to retrieve the CustTable record, check the CustTableListPageInteraction class.
In the selectionChanged method, it has the following code:
custTable = CustTable::findRecId(this.listPage().activeRecord(queryDataSourceStr(CustTableListPage, CustTable)).RecId);
This is how you can retrieve the record. But since it is already done, you can simply use the custTable variable that is already declared in the class declaration.
Side note: if you have an other form that is opened from the list page, it should automatically be filtered based on the relations between the data sources of the form. So you might be searching for a solution for a problem you shouldn't have. For example create a form that has a data source with a relation to the CustTable table on it and it should create a dynaink between the list page and your form, filtering the records for that customer.
If only one record is selected you can do:
info(CustTable_ds.accountNum);
Otherwise, if more than one record is selected you need to do something like:
custTable = CustTable_ds.getFirst(true);
while (custTable)
{
info(custTable.accountNum);
custTable = CustTable_ds.getNext();
}

How to get a class from listbox datasource

So I have the code code which binds the object class to a list box:
lstDealers.DataSource = dealers;
lstDealers.DataTextField = "DealerName";
lstDealers.DataBind();
This works fine, I have no problem displaying the values. However the problem I am having is I am trying to get the class from the list object.
I have tried two different methods but none have worked:
var selectedItems = from ListItem i in lstDealers.Items where i.Selected select i;
Dealer dealer = (Dealer)selectedItems;
and
Dealer dealer = (Dealer)lstDealers.SelectedItem;
Now I know the second one works in a winform, however I am trying to accomplish this in ASP.Net framework 4.5
Any suggestions?
In web forms the SelectedItem does not have the object of type being assigned in DataSource rather you will get ListItem.
You have to use the current item to get the record from your data source as the dealers object is not available on postback. You can assign unique id to DataValueField which you will use after postback to fetch the record from datasource (database)
lstDealers.DataValueField = "IdOfDealer";
On postback
string dealerId = (Dealer)lstDealers.SelectedValue;
Dealer dealer = someMethodToFetchAndReturnDealer(dealerId);

Populating checkbox values from a LINQ query?

I'm trying to populate the checked values on a checkboxlist based upon a LINQ query. But I am having trouble figuring out how to do this.
I have an Enum called UserRoles and on page load I bind the checkbox list to the enum values, and descriptions.
uRoles.DataSource = RiseBi.Enumeration.GetEnumDescriptions(GetType(UserTypes))
uRoles.DataTextField = "Value"
uRoles.DataValueField = "Key"
Next to get the user roles:
Public Shared Function GetAllUserRoles(ByVal EID As Integer) As IQueryable(Of RiseDB.UserRole)
Dim DB As New RiseDB.RiseDBContainer
Dim tmp = (From p In DB.Users Where p.Id = EID).First
Return tmp.UserRoles
End Function
What would be the best way to populate the checkboxes for that particular user that matches the roles?
I was thinking just a loop for each value in UserRoles and where they match check it, but there has to be a simpler way, no?
in msdn CheckBoxList Class
To determine the selected items in the CheckBoxList control, iterate through the Items collection and test the Selected property of each item in the collection.
so only loop for checking items

DataBind of DropDownList works, but does not show in code behind

I'm working on web pages that have an ASP DropDownList defined, and on page load the data source is bound to it through DataSource.DataBind(). When I step through the code, the drop down list does not show anything in it, but when the page actually displays, it does have items in the list. When exactly does DataBind() get applied to the control?
The problem is that some of the values returned by the SQL back end have a null Text, so nothing is displayed in the drop down list for that row. I want to just use the Value as the Text, but only if the Text is null. And when I put the code to loop through and do that right after the DataBind(), there is nothing in the drop down list at that point in the code.
Thanks!
The best option for your specific example is to do what kd7 said, and modify your query. However, if you ever need to do other modifications to the items, you can access them in the DataBound event of the DropDownList. MSDN. This event fires after the binding occurs.
You could
a) Modify your database query to exclude null values
or
b) Before you databind, iterate through the data returned and remove the undesired values.
You can just use LINQ on the data before you assign it to create a new type on the fly with the data arranged how you want. Here is a quick example:
// This simulates the data you get from the DB
List<SomeObject> lstData = new List<SomeObject>();
lstData.Add(new SomeObject() { ID = "1", Text = "One" });
lstData.Add(new SomeObject() { ID = "2", Text = "" });
lstData.Add(new SomeObject() { ID = "3", Text = "Three" });
// This will take your data and create a new version of it substituting the
// ID field into the Text field when the Text is null or empty.
yourDropDownList.DataSource = lstData.Select(i => new {
ID = i.ID,
Text = string.IsNullOrEmpty(i.Text) ? i.ID : i.Text }).ToList();
// Then just databind
yourDropDownList.DataBind();
The DropDownList would have the following items:
One
2
Three

Binding LinqDataSource from code-behind to Gridview

i have a grdidview control on the .aspx page and i am trying to connect dynamically from code behind and bind the gridview but somehow it throwing me an error... what is wrong with this code? any help?
LinqDataSource LDS_POReport = new LinqDataSource();
LDS_POReport.ContextTypeName = "DataContextDataContext";
LDS_POReport.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSourcePO_Selecting);
this.gvReport.DataSource = "LDS_POReport";
//this.gvReport.DataBind();
Update:
after i update the code to
this.gvReport.DataSource = LDS_POReport;
it works fine but when i try to sort i get this error:
The GridView 'gvReport' fired event Sorting
which wasn't handled.
i added this but no effect.
LDS_POReport.AutoPage = true;
LDS_POReport.AutoSort = true;
I guess that your problem is here:
this.gvReport.DataSource = "LDS_POReport";
The above code line attempts to assign a string to a property that expects some sort of data source. I assume that you really intended to assign the LinqDataSource object itself:
this.gvReport.DataSource = LDS_POReport;
First thing, the DataSource should get a reference to the object containing the data, not the name of the object containing the data. GridViews can work reflectively, but not THAT reflectively.

Resources