Thursday 28 February 2013

ASP.NET Routing - How to fix error The resource cannot be found.


The resource cannot be found.

 Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

 

watch this example online:


In global.aspx replace the following code
            routes.MapPageRoute(
                "ThirdRoute",
                "Third",
                "~/Third.aspx/{Name}
"
            );

 with:
            routes.MapPageRoute(
                "ThirdRoute",
                "Third/{Name}",
                "~/Third.aspx"

            );


Friday 22 February 2013

ASP.NET - Entity Framework and Model Binding FormView ListView simple example

Watch this example online:



1. For purpose of this tutorial I have simple database with table called StyleMaster.
Table has list of different styles:

 2. First  create new project in Visual Studio 2012.  Add ADO.NET Entity Data Model to it and select StyleMaster table (all details in video).
3. Add new web page.
4. Add FormView to the page
        <asp:FormView ID="FormView1" runat="server" ItemType="WebApplication16.StyleMaster" SelectMethod="GetStyle">
            <ItemTemplate>
                <table>
                    <tr>
                        <td>
                            <%#: Item.StyleDescription %>
                            <br />
                            <%#: Item.StyleName %>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:FormView>
5. Ensure that ItemType is set to WebApplication16.StyleMaster (you may have to rebuild project to get intellisense, also your project most likely will have different name, not WebApplication16)
6. Ensure that SelectMethod is set to GetStyle.
7. Implement GetStyle method:
        public List<StyleMaster> GetStyle()
        {
            using (CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities())
            {
                IQueryable<StyleMaster> result = db.StyleMasters;
                result = result.Where(s => s.StyleId == 3);
                return result.ToList();
            }
        }
8. Run the project.  It will populate form view with info about style id 3.  to make this example more realistic, I will add another page that will display groups.  Each group will have 1 (or more but I will ignore it) style.  Once user click the group - first associated style with that group will be populated in form view.
9. My GroupMaster table looks like this:

10. Update Entity Model (watch video how to do this)
11. Add new web page, name it First Page, set as start page, add list view
        <asp:ListView ID="ListView1" runat="server" ItemType="WebApplication16.GroupMaster" SelectMethod="GetGroups">
            <ItemTemplate>
                <a href="DisplayStyleInFormView.aspx?Id=<%#: Item.GroupId %>"><%#: Item.GroupName %></a>
            </ItemTemplate>
        </asp:ListView>
12. Generate GetGroups function
        public List<GroupMaster> GetGroups()
        {
            using (CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities())
            {
                var result = db.GroupMasters.ToList();
                return result;
            }
        }
13. Modify GetStyle function
        public List<StyleMaster> GetStyle([QueryString("Id")] int? groupId)
        {
            using (CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities())
            {
                IQueryable<StyleMaster> result = db.StyleMasters;
                if (groupId.HasValue && groupId > 0)
                {
                    result = result.Where(s => s.GroupId == groupId);
                }
                else
                {
                    result = result.Where(s => s.StyleId == 3);//default to group id 3
                }
               
                return result.ToList();
            }
        }
14 run.

Thursday 21 February 2013

VB.NET - Excel (INTEROP) How to add new column to existing sheet

       oSheet.Columns("A:A").Insert(Microsoft.Office.Interop.Excel.Constants.xlRight)

ASP.NET Simple Example - Model Binding Entity Framework with ListView


Watch Online


1. For purpose of this tutorial I have simple database with table called GroupMaster.
Table has list of different groups:

2. First  create new project in Visual Studio 2012.  Add ADO.NET Entity Data Model to it and select GroupMaster table (all details in video).
3. Add new web page.
4. Add ListView to the page
        <asp:ListView ID="ListView1" runat="server" ItemType="WebApplication14.GroupMaster"
            SelectMethod="GetGroups">
            <ItemTemplate>
                <a href="/AnotherPage.aspx?id=<%#: Item.GroupId %>"><%#: Item.GroupName %></a>

            </ItemTemplate>
        </asp:ListView>
5. Ensure that ItemType is set to WebApplication14.GroupMaster (you may have to rebuild project to get intellisense)
6. Ensure that SelectMethod is set to GetGroups.
7. Implement GetGroups method:
        public List<GroupMaster> GetGroups()
        {
            using (CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities())
            {
                var result = db.GroupMasters.ToList();
                return result;
            }
        }
8. Run the project.

Wednesday 20 February 2013

Cannot implicitly convert type 'System.Linq.IQueryable<  to 'System.Data.Entity.DbSet<

More Info:

 Replace
        public List<StyleMaster> getStyle([QueryString("id")] int? groupId)
        {
            using (CompanyTestEntities db = new CompanyTestEntities()){
               var  result = db.StyleMasters;
                if (groupId.HasValue && groupId > 0){
                    result = result.Where(s => s.GroupId == groupId);
                }
                return result.ToList();
            }

        }
With

        public List<StyleMaster> getStyle([QueryString("id")] int? groupId)
        {
            using (CompanyTestEntities db = new CompanyTestEntities()){
                IQueryable<StyleMaster> result = db.StyleMasters;
                if (groupId.HasValue && groupId > 0){
                    result = result.Where(s => s.GroupId == groupId);
                }
                return result.ToList();
            }

        }

How to fix error: The operation cannot be completed because the DbContext has been disposed.


Watch online:



Replace

        public IQueryable getGroups()
        {
            using (CompanyTestEntities db = new CompanyTestEntities())
            {
                var result = db.GroupMasters;
                return result;
            }
        }
With

        public List<GroupMaster> getGroups()
        {
            using (CompanyTestEntities db = new CompanyTestEntities())
            {
                var result = db.GroupMasters.ToList();
                return result;
            }
        }