0

I am looking for an example of how to populate a dropdown list from one SQL data source and set the selected field from another, it is basically exactly the same thing that is asked here http://forums.asp.net/p/793752/793955.aspx there is a reply further down (http://forums.asp.net/post/793978.aspx) which is supposed to work but when I copy and paste the code it is giving me lots of errors. I am looking for an ASP.NET 2 example that is coded in C#.

The answer seems to suggest that it is possible to do without writing any code behind, is this correct?

Ultimately I want to use this as part of a much more complicated form where an undefined number of similar dropdowns are created dynamically inside a repeater control.

4
  • What sort of errors did you get? If you simply copied and pasted his code, you would need to change all the "s to ' marks (and so on) Commented Dec 8, 2011 at 15:18
  • @sq33G I probably should have mentioned that I have done all of the " (although there code be something that I have missed) and that I am new to ASP.NET, but your response doesnt anwser my question "The answer seems to suggest that it is possible to do without writing any code behind, is this correct?" Commented Dec 8, 2011 at 15:52
  • ...that's why it's a comment, not an answer. :) Welcome to Stack Overflow. Your comment, by the way, doesn't answer my question of "what sort of errors did you get?" Commented Dec 8, 2011 at 15:54
  • I am considering abandoning the question and then rephasing it without the example I have found as I suspect it is going to confuse everything but to answer your question some example errors include: Error 1 Newline in constant C:\projects\net\Temp\WebApplication5\WebApplication5\Default.aspx 1 1 WebApplication5 Error 2 Too many characters in character literal C:\projects\net\Temp\WebApplication5\WebApplication5\Default.aspx 1 1 WebApplication5 I can post the code as I have it if it will help? Commented Dec 8, 2011 at 16:03

2 Answers 2

1

The problem is that the example had a few typos due to conversion to HTML. Here is a copy of that page that should work correctly:

<%@  page="" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FormView DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" ID="FormView1"
            runat="server">
            <EditItemTemplate>
                CustomerID:
                <asp:Label ID="CustomerIDLabel1" runat="server" Text="<%# Eval("CustomerID") %>"
                    gt="" br="">CompanyName:
                    <asp:TextBox ID="CompanyNameTextBox" runat="server" Text="<%# Bind("CompanyName") %>"></asp:TextBox><br />
                    ContactName:
                    <asp:TextBox ID="ContactNameTextBox" runat="server" Text="<%# Bind("ContactName") %>"></asp:TextBox><br />
                    ContactTitle:
                    <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="ContactTitle"
                        DataTextField="ContactTitle" DataSourceID="SqlDataSource1" SelectedValue="<%# Bind("ContactTitle") %>">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"
                        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"></asp:SqlDataSource>
                    <br />
                    <asp:LinkButton ID="UpdateButton" runat="server" Text="Update" CommandName="Update"
                        CausesValidation="True"></asp:LinkButton>&nbsp;<asp:LinkButton ID="UpdateCancelButton"
                            runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False"></asp:LinkButton>
                </asp:Label></EditItemTemplate>
            <ItemTemplate>
                CustomerID:
                <asp:Label ID="CustomerIDLabel" runat="server" Text="<%# Eval("CustomerID") %>"></asp:Label><br />
                CompanyName:
                <asp:Label ID="CompanyNameLabel" runat="server" Text="<%# Bind("CompanyName") %>"></asp:Label><br />
                ContactName:
                <asp:Label ID="ContactNameLabel" runat="server" Text="<%# Bind("ContactName") %>"></asp:Label><br />
                ContactTitle:
                <asp:Label ID="ContactTitleLabel" runat="server" Text="<%# Bind("ContactTitle") %>"></asp:Label><br />
                <asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit"></asp:Button></ItemTemplate>
            <asp:sqldatasource id="SqlDataSource1" runat="server" selectcommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]"
                connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>" updatecommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle WHERE [CustomerID] = @original_CustomerID">
            </asp:sqldatasource>
            <div>
            </div>
        </asp:FormView>
    </div>
    </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for trying but Visual studio 2005 doesnt like that either. page="" should be Page and the first label tag is not closed correctly and the quotes for Eval and Bind should be single rather than double. I will post a working example if I get it to compile.
0

I have managed to correct the example so that it compiles and it does indeed demonstrate how to populate from one data source and set the selected value from another. The working code is below, but first here are some things that caught me out which may help other beginners:

  • Eval is used when you only want to read the value, use bind if you need to read and update it.
  • If you are setting the text property of an object via Bind or Eval, the outer and inner quotes need to be different styles one must be single quotes and one must be double quotes but it doesn’t seem to matter which.
  • If you are copying and pasting the example you will need to change the Inherits directive in the first line.

Here is the working code:

<%@  Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="WebApplication6._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:SqlDataSource id="SqlDataSource1" runat="server" selectcommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle] FROM [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>" updatecommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle WHERE [CustomerID] = @CustomerID">
    </asp:sqldatasource>
    <asp:FormView DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" ID="FormView1" runat="server">
      <EditItemTemplate>
        CustomerID:
        <asp:Label ID="CustomerIDLabel" runat="server" Text="<%# Bind('CompanyName') %>"></asp:Label><br />
        CompanyName:
        <asp:TextBox ID="CompanyNameTextBox" runat="server" Text="<%# Bind('CompanyName') %>"></asp:TextBox><br />
        ContactName:
        <asp:TextBox ID="ContactNameTextBox" runat="server" Text="<%# Bind('ContactName') %>"></asp:TextBox><br />
        ContactTitle:
        <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="ContactTitle" DataTextField="ContactTitle"
                            DataSourceID="SqlDataSource2" SelectedValue="<%# Bind('ContactTitle') %>"></asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"
                            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"></asp:SqlDataSource><br />
        <asp:LinkButton ID="UpdateButton" runat="server" Text="Update" CommandName="Update" CausesValidation="True"></asp:LinkButton>
        <asp:LinkButton ID="UpdateCancelButton" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False"></asp:LinkButton>
      </EditItemTemplate>
      <ItemTemplate>
        CustomerID:
        <asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>'></asp:Label><br />
        CompanyName:
        <asp:Label ID="CompanyNameLabel" runat="server" Text="<%# Bind('CompanyName') %>"></asp:Label><br />
        ContactName:
        <asp:Label ID="ContactNameLabel" runat="server" Text="<%# Bind('ContactName') %>"></asp:Label><br />
        ContactTitle:
        <asp:Label ID="ContactTitleLabel" runat="server" Text="<%# Bind('ContactTitle') %>"></asp:Label><br />
        <asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit"></asp:Button>
        <div>
        </div>
      </ItemTemplate>
    </asp:FormView>
  </div>
  </form>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.