Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5.1 > BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 2nd, 2018, 10:32 PM
Authorized User
Points: 222, Level: 4
Points: 222, Level: 4 Points: 222, Level: 4 Points: 222, Level: 4
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default Populating DDL within a FormView

I'm really confused as to what I'm doing incorrectly:

I've created a formview with default mode = Insert.
Within the formview - I've placed a drop down list in the InsertItemTemplate tags.
The DDL ID is "ArmorCategory".
OnDataBinding="ArmorCategory_getData"

Here is the code behind (I am getting a System.StackOverFlowException (no further details) - what am I doing wrong?:

Code:
    protected void ArmorCategory_getData(object sender, EventArgs e)
    {
        SetProductDropDown(GetProductsForDropDown(), (DropDownList)sender);
    }

    private IQueryable GetProductsForDropDown()

    {

        HolyRollersEntities db = new HolyRollersEntities();

        var items = from p in db.tb_Decode
                    where p.tb_DecodeHeaders.DecodeNumber == 4
                    select new

                    {

                        Text = p.DecodeDescription,
                        Value = p.DecodeNumber

                    };

        return items;

    }

    private void SetProductDropDown(IQueryable products, DropDownList ddl)

    {

        ddl.DataSource = products;

        ddl.DataTextField = "Text";

        ddl.DataValueField = "Value";

        ddl.DataBind();

    }
Reply With Quote
  #2 (permalink)  
Old April 3rd, 2018, 06:34 AM
Imar's Avatar
Wrox Author
Points: 71,540, Level: 100
Points: 71,540, Level: 100 Points: 71,540, Level: 100 Points: 71,540, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,998
Thanks: 79
Thanked 1,572 Times in 1,549 Posts
Default

You're databinding from the method that gets called when you databind. This means that ArmorCategory_getData is fired which ultimately calls SetProductDropDown which calls DataBind again on the DLL. Either call this from, say, OnLoad or don't call DataBind()

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old April 3rd, 2018, 07:44 AM
Authorized User
Points: 222, Level: 4
Points: 222, Level: 4 Points: 222, Level: 4 Points: 222, Level: 4
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default

That's what I had finally discovered.
So, the issue is now - when I try to call this from OnLoad, I can't seem to reference the ArmorCategory (ID) dropdownlist.

I've tried FindControl (no good).
I've tried this.(whatever) (no good).
I've tried using the ID (ArmorCategory.etc.) (no good).
How do I reference this ddl - so I can add the datasource?

Actually...

All I want to do (please see markup below) is create a form for inserting armor records. I want a drop down list on the form that allows me to select the category for the Armor I'm inserting. This category comes from the DB (the DB tables - decode & decode header) Shouldn't this be easier than I'm trying to make it (with all the functions in the code behind)?

Code:
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="ArmorID" DefaultMode="Insert">
        <InsertItemTemplate>
            <table>
                <tr>
                   <th>Armor Category:</th>                        
                   <td>
                       <asp:DropDownList ID="ArmorCategory" Name="ArmorCategory" runat="server" >
                       </asp:DropDownList>
                   </td>
                </tr>
                <tr>
                    <th>Armor Description:</th>
                   <td>
                       <asp:TextBox ID="ArmorDescription" runat="server" Text='<%# Bind("ArmorName") %>'>

                       </asp:TextBox>
                   </td> 
                </tr>
            </table>
            <asp:Button ID="InsertButton" runat="server" Text="Insert" CommandName="Insert" />
        </InsertItemTemplate>
    </asp:FormView>
</asp:Content>
Reply With Quote
  #4 (permalink)  
Old April 3rd, 2018, 09:16 AM
Imar's Avatar
Wrox Author
Points: 71,540, Level: 100
Points: 71,540, Level: 100 Points: 71,540, Level: 100 Points: 71,540, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,998
Thanks: 79
Thanked 1,572 Times in 1,549 Posts
Default

Have you tried data binding the parent FormView instead?
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old April 3rd, 2018, 10:22 AM
Authorized User
Points: 222, Level: 4
Points: 222, Level: 4 Points: 222, Level: 4 Points: 222, Level: 4
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Wouldn't that be for editing data coming into the form itself?

In other words, if I wanted to edit the Armor Name.
All I want for this form was to INSERT the Armor Name (and Armor Category).
There are other fields as well, but i'm simplifying here (ex: Armor Weight, Armor Size, etc.)

I WILL have a code-behind method for the InsertMethod on the formview, yes, but that's afterwards. First, I need to figure out how to simply populate the Armor Category drop-down-list with data from the tb_Decode table.

That is - a text and value. Example. Value = 40; Text=Light Armor
And yes I will need that data bound when I insert it - but that's another question for another time.

How do I populate this dropdownlist? I've got the LINQ query and methods in the code-behind. But I can't reference the control (ddl)...do I need to reference it node-style (ie: Parent-Child) As a child of the formview - or of the ItemTemplate? I'm still unclear on how this works. Do you have a reference link you can send me to for a better understanding? Alot of code is behind the scenes when these controls get created and I'm not sure if objects have been instantiated on them already or if I need to do that myself. I was able to instantiate an object on the sender when the method was OnDataBinding, but that's obviously not the right method. I tried SelectMethod on the drop down list but that did not work either.
Reply With Quote
  #6 (permalink)  
Old April 3rd, 2018, 10:27 AM
Imar's Avatar
Wrox Author
Points: 71,540, Level: 100
Points: 71,540, Level: 100 Points: 71,540, Level: 100 Points: 71,540, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,998
Thanks: 79
Thanked 1,572 Times in 1,549 Posts
Default

Did you try it?
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #7 (permalink)  
Old April 3rd, 2018, 10:35 AM
Authorized User
Points: 222, Level: 4
Points: 222, Level: 4 Points: 222, Level: 4 Points: 222, Level: 4
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default

When you ask if I tried it, I don't follow - sorry.

Try what? OnDataBinding in the formview? Doesn't the formview directly relate to the Armor table and not the decode table?

If I tried Data Binding in the formview - what would I do?

The only thing I know how to do on the FormView level would be to create the InsertMethod. I could create a SelectMethod - but I don't want to bring in Armor data - as I'm only inserting NEW Armor records.
Reply With Quote
  #8 (permalink)  
Old April 3rd, 2018, 11:08 AM
Authorized User
Points: 222, Level: 4
Points: 222, Level: 4 Points: 222, Level: 4 Points: 222, Level: 4
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Okay, on a side note:

I was able to figure out how to locate and reference the control - although it seems to be such a roundabout way of doing things

However, even though I got that working, now I'm getting the following error (Note - this seems like overkill to me to just populate a ddl with db values. I thought I WAS using Model Binding!):
Quote:
System.NotSupportedException: 'Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. ...
For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592.'

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _NewArmor : BasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)FormView1.Row.Cells[0].FindControl("ArmorCategory");
        SetProductDropDown(GetProductsForDropDown(),ddl);
    }

    private IQueryable GetProductsForDropDown()

    {

        HolyRollersEntities db = new HolyRollersEntities();

        var items = from p in db.tb_Decode
                    where p.tb_DecodeHeaders.DecodeNumber == 4
                    select new

                    {

                        Text = p.DecodeDescription,
                        Value = p.DecodeNumber

                    };

        return items;

    }

    private void SetProductDropDown(IQueryable products, DropDownList myControl1)

    {

        myControl1.DataSource = products;

        myControl1.DataTextField = "Text";

        myControl1.DataValueField = "Value";

        myControl1.DataBind();

    }
}
Reply With Quote
  #9 (permalink)  
Old April 3rd, 2018, 12:25 PM
Imar's Avatar
Wrox Author
Points: 71,540, Level: 100
Points: 71,540, Level: 100 Points: 71,540, Level: 100 Points: 71,540, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,998
Thanks: 79
Thanked 1,572 Times in 1,549 Posts
Default

As the error message say, you can't bind to a query which is what you're doing. Instead, call ToList on items before you return it to execute the query.

Also, your original example should just work. Here's the simplest example I can get working:

Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="Id" DataSourceID="EntityDataSource1" DefaultMode="Insert">
                <InsertItemTemplate>
                    <br />
                    Name:
                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                    <br />
                    GenreId:
                    <asp:DropDownList ID="DropDownList1" DataValueField="Id" DataTextField="Name" runat="server" OnDataBinding="ArmorCategory_getData"></asp:DropDownList>

                    <br />
                    Genre:
                    <br />
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
                    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </InsertItemTemplate>
            </asp:FormView>
            <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=TestEntities" DefaultContainerName="TestEntities" EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True" EntitySetName="Reviews">
            </asp:EntityDataSource>
        </div>
    </form>
</body>
</html>
and the code behind
Code:
using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace WebApplication6
{
  public partial class Default : System.Web.UI.Page
  {
    protected void ArmorCategory_getData(object sender, EventArgs e)
    {
      SetProductDropDown((DropDownList)sender);
    }

    private void SetProductDropDown(DropDownList ddl)
    {
      var context = new TestEntities();
      ddl.DataSource = context.Genres.Select(x => new {x.Id, x.Name}).ToList();
    }
  }
}
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #10 (permalink)  
Old April 3rd, 2018, 12:45 PM
Authorized User
Points: 222, Level: 4
Points: 222, Level: 4 Points: 222, Level: 4 Points: 222, Level: 4
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Ok.

This worked:

Code:
        var context = new HolyRollersEntities();
        ddl.DataSource = context.tb_Decode.Select(x => new { x.DecodeID, x.DecodeDescription }).ToList();
With the exception that I lost my filter (where DecodeNumber == 4)
Where do I incorporate that into the one-liner above?

I could look it up myself, but I don't even know how to GOOGLE such a statement. It's not like I learned (using the LINQ query).
What do you even call the statement above (using the x=>.etc...)?

Finally - after I figure out how to filter my list, I need to use the value the user selects when I insert the data. Do I put that in a SelectedValue attribute and bind it <%# Bind("ArmorCategory") %>. Or do I just do it in the InsertMethod code-behind?

Sorry, for so many questions - but the concepts you explain in the book don't seem to cover this - unless I read something wrong?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Editable ddl? FOD ASP.NET 4 General Discussion 0 May 31st, 2011 10:37 PM
XmlDataSource DDL and Gridview rthomson77@hotmail.com XML 4 January 21st, 2009 09:57 AM
how can selectedvalue of DDL make other DDL visibl hertendreef ASP.NET 2.0 Professional 4 May 20th, 2008 04:38 PM
problem with populating ddl in edit of detailsview hertendreef ASP.NET 2.0 Professional 1 August 16th, 2007 12:21 PM
DDL error cmw231 ASP.NET 2.0 Basics 2 July 17th, 2006 08:07 AM



All times are GMT -4. The time now is 06:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.