Populate A Generic List Of Objects In C# From SQL Database?
Mar 16, 2010
I am just learning ASP.NET c# and trying to incorporate best practices into my applications. Everything that I read says to layer my applications into DAL, BLL, UI, etc based on separation of concerns. Instead of passing datatables around, I am thinking about using custom objects so that I am loosely coupled to my data layer and can take advantage of intellisense in VS. I assume these objects would be considered DTOs?
First, where do these objects reside in my layers? BLL, DAL, other?
Second, when populating from SQL, should I loop through a data reader to populate the list or first fill a data table, then loop through the table to populate the list? I know you should close the database connection as soon as possible, but it seems like even more overhead to populate the data table and then loop through that for the list.
Third, everything I see these days says use Linq2SQL. I am planning to learn Linq2SQL, but at this time I am working with a legacy database that doesn't have foreign keys setup and I do not have the ability to fix it atm. Also, I want to learn more about c# before I start getting into ORM solutions like nHibernate. At the same time I don't want to type out all the connection and SQL plumbing for every query. Is it ok to use the Enterprise DAAB for now?
View 2 Replies
Similar Messages:
Aug 1, 2010
I have a list of Categories which I need to bind to a TreeView control in WPF and I can't find a working tutorial or get it working.My Category class consist of (ID, Title, ParentID). If the Category is a top level category the parentID is null.Can anyone sow me how to bind this to a treeview?
View 3 Replies
Apr 15, 2014
I am displaying data with in textboxes in gridview here user will change the values my problam is how to get the gridview data and assign it to List?
View 1 Replies
Nov 17, 2010
have some Objects, lets say Employee and Role defined as below and I have defined relationships in my database that gives me a list of objects say employees and thanks to my framework each employee object also has a Role object linked via the RoleIDID, UserName, Password, Email, RoleIDRoleID, RoleNameSo in code I can do something like this
Employee emp = dataService_GetEmployeeByID(1);
string RoleName = emp.Role.RoleName;
Now here is my problemI can bind any object in a repeater and it works fine for the first level in my relationship
For instance <%# Eval("UserName") %>
But I need to be able to show the details for my child objects as well (Role) so something like this (which does not work)
<%# Eval("Role.RoleName") %>
View 2 Replies
Oct 11, 2010
I am having trouble populating a database that is shown in a list box. The user uses a text box to enter an ID and the ID will bring up information. The problem I am having is in the highlighted area. It says The expression contains an invalid string constant: '1010. where 1010 is what i entered in the text box to test it.
[Code]....
View 3 Replies
Apr 6, 2010
I know how to bind a simple objects to a dropdown list. However I am having problems binding my objects which contains sub objects to the control.i.e. with simple object i just do ddl.DataValueField = "myproperty"
with my objects they contains sub objects which i want to bind. I have tried ddl.DataValueField = "sub-object.myproperty" which doesnt work.
View 2 Replies
Oct 4, 2010
I'm stuck with a problem to populate a DropDownList control with values from the database using item field template in read only mode. since I'm new to ASP.NET. Below is the code and the error I'm getting:
'PictureReadOnlyCategories' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'PictureReadOnlyCategories' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
The code for this is :
<asp:SqlDataSource ID="categoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand ="SELECT [CategoryID], [Name] FROM [Categories] WHERE ([UserId] = @UserId)">
<SelectParameters>
[Code]....
View 1 Replies
Oct 6, 2010
I have an application as: SelectCommand="SELECT [CategoryID], [Name], [UserId] FROM [Categories] WHERE ([UserId] = @UserId) ORDER BY [Name]">
</asp:SqlDataSource>
Pick a category: <asp:DropDownList ID="categories" runat="server" AutoPostBack="True"
DataSourceID="categoriesDataSource" DataTextField="Name" [code]...
When i take away the <SelectParameters>
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters> and ([UserId] = @UserId), the dropdown list control will populate values from the database but when i live those 2, it won't populate any value from the database. When i remove the <asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>,and live ([UserId] = @UserId), i get the error : Must declare the scalar variable "@UserId".
View 1 Replies
Apr 13, 2010
I have been banging my head against this problem for a bit and I think I am stuck. I would like to pull data from a Database to populate a list of possible "clients" to select from on one side. Then using what has been clicked load the client specific data into a tab container on the opposite side. I plan on doing lazy tab loading so I am not sure if that matters. I am really stuck and any help or guidance anyone could give would be greatly appreciated! I understand the basics of data loading and of tab containers, but i am unsure of what to do. I am coding this in C#. Example Mock Interface This is where the data list would be and I want to be able to select something here And update the Tab Panel control here based on the selection from the left side via querying the database.
View 3 Replies
Jan 17, 2011
I have 5 radiobutton list in the form . i want to populate from it from the database.I have one table which has 5 Coloumn ( productname,group,id ,value,decscription) .Accrodindg to grouto i want to popluate all radiobutton list which display the product name.after populate all radiobutton list user select whatever list items i want to save using session in to new table.and which r the columns i make it for new table so i can save it user selction in databse..
my table veiw is like this
productname group id value decription
abcd g1 1 1 XXXX
bcdef g1 2 2 XXXXX
efgh g1 3 3 XXXXX
ghi g2 4 1 XXXX
dgjks g2 5 2 XXXX
ghidsg g3 6 1 XXXX
ghi g3 7 2 XXXX
fsdfghi g4 8 1 XXXX
so on upto g5 ..... my first raiobutton just populate group= g1 productname only for second raidobutton list just populate group=g2 productname only soon.
View 5 Replies
Apr 8, 2010
I would like to alter the query that is run, depending on the selection made by the user in a drop down list on the same page. Would this best be done using a partial page update (AJAX)? Ideally it would be great to have the textbox result change quickly once the drop-down list value has changed, without a full page postback. I would like to change the SQL query to a completely different query if the user selects an option different to the default (page_load value) in the drop-down list. I would then like the newly retrieved result to be displayed in the textbox as before.
My current VB function looks like this:
[Code]....
View 2 Replies
Feb 6, 2010
I have the following Students class:
[Code]....
I need the 1 since it's a foreign key in another table. For the life of me, I can't get this to work like this.
View 7 Replies
Feb 7, 2011
I have following doimain objects:
public class ComponentType
{
public int ComponentTypeID { get; set; }
public string Component_Type { get; set; }
public string ComponentDesc { get; set; }
}
[code]...
View 2 Replies
Mar 7, 2011
I have string which came from the database,now i want to compare each charater of the string with the radiobuttonlist value and check box list value and select both radiobutton list and checkboxlist.
Here i write down some code but its just select 2 item of the each radiobuttonlist and checked all checkboxlist.
[Code]....
View 2 Replies
Feb 24, 2010
I'm designing my own custom control that contains a .NET dropdownlist. What I'm wondering is if it is possible to populate my dropdownlist with listitems placed in a placeholder? For example:
<asp:DropDownList ID="ddlFilter" runat="server" >
<asp:PlaceHolder ID="ListItemPlaceholder" runat="server"/>
</asp:DropDownList>
This doesn't work because the DropDownList control only allows ListItems as child controls. But, I want to do something similar to this so when the user includes my control on a page, they can do something like this:
<mytag:MyControl Mode="DropDown" runat="server">
<ListItemTemplate>
<asp:ListItem Text="C" Value="c"></asp:ListItem>
<asp:ListItem Text="E" Value="e"></asp:ListItem>
<asp:ListItem Text="B" Value="b"></asp:ListItem>
</ListItemTemplate>
</myTag:MyControl>
I know I can do this by dynamically adding the ListItems in the page code behind, but I'd like to avoid that if possible.
View 1 Replies
Jan 3, 2011
I have a list view control. Each row within it has a rowid and a textbox where a note(user enters relevant info to that particular record) is entered. On a paging event I store the rowID and the note text in a generic list for retrieval if the user pages back later on. Goal here is to keep the text the user has entered. (see the code below)This generic list does in fact get populated correctly when I go to the next page currently. I can see this in Visual Studio. So now, when the user goes back to the first page I want to run a test within my Listviews ItemDataBound event. I want to see if the row ID being tested is part of the generic list. If so, I then want to take the corresponding note that is also within the list and bind it to the text box. However, I don't know how to write the correct conditional for this and how to bind the textbox if the conditional evaluates to true.
View 1 Replies
Feb 8, 2011
I'm trying to populate a dropdown list from a list of existing SQL tables in a database. Does anyone have a code sample for doing this?
View 10 Replies
Apr 7, 2010
I have two generic list with Hotel class object. Both List contains hotel ID. I want to retrieve common hotel id from both Hotel List and assign to new HotelList.I have done following code for that but any other sort way to implement this kind of functionality.
[Code]....
Note: I dont want to use Linq because I am working on framwork 2.0
View 1 Replies
Aug 4, 2010
I'm trying to render out some images on an aspx page. Error I'm getting in the code below is:
DataBinding: '_Default+ImageThing' does not contain a property with the name 'FileName'.
public class ImageThing
{
public string FileName;
}
private void DisplayThumbnailImages()
{
ImageThing imageThing1 = new ImageThing();
ImageThing imageThing2 = new ImageThing();
imageThing1.FileName = "asdf.jpg";
imageThing2.FileName = "aaa.jpg";
List<ImageThing> imagesToRender = new List<ImageThing>();
imagesToRender.Add(imageThing1);
imagesToRender.Add(imageThing2);
Repeater1.DataSource = imagesToRender;
Repeater1.DataBind();
}
here is the aspx:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "FileName")%>
</ItemTemplate>
</asp:Repeater>
View 3 Replies
Jan 19, 2011
in my model there is a List.
public class InvoiceModel{
public int InvoiceID { get; set; }
public int Date { get; set; }[code]...
for create view i can use Html.TextBoxFor(m => m.InvoiceID) and for Date.but how can i fill List?and how can i use global for this list as Shopping cart?
View 6 Replies
Nov 27, 2010
I need to populate a listbox, however, the value on the second box needs to derive from the first one, such as when I click on North America, USA and Canada would show up. the tricky part is that I need to be able to select North America and Europe in the same time and 2nd list box needs to show USA, Canada, UK, Italy, German accordingly.
A1: North America
A2: Europe
A3: Asia
B1: USA
B2: Canada
C1: UK
C2: Italy
C3: German
D1: China
D2: India
View 5 Replies
Aug 17, 2010
Can any one can describe the main diffrence between the array and generic list.
View 6 Replies
Apr 7, 2010
I have one Hotel class that contains many properties. When I retrieve all hotels data from database, I store them in Generic List object.Now what I want that I have two properties called LowestPrice and HiestPrice. I want to get Minimum(LowestPrice) and Maximum(HiestPrice) from generic list directly.
Something like HotelList.Min();
Right now I have following logic implemented in my project, but I dont want to use foreach loop.
[Code]....
View 6 Replies
Nov 23, 2010
I have declared a simple class
[Code]....
And Pass some values to it :
[Code]....
Then I use Linq to select rows :
[Code]....
How I can enumerate it?! I used this, but it has error
[Code]....
View 3 Replies
Jun 25, 2010
I am using datalist to bind the generic list. On databind() command I am getting error "nable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
View 3 Replies