locked
Access Databind to Label and DDL RRS feed

  • Question

  • User1311411192 posted

    I want to create a code in which I select an Item from a dropdownlist and the corresponding value gets sent to a label.text The dropdownlist is binding to an access database. For example a name would get selected from the dropdownlist then the corresponding name will have a short discription which came from the database this short discription will appear in the label text. Any suggestions for this asp.net visual basic code?

    Sunday, September 12, 2010 1:03 AM

Answers

  • User-1199946673 posted

    what would i write? 

     

            <asp:DropDownList ProductID="DropDownList1" runat="server" AutoPostBack="True" DataSourceProductID="AccessDataSource1" DataTextField="Name" DataValueField="ProductID" />
            <asp:FormView ProductID="FormView1" runat="server" DataKeyNames="ProductID" DataSourceProductID="AccessDataSource2">
                <ItemTemplate>
                    <asp:Label ProductID="Label1" runat="server" Text='<%# Bind("ShortDescription") %>' />
                </ItemTemplate>
            </asp:FormView>
            <asp:AccessDataSource ProductID="AccessDataSource1" runat="server" DataFile="~/App_Data/Halloween.mdb" SelectCommand="SELECT [ProductID], [Name] FROM [Products]">
            </asp:AccessDataSource>
            <asp:AccessDataSource ProductID="AccessDataSource2" runat="server" DataFile="~/App_Data/Halloween.mdb" SelectCommand="SELECT [ProductID], [ShortDescription] FROM [Products] WHERE ([ProductID] = ?)">
                <SelectParameters>
                    <asp:ControlParameter ControlProductID="DropDownList1" Name="ProductID" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
            </asp:AccessDataSource>


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 12, 2010 4:47 PM

All replies

  • User-1828494216 posted

    If you require retriving only 2 "Columns" from Database then fill DropDownList Text Property with Name and Value Property with Description and then display Seleted Value in that textbox or label...

    And if you are trying to do it with filtering, search forum for Filter DropDownList


    Sunday, September 12, 2010 2:58 AM
  • User-1199946673 posted

    Any suggestions for this asp.net visual basic code?

    You can do this withhout writing a single line of code behind:

            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="AccessDataSource1" DataTextField="namefield" DataValueField="ID" />
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="AccessDataSource2">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("descriptionfield") %>' />
                </ItemTemplate>
            </asp:FormView>
            <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/yourdatabase.mdb" SelectCommand="SELECT [ID], [namefield] FROM [table1]">
            </asp:AccessDataSource>
            <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/yourdatabase.mdb" SelectCommand="SELECT [ID], [descriptionfield] FROM [table1] WHERE ([ID] = ?)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" Name="ID" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
            </asp:AccessDataSource>
    


     

    Sunday, September 12, 2010 5:07 AM
  • User1311411192 posted

    This is what I got,, the dropdown is working but when i select an item from the dropdown it still does not give the corressponding items discription to the label

        <form id="form1" runat="server">
        <div>
        
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                DataSourceID="AccessDataSource1" DataTextField="Name" 
                DataValueField="ProductID">
            </asp:DropDownList>
            <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
                DataFile="~/App_Data/Halloween.mdb" 
                SelectCommand="SELECT [ProductID], [Name], [ShortDescription] FROM [Products]">
            </asp:AccessDataSource>
            <br />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" AssociatedControlID="DropDownList1"></asp:Label>
        
        </div>
        </form>
    </body>
    </html>
    


     

    Sunday, September 12, 2010 12:52 PM
  • User-1828494216 posted

    You need to do some codebehind and on SelectedIndexChanged event by passing value to label.

        Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
            Label1.text = DropDownList1.SelectedValue
        End Sub


    Sunday, September 12, 2010 1:05 PM
  • User1311411192 posted

    what would i write? 

    Sunday, September 12, 2010 1:06 PM
  • User-1828494216 posted

    Something like this...

        Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
            Label1.text = DropDownList1.SelectedValue
        End Sub


    Sunday, September 12, 2010 1:08 PM
  • User-1199946673 posted

    You need to do some codebehind and on SelectedIndexChanged event by passing value to label.
     

    No you don't. As I already explained, everything can be done in HTML markup only....

    Sunday, September 12, 2010 4:19 PM
  • User-1828494216 posted

    You need to do some codebehind and on SelectedIndexChanged event by passing value to label.
     

    No you don't. As I already explained, everything can be done in HTML markup only....

    oh please dear Sir, enlighten me!


    how to do it with binding DropDownListValue to Label without any code behind or javascript code? This is the code:

        <form id="form1" runat="server">
        <div>
        
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                DataSourceID="AccessDataSource1" DataTextField="Name" 
                DataValueField="ProductID">
            </asp:DropDownList>
            <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
                DataFile="~/App_Data/Halloween.mdb" 
                SelectCommand="SELECT [ProductID], [Name], [ShortDescription] FROM [Products]">
            </asp:AccessDataSource>
            <br />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" AssociatedControlID="DropDownList1"></asp:Label>
        
        </div>
        </form>
    </body>
    </html>
    



    Sunday, September 12, 2010 4:25 PM
  • User-1199946673 posted

    oh please dear Sir, enlighten me!
     

    If you would read my first reply in this thread, you will see how you can do this by simply putting the label in a formview and bind that to a second datasource....

    Sunday, September 12, 2010 4:29 PM
  • User-1828494216 posted

    oh please dear Sir, enlighten me!
     

    If you would read my first reply in this thread, you will see how you can do this by simply putting the label in a formview and bind that to a second datasource....

    Well he is NOT using that approach, i'm sorry if he did not used what you suggested. BUT him beeing confused and a beginer and then getting the answer that he could make his example (without FormView) work from an guy with 22.000+ answers.... come on... you are smarter than that.

    Sunday, September 12, 2010 4:35 PM
  • User-1199946673 posted

    Well he is NOT using that approach, i'm sorry if he did not used what you suggested. BUT him beeing confused and a beginer and then getting the answer that he could make his example (without FormView) work from an guy with 22.000+ answers.... come on... you are smarter than that.
     

    If you're so smart, then lead him in the right direction! You told him that he needed code behind. I showed that this isn't true, that you could do this without a single line of code behind. I don't see what the difficulty by using a formview is? But if you think your approach by using code behind is easier, please notice that the DataValue Field is ProductID, not ShortDescription. So your code behind is putting the ID field in the label, not the ShortDescription!

    Sunday, September 12, 2010 4:43 PM
  • User-1199946673 posted

    what would i write? 

     

            <asp:DropDownList ProductID="DropDownList1" runat="server" AutoPostBack="True" DataSourceProductID="AccessDataSource1" DataTextField="Name" DataValueField="ProductID" />
            <asp:FormView ProductID="FormView1" runat="server" DataKeyNames="ProductID" DataSourceProductID="AccessDataSource2">
                <ItemTemplate>
                    <asp:Label ProductID="Label1" runat="server" Text='<%# Bind("ShortDescription") %>' />
                </ItemTemplate>
            </asp:FormView>
            <asp:AccessDataSource ProductID="AccessDataSource1" runat="server" DataFile="~/App_Data/Halloween.mdb" SelectCommand="SELECT [ProductID], [Name] FROM [Products]">
            </asp:AccessDataSource>
            <asp:AccessDataSource ProductID="AccessDataSource2" runat="server" DataFile="~/App_Data/Halloween.mdb" SelectCommand="SELECT [ProductID], [ShortDescription] FROM [Products] WHERE ([ProductID] = ?)">
                <SelectParameters>
                    <asp:ControlParameter ControlProductID="DropDownList1" Name="ProductID" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
            </asp:AccessDataSource>


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 12, 2010 4:47 PM
  • User-1828494216 posted

    Well he is NOT using that approach, i'm sorry if he did not used what you suggested. BUT him beeing confused and a beginer and then getting the answer that he could make his example (without FormView) work from an guy with 22.000+ answers.... come on... you are smarter than that.
     

    If you're so smart, then lead him in the right direction! You told him that he needed code behind. I showed that this isn't true, that you could do this without a single line of code behind. I don't see what the difficulty by using a formview is? But if you think your approach by using code behind is easier, please notice that the DataValue Field is ProductID, not ShortDescription. So your code behind is putting the ID field in the label, not the ShortDescription!

    Dear hans_v!

    This is not about whose suggesstion he used. This is not about winning.

    This is about him binding value of DropDownList to Label and you giving him the wrong information.

    AGAIN binding DropDownList value to Label cannot be done without code IF you put it in FromView THEN u don't need code behind. BUT AGAIN without FromView can't be done. Understand now?


    And i won't even go into the debate whose suggestion is better, and what i'm binding to label, so childish...

    Sunday, September 12, 2010 4:53 PM
  • User-1199946673 posted

    AGAIN binding DropDownList value to Label cannot be done without code IF you put it in FromView THEN u don't need code behind. BUT AGAIN without FromView can't be done. Understand now?

    I understand, but where did I say you can do it without code behind and without a formview? I simply say that you can do it without code behind when you use a formview and you don't need to use code behind! Nothing more, nothing less....

    And i won't even go into the debate whose suggestion is better, and what i'm binding to label, so childish...

    Again, where did I say that either approach is better? But if you insist in using code behind, give him a working approach, yours isn't working. My approach (with a formview) is working, so in that sence, this is better....

    Sunday, September 12, 2010 5:01 PM
  • User-1828494216 posted


    HIS POST:

    This is what I got,, the dropdown is working but when i select an item from the dropdown it still does not give the corressponding items discription to the label

    1.     <form id="form1" runat="server">  
    2.     <div>  
    3.       
    4.         <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"   
    5.             DataSourceID="AccessDataSource1" DataTextField="Name"   
    6.             DataValueField="ProductID">  
    7.         </asp:DropDownList>  
    8.         <asp:AccessDataSource ID="AccessDataSource1" runat="server"   
    9.             DataFile="~/App_Data/Halloween.mdb"   
    10.             SelectCommand="SELECT [ProductID], [Name], [ShortDescription] FROM [Products]">  
    11.         </asp:AccessDataSource>  
    12.         <br />  
    13.         <br />  
    14.         <br />  
    15.         <asp:Label ID="Label1" runat="server" AssociatedControlID="DropDownList1"></asp:Label>  
    16.       
    17.     </div>  
    18.     </form>  
    19. </body>  
    20. </html>  
        <form id="form1" runat="server">
        <div>
        
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                DataSourceID="AccessDataSource1" DataTextField="Name" 
                DataValueField="ProductID">
            </asp:DropDownList>
            <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
                DataFile="~/App_Data/Halloween.mdb" 
                SelectCommand="SELECT [ProductID], [Name], [ShortDescription] FROM [Products]">
            </asp:AccessDataSource>
            <br />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" AssociatedControlID="DropDownList1"></asp:Label>
        
        </div>
        </form>
    </body>
    </html>
    

     

    MY ANSWER:

    You need to do some codebehind and on SelectedIndexChanged event by passing value to label.

     

    1. Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged  
    2.     Label1.text = DropDownList1.SelectedValue  
    3. End Sub  
        Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
            Label1.text = DropDownList1.SelectedValue
        End Sub



    YOUR ANSWER:

    You need to do some codebehind and on SelectedIndexChanged event by passing value to label.
     

    No you don't. As I already explained, everything can be done in HTML markup only....


    and about binding the wrong value... i'm posting EXAMPLES NOT COPY/PASTE SOLUTIONS. Is it not better for me to show you how to hunt so you can eat instead me cathing your meal?


    and only for yur happines this is a complete copy/paste solution with codebehind file inside page

    <%@ Page Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
            Label1.text = DropDownList1.SelectedValue
        End Sub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="AccessDataSource1"
                    DataTextField="Name" DataValueField="ShortDescription">
                </asp:DropDownList>
                <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Halloween.mdb"
                    SelectCommand="SELECT [ProductID], [Name], [ShortDescription] FROM [Products]">
                </asp:AccessDataSource>
                <br />
                <br />
                <br />
                <asp:Label ID="Label1" runat="server" AssociatedControlID="DropDownList1"></asp:Label>
            </div>
        </div>
        </form>
    </body>
    </html>


    Sunday, September 12, 2010 5:09 PM
  • User-1199946673 posted

     I know what he wrote, what you responded and what I said. No need to repeat that, it is all in the thread. Who is being childish, and who want to win?At least there now 2 working examples, he may choose whatever he wants.... Good luck!

    Sunday, September 12, 2010 5:17 PM
  • User-1828494216 posted

    You should really ask your selfe "what have i learned from this topic..."

    Answering to a beginner like you did could lead him in the wrong direction (binding ddl value to label without FormView and not using code behind or Js).


    Altrough i am aware that your suggestion is allso working, but your reply to the user asking the question was wrong. That was all. You were just too focused in pushing your solution as top solution and earning points, not seeing the point.


    I allso appologize to vik_d for messing up his post and to moderators and people that had to read this, but i was affraid that reply might lead him in wrong direction and spinning him in circle with no exit, at least we can all learn something from it :)


    Sunday, September 12, 2010 5:30 PM
  • User-1199946673 posted

    Answering to a beginner like you did could lead him in the wrong direction (binding ddl value to label without FormView and not using code behind or Js).

    Why do you think that the points I've came from? because I answer many years to beginners with answers just like this. Just keeping things simple, In this case not writing any code behind because it isn't needed, giving a complete working example.

    but your reply to the user asking the question was wrong.
     

    It was and isn't wrong. It's just a solution using a different approach

    You where just too focused in pushing your solution as top solution and earning points, not seeing the point.

    I think you don't want to see the point! And the points, I really don't care. I just want to help.... 

     

    Sunday, September 12, 2010 6:02 PM