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
Related
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);
i am new to .net..i have to develop an asp.net application.
The UI of the web page will have a Data-bound Grid control on the Home page and there will be a Textbox where users can enter their search criteria.
I know to do this by using ado.net concept...
But i am supposed to do it using generics concept.How can i store the values in the generic list or dictionary of .net and filter the data based on the text entered in the text box.
Please help me out..
Thanks in advance..
You can indeed bind a GridView to List<T>, I do it all the time, like this:
Create a POCO for the data
public class SomeData
{
public string SomeField {get;set;}
public string SomeOtherField {get;set;}
}
Build the list (either manually or as a result a DB query) e.g.
var mylist = new List<SomeData>();
var myitem = new SomeData()
{
SomeField = "Hello",
SomeOtherField = "World"
};
To Filter the data do something like this:
myfilter = MyTextBox.Value;
mylist = mylist.Where(somedata => somedata.SomeField.Equals(myfiltervalue)).ToList();
Bind it to the GridView
mygridview.DataSource = mylist;
mygridview.DataBind();
And this is it!!
I assume you know ado.net and how to bind gridview.
You just need to iterate through your database resultset and add it to list and bind it.
Following link might help you to begin with:
http://www.aspsnippets.com/Articles/How-to-bind-GridView-with-Generic-List-in-ASPNet-using-C-and-VBNet.aspx
Pass your textbox value to your database query/stored procedure as parameter and return the result based on search value.
Edit:
You may want to use FindAll, Find method.
Check below link:
http://msdn.microsoft.com/en-us/library/aa701359(VS.80).aspx
I have created an ArrayList with items in my Order.aspx.vb. I pass these on to my bllOrder, which passes it on to my dalOrder.
Order.aspx.vb
Dim item As RepeaterItem
For Each item In rptProductList.Items
objOrder.OrderCustID = Session("CustID")
objOrder.OrderProdID = DirectCast(item.FindControl("ddlProducts"), DropDownList).SelectedValue
bllOrder.InsertOrder(objOrder)
Next item
dalOrder
Function InsertOrder(ByVal objOrder As Order) As Boolean
Dim Result as New Boolean
myconn.open()
Dim SQL As String = "INSERT INTO order(OrderCustID, OrderProdID) VALUES (?,?)"
Dim cmd As New OdbcCommand(SQL, myconn)
cmd.Parameters.AddWithValue("OrderCustID", objOrder.OrderCustID)
cmd.Parameters.AddWithValue("OrderProdID", objorder.OrderProdID)
result = cmd.ExecuteNonQuery()
myconn.close()
Return Result
End Function
This is good for one item, but how would I do it for my ArrayList?
All help is much appreciated!
instead of passing single Order item, pass a List of Orders and them loop it though inside your method. make it like that Public Function InsertOrder(objOrder As List(Of Order)) As Boolean and then use objOrder as a list of Orders to loop it through.
put the following code inside a foreach loop following code and pass the current item values;
cmd.Parameters.AddWithValue("OrderCustID", objOrder.OrderCustID)
cmd.Parameters.AddWithValue("OrderProdID", objorder.OrderProdID)
result = cmd.ExecuteNonQuery()
Convert the array of items into an xml string and you can do a bulk insert in the stored procedure using openxml. http://msdn.microsoft.com/en-us/library/ms191268.aspx also refer an older post for sql server 2005 http://weblogs.asp.net/jgalloway/archive/2007/02/16/passing-lists-to-sql-server-2005-with-xml-parameters.aspx
** edited to account for extra info **
You could adapt the existing "for each" logic in your codebehind to build an arraylist or List<> of products - this array/list should be a property of your Order object. Pass the Order object to the DAL via your BAL as currently.
Then iterate through the products in the Order object in the DAL code(InsertOrder) and either
insert each row individually in a
loop
or dynamically build an insert statement for the Order
.
You should wrap it in a transaction to ensure the order is rolled back competely if one row fails to insert.
For orders with large amout of products i'd go for #Aravind's answer.
I’d use SqlClient.SqlBulkCopy. This is effectively a .Net version of Bulk Insert, to use it you need to either have your objects you want to insert in a either a DataTable or create a class to read your data that implements IDDataReader. If your inserting 1,000’s of rows then you should see a dramatic performace increase and much tidier code.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx
Please go through the following link
How to insert a c# datetime var into SQL Server
I will suggest you to use the comma seperated values . Do not send the array list in your DAL Layer instead a function should return a formatted comma seperated value from the Presentation Layer and send this value to DAL and finally seperate the values in your stored procedure.
Alternative
You can format your values in an XML and send it to Stored Procedure.
In order to perform it your parameter should be of varchar type.
I'm databinding a radiobuttonlist with a LINQ query like so:
var query = from m in Db.Udt_Menu
orderby m.Name
select m;
this.radMenuSelection.DataValueField = "menuID";
this.radMenuSelection.DataTextField = "name";
this.radMenuSelection.DataSource = query;
this.radMenuSelection.DataBind();
However, when i want to update the record I need to set the selectedindex of the radiobutton to a value from the database. There is a table called udt_PageMenuSelection which has a column called menuID which is a foreign key to udt_Menu.menuID.
When i want to update an existing record, how do i set the selectedindex of the radiolist to the value equal to udt_PageMenuSelection.menuID ?
Do I need to do an additional query?
Thanks
higgsy
I think I understand the structure, and yes it seems the best way to do things; the udt_Menu entity would have a relationship to udt_PageMenuSelection as a one to many relationship, and you could use LoadWith<> to load those, but that is overkill since I think you are talking loading one record. It would be best to just query it separately.
HTH.
I have a DataGridView binded to this table:
[Users]
ID Name
-- ----
11 Qwe
22 Asd
Grid is direclty binded to typed dataset table.
I have a second table like this:
[Records]
ID UserID Data
-- ------ ----
67 11 ....
68 11 ....
Records.UserID is connected to Users with a foreign key.
What I want to do is: when the user doubleclicks User #11 I open a new grid, binded to [Records] table but only binded to rows where UserID = 11. Doubleclick, getting ID, new grid etc. those are OK. I wouldn't had any problems if I was doing this connected with sprocs but I want it to be binded and I simply have no idea how to do this.
Can you please give me any ideas?
If you are using DataSets/DataTables, then you can do something like this to filter your rows for the second grid:
string filter = String.Format("UserID = {0}", selectedUserId);
grdRecords.DataSource = ds.Tables["Records"].Select(filter);
I believe you can edit the records in the array that is returned, but you won't be able to add/remove new items to this collection. You would have to add them to the regular table, then refilter the grid.
Of course, there's all kinds of other ways to do this as well. If you are using objects that derive from IBindingListView, such as instances of the DataView class, you should have access to the Filter property. Then you can just do this:
string filter = String.Format("UserID = {0}", selectedUserId);
DataView myView;
grdRecords.DataSource = myView;
myView.Filter = filter;
And your view will stay filtered to match whatever data you place into it.
Hopefully, this post is clear enough for you to be able to work out the details... I'm a little scatterbrained today. :)
In the end I had to this:
Create a DB class, with a public property (MyDataSetProperty) that returns the typedDataset
Create a custom Fill function in DataSet that accepts an Id parameter.
Use this function to fill the table in the private typedDataset
this.myTableAdapter.FillCustom(this.myTypedDataset.MyTable, this.Id);
Bind bindingSource to this public property:
this.bindingSource.DataSource = this.db.MyDataSetProperty;