December 2008 - Posts
In this example, I'll show the differences between populating a Treeview using Linq to Entities, versus a SQL Command in Code-Behind using a function, versus Code-In-Front using a SelectCommand.
In creating a Treeview control, I found some differences in assigning the datasource. I list 3 different methods. In my example treeview, I am getting the menu name, preceded by the numeric list order. (This treeview is used for admin purposes to list all menu items, including menu dividers, so they can be selected and populate a form for editing of the menu item, thus the Home item #3 shows a --- to represent an editable menu divider.)
To help you understand what we're populating, our finished admin treeview will look like this:

Here is my TreeView (I happen to be using the Telerik Treeview, but hopefully enough will be applicable to the .net treeview. Feel free to let me know any discrepancies for our readers!):
<
telerik:RadTreeView ID="tvMenu" runat="server">
</telerik:RadTreeView>
1) Populating with a Datasource and SelectCommand in the code-in-front:
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT * FROM [myMenu]">
</asp:SqlDataSource>
Codebehind:
With tvMenu .DataSourceID = "sqldatasource1" ' if defined a datasource in the code-in-front
.DataFieldID = "menuId"
.DataFieldParentID = "parentMenuId"
.DataTextField = "menuName"
.DataValueField = "menuId"
.CausesValidation = False
.Height = 300
.DataBind()
.ExpandAllNodes()
End With
Notice in the above example, we use a string with the datasource name from the code in front to define the datasourceID.
Our NodeDataBound looks like this:
Protected Sub tvMenu_NodeDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadTreeNodeEventArgs) Handles tvMenu.NodeDataBound
Try
Dim item As RadTreeNode = e.NodeDim dataRow As DataRowView = CType(e.Node.DataItem, DataRowView)
item.Text = dataRow("listorder").ToString & " " & _
item.Text
Catch ex As Exception
SendError(ex.Message)
End Try
End Sub
Our dataRow("listorder") should be wrapped in a function that checks for nulls. But in this example I'm simply converting it to string.
2) Populating a Datasource in our CodeBehind:
In this example, remove the datasource in the code-in-front. We'll define it programmatically in the codebehind:
Dim oMenuAdo As New MyMenuAdo()
Dim sqldatasource1 As New SqlDataSource
sqldatasource1.SelectCommand = oMenuAdo.GetAllMenusQueryString()
sqldatasource1.ConnectionString = GetMyConnectionString()
With tvMenu
.DataSource = sqldatasource1 ' if defined a datasource in the code-behind
... the rest remains the same as the first example, including the NodeDataBound...
Notice in the above, we use the name of the datasource, rather than the string name.
The GetMyConnectionString() function simply retrieves the connection string from the ConfigurationManager.AppSettings.
Our GetAllMenusQueryString() looks like this simply returns our query string:
Public Function GetAllMenusQueryString()
Dim sql As String = "SELECT * FROM [myMenu] order by listOrder"
Return sql
End Function
3) Populating with a Linq to Entities Datasource in our CodeBehind:
In our third example, we will define our datasource as an object:
Dim oMenuLinq As New MyMenuLinq()
With tvMenu
.DataSource = oMenuLinq.GetAllMenusDatasourceLinq() ' if defined a datasource as an object
... the rest of our tvMenu remains the same...
Our GetAllMenusDatasourceLinq looks like this:
Public Function GetAllMenusDatasourceLinq() As IEnumerable(Of myMenu) Dim query As IEnumerable(Of myMenu) = Nothing
Try
query = (From m In oEntities.myMenu _
Order By m.listOrder _
Select m)
Catch ex As Exception
End Try Return query
End Function
Our NodeDataBound must be updated to cast to the object's type instead of a DataRowView:
Dim item As RadTreeNode = e.Node
' with linq datasource, cast to my object's type
Dim dataRow As myMenu = DirectCast(e.Node.DataItem, myMenu )
item.Text = dataRow.listOrder.ToString & " " & item.Text
If I've made any mistakes, please feel free to educate me!
May your dreams be in ASP.NET!
Nannette Thacker
I recently did a project using LINQ to SQL. I decided to convert it to the ADO.NET Linq to Entities model instead. It was fairly easy to convert with a few gotchas. I will list the changes here:
| Linq to SQL |
ADO.NET Linq to Entities |
Creating a Model: Add New Item: Linq to SQL Classes |
Creating a Model: Add New Item: ADO.NET Entity Data Model |
query = _
(From m In dc.Menu _
Where m.menuId = menuId _
Select m).SingleOrDefault()
SingleOrDefault is not supported in the current Entities releases (talk has it that it will be added in future releases). So use FirstOrDefault() in your Linq to SQL if you want your code to be forward compatible with Entities. |
query = _
(From m In dc.Menu _
Where m.menuId = menuId _
Select m).FirstOrDefault()
|
Linq to SQL Deletion and Submitting Changes:
Dim dc As New MyEntities()
Dim menuDetail = _
(From md In dc.Menu _
Where md.menuId = menuId _
Select md).FirstOrDefault
If Not menuDetail Is Nothing Then
dc.Menus.DeleteOnSubmit(menuDetail)
dc.SubmitChanges()
End If |
Linq to Entities Deletion and Submitting/Saving Changes:
Dim dc As New MyEntities
Dim menuDetail = _
(From md In dc.Menu _
Where md.menuId = menuId _
Select md).FirstOrDefault
If Not menuDetail Is Nothing Then
dc.DeleteObject(menuDetail)
dc.SaveChanges()
End If |
dc.Menu.InsertOnSubmit(myRecord) |
dc.AddToMenu(myRecord) |
Also, the Linq to SQL messes with the "s" on the end of table names and Linq to Entities does not.
If I've made some mistakes, feel free to educate me. :)
Thanks to the Experts at Experts-Exchange for their help!
May your dreams be in ASP.NET!
Nannette Thacker
Face it, laptops are flimsey. We have 5. They constantly have problems and 3 recently broke down. Just in time for Christmas.
Our girls like to sit on the couch and use their laptops. They don't haul them around, just pick them up off the end table and put them on their laps and use them.
We have Desktop CPU's, 10 of them. They are great. We only need to replace them when we want to go bigger and better.
So, I want a CPU MonKey (monitor+keyboard).
I want to set a CPU on the floor. Then hook a laptop-like Monitor and Keyboard to it wirelessly, and then the user can sit on the couch and use their MonKey (monitor+keyboard) to control their CPU which is sitting on the floor.
The MonKey unit is much cheaper than a laptop and the CPU is much more rugged and affordable as well. So no more expensive, broken laptops.
So then, people like my daughters and millions of gamers, who always sit in the same chair, etc., can now use their MonKey to laptop-enable their CPU.
So, has that been invented? Where can I get one?
May your dreams be in ASP.NET and may you get a MonKey for Christmas!
Nannette
I have been working with the ADO.NET Entities Data Model for ORM and I am very impressed. I'd been working with Data Sets and Table Adapters, so was excited about diving into the new release of ADO.NET Entities Data Model.
I found some excellent tutorials published in October 2008 (so they're fairly recent) to help get you started and want to share them with you.
Anand Thakur shares three C# tutorials:
Getting started with ADO.NET Entity Framework in .NET 3.5
Getting started with ADO.NET Entity Framework in .NET 3.5 - Part II
Getting started with ADO.NET Entity Framework in .NET 3.5 - Part III
I was able to follow along and whip together a quick VB version using my own tables. (You can do that assuming you know both C# and VB.) But in other words, it's a well thought out, documented set of tutorials.
Now what I particularly like about Anand's tutorials is that he caters to the very beginner. For instance, he takes you step by step through creating a 3-tier web application and projects, first by creating a windows class library and adding the ADO.NET Entities Data Model.
Then he shows you how to create the service layer project and how to add references. And finally the web site itself and adding references. If you've never created a three-tier project, this tutorial is an excellent walk-through.
Another tutorial by Wriju Ghosh covers tables with relationships:
ADO.NET Entity: Insert Update and Delete with Relationship
Also see: ADO.NET Entity: Insert Update and Delete
Wriju has also written numerous other blog posts on ADO.NET.
Now let me backup for those who are new to programming ASP.NET. If you're new to ADO.NET, maybe you should review some tutorials on ADO.NET as well. I recommend Joe Mayo's C# tutorial:
Lesson 01: Introduction to ADO.NET
There are additional lesson links at the bottom of this lesson. His objectives are to help you:
Learn what ADO.NET is.
Understand what a data provider is.
Understand what a connection object is.
Understand what a command object is.
Understand what a DataReader object is.
Understand what a DataSet object is.
Understand what a DataAdapter object is.
May your dreams be in ASP.NET!
Nannette Thacker
P.S. I just found this great 7-part tutorial by mendhak, which also includes information on Insert, Update, and Delete with Stored Procedures.
ADO.NET Entity Framework Tutorial and Basics
Enjoy!
In Visual Studio, when you create a new Property, you're likely aware that as you type, intellisense will allow you to type the property name and type and upon hitting enter, it prepopulates the Property as below:

But are you aware that if you type the word "Property" followed by the TAB key, that VS will provide you with this slick little Property template:

Once the template is loaded, you may now edit the green highlighted areas and tab to the next green area, etc. Once you change the private variable name at the top, it will also change the values within the Property. Once you tab to the "String" type and change it, it will update the other "String" types as well:

Anyway, pretty slick. If you didn't know already, give it a shot.
May your dreams be in ASP.NET!
Nannette
I received such a positive response from my post on Laid off? Need a new technology? Free Microsoft Training and Education that I decided to follow up with this brief post.
In my previous "Need a new technology" post, I discussed how you can learn ASP.NET or Silverlight for free and all the different methods of obtaining free training, from the free development tools provided by Microsoft, to the free forums and free web cast events, and more.
But in this post, I'd like to discuss sites that I use that are invaluable, I think, for training as well as on-the-job work. However, these are not free sites, but well worth the cost. I get nothing from these reviews, so they're all from the heart:
Experts Exchange
Experts Exchange: This is a site where you can ask questions about all kinds of topics. The members earn points and status for answering your questions. The zone topics range from Microsoft, Internet, Gamers, Digital Living, Virus & Spyware, Hardware, Software, Developer, Storage, Operating Systems, Database, Security, Programming, Web Development, Networking, and a ton of sub topics under each.
I receive more reponses, as well as timely responses on Experts Exchange, than on any other forum. EE has a free trial period and you can answer questions to earn points in order to ask questions. I know some members who never pay because they answer questions. I personally would rather pay the minimal fee and spend my time earning money, than answering questions. I ask questions about all kinds of things and am very happy with the response.
I ask questions about ASP.NET development mostly, but also SQL queries, SQL Server, IIS6, IIS7, Visual Studio. Firefox, IE, CSS, Adobe Illustrator, Adobe Photoshop, Microsoft Word, Excel, Javascript, Linq, .NET Framework, and even my dishwasher!
As with many forums, you can get lame answers, especially with some who are trying to earn the points, but don't know what they're doing. But that only happens occasionally, and for the most part, you'll typically get someone with a great answer who'll stick it out with you until your problem is resolved. They also have a method of uploading your project to another site for your helpers to look at your code. Then once resolved, you may later delete that project. Anyway, check them out.
If you work at a company in the IT biz, talk to your boss, s/he'll save a ton of money and time by purchasing an EE account for their employees. (I suppose that could back-fire if you're such a hot-dog you want to get the Genius rank and spend all your time answering questions on your bosses dime, rather than asking questions, but do as I do and use it as a resource only, and it will be well worth the money.)
Learn Visual Studio
LearnVisualStudio.net isn't quite what the domain name implies, it's much more. This site consists of a video library covering numerous topics such as Linq, VB, C#, Ajax, Silverlight, IDE, Web Service, Web Controls, Visual Studio, Crystal Reports, Windows Forms, ADO.NET, DataBinding, OOP, Configuration, Deployment, Security and many more. Just check out the video library. On the home page, toward the bottom, you'll see some links where they offer some printable "Cheat Sheets." These are very nice.
At LearnVisualStudio.net, you find a topic, which is typically a series of videos, then you may download the zip for each video invidividually, or scroll all the way to the bottom for a complete zip set of the entire video series. Then I just play the video right out of the zip file by clicking it. But if there is also source code involved and you may download that as well.
There are also some free videos you may watch without paying.
I bought the life-time membership for around $140 and it has been an invaluable tool. The price is minimal. I like to watch the videos while I'm treadmilling, or waiting for something to install or compile or what-not on my development machine. (I have a box on the right for emails, web browser, watching my videos, Word, Illustrator, Photoshop, etc. And on my left I have my development box. It is entirely for web site development only with only applications for that purpose.) When I'm doing something that requires I be brain-dead, I can watch a video.
The most recent video series is on LINQ to SQL. Now, who knows enough about that to call themselves a guru? Not many, too new. But these videos get you up to speed.
Now their sister site, (or would it be their brother site), is TrainingSpot.com. I haven't made my way through all the pertinent videos on LearnVisualStudio.net, so therefore I haven't even looked at TrainingSpot.com until today. But if you're more of a techie, check out this site. It covers SQL Server Integration Services, Analysis Services, Reporting Services, Migration Assistant, SQL Server 2008, Transact-SQL, and Sharepoint.
Nannette Digresses
Just do me a favor, if you do purchase this or your boss does and lets you watch during working hours, get a decent set of headphones so your cubical mates don't have to listen to your tinny-staticy-noise while they're trying to concentrate. (Nannette's pet peeve when working in a cubical. Okay, nail-clipping and loud gum-chewing and chatting on speaker-phone gets to me too. ;) Fortunately, out of 9 years, I've only worked outside the home in a cubical environment for a total of 19 months. But I digress.
BrainBench
Okay, for my final recommendation, Brainbench.com. Brainbench provides aptitude tests and certifications in numerous technologies, including .NET Programmer, Computer Programmer, Database Administrator, Web Developer and much more.
What I like about BrainBench, is that you can see where your skill levels are on a given topic. For instance, I can study a new technology, go to BrainBench, use their recommended studies for a given topic too, then take a test. Upon completion of the test, I can see where I stand in relation to others who have taken the test. Am I pretty knowledgeable or is there more I need to learn. Their test results tell you what topics you did well at and what topics you failed miserably; thus indicating where you need further study.
Then when you're done, you can even download a little icon indicating your certification to put on your website and link back to your test results, so future employers can see how well you did.
This is also a pay site, but Brainbench also offers several free tests.
Okay, those are my top 3 recommendations for pay sites. But don't forget, each of them also offers limited, free services.
May your dreams be in ASP.NET!
Nannette
In SQL Server, if you wish to copy the structure of an entire database, including the Constraints, Keys, Indexes, etc., don't use the Import option, as it does not include the constraints, keys, indexes, etc. Instead, within SQL Server Management Studio, right click a database, select Tasks, then "Generate Scripts."
Now you may go through the wizard to create a script to copy your entire database. On the "Select Database" page, check the box at the bottom to "Script all objects in the selected database."
On the "Choose Script Options" page, review the True/False settings. Scroll down to the "Table/View Options." If you wish to have a blank set of tables, leave the "Script Data" option False, otherwise, change it to True.
If you wish to have the script create your database, set "Script Database Create" to True, then when the script is created, change the name of the database to the desired new database. Otherwise, create your new database manually, then change the first line of the created script to USE [databasename].
On the Output Option page, I select to "Script to New Query Window," but you could easily save it to a file in order to use the file on another server.
Make sure to change the database name you are creating to your new database, whether you are creating a new database or not. Right click the script window, and select to Execute the script.
Although the server roles will be in the new database, the db_datareader and db_datawriter permissions will not be there, so be sure to add these to the anonymous IISUser role used by your websites.
For further scripting information, please see my previous post on How to Deploy a local MDF Database to a Remote SQL Server Database.
May your dreams be in ASP.NET!
Nannette
More Posts