ASP.NET: Populating a dropdown with a datavalue field different from the datatext field












0















I am trying to populate a drop-down list with data returned by a SQL Server stored procedure but make it so that the data value text field is one field returned by the stored procedure and the data value field is another value returned by the stored procedure (the stored procedure returns a field called 'Parent Name' and another field called 'Parent ID'). The code behind is VB and I will post it below:



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim s_SQL_Database_Connection As SqlConnection

Dim s_Connection_String As String = Session("Connection_String")

Dim s_SQL_Command As SqlCommand

Dim s_Record_Set As SqlDataReader

s_SQL_Database_Connection = New SqlConnection(s_Connection_String)

s_SQL_Database_Connection.Open()

s_SQL_Command = New SqlCommand("EXEC Krandor.MattA.__Parent_List", s_SQL_Database_Connection)

s_Record_Set = s_SQL_Command.ExecuteReader()

Dim parent_name As String
Dim parent_id As Integer

While s_Record_Set.Read
parent_name = s_Record_Set.Item("Parent Name")
Me.cbo_parent.Items.Add(New ListItem(parent_name))
End While

s_Record_Set.Close()

s_SQL_Database_Connection.Close()

End Sub


Public ReadOnly Property parent_id() As String
Get
Dim s_parent As String

s_parent = cbo_parent.DataValueField

Return s_parent
End Get
End Property


Basically, in the while loop, I want to set 'Parent Name' to the text field for each drop-down item and 'Parent ID' equal to the value field for each drop-down item. Any help will be greatly appreciated and I apologize if this question has been asked already (I searched but could not find anything that I could apply to my situation).










share|improve this question





























    0















    I am trying to populate a drop-down list with data returned by a SQL Server stored procedure but make it so that the data value text field is one field returned by the stored procedure and the data value field is another value returned by the stored procedure (the stored procedure returns a field called 'Parent Name' and another field called 'Parent ID'). The code behind is VB and I will post it below:



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim s_SQL_Database_Connection As SqlConnection

    Dim s_Connection_String As String = Session("Connection_String")

    Dim s_SQL_Command As SqlCommand

    Dim s_Record_Set As SqlDataReader

    s_SQL_Database_Connection = New SqlConnection(s_Connection_String)

    s_SQL_Database_Connection.Open()

    s_SQL_Command = New SqlCommand("EXEC Krandor.MattA.__Parent_List", s_SQL_Database_Connection)

    s_Record_Set = s_SQL_Command.ExecuteReader()

    Dim parent_name As String
    Dim parent_id As Integer

    While s_Record_Set.Read
    parent_name = s_Record_Set.Item("Parent Name")
    Me.cbo_parent.Items.Add(New ListItem(parent_name))
    End While

    s_Record_Set.Close()

    s_SQL_Database_Connection.Close()

    End Sub


    Public ReadOnly Property parent_id() As String
    Get
    Dim s_parent As String

    s_parent = cbo_parent.DataValueField

    Return s_parent
    End Get
    End Property


    Basically, in the while loop, I want to set 'Parent Name' to the text field for each drop-down item and 'Parent ID' equal to the value field for each drop-down item. Any help will be greatly appreciated and I apologize if this question has been asked already (I searched but could not find anything that I could apply to my situation).










    share|improve this question



























      0












      0








      0








      I am trying to populate a drop-down list with data returned by a SQL Server stored procedure but make it so that the data value text field is one field returned by the stored procedure and the data value field is another value returned by the stored procedure (the stored procedure returns a field called 'Parent Name' and another field called 'Parent ID'). The code behind is VB and I will post it below:



      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim s_SQL_Database_Connection As SqlConnection

      Dim s_Connection_String As String = Session("Connection_String")

      Dim s_SQL_Command As SqlCommand

      Dim s_Record_Set As SqlDataReader

      s_SQL_Database_Connection = New SqlConnection(s_Connection_String)

      s_SQL_Database_Connection.Open()

      s_SQL_Command = New SqlCommand("EXEC Krandor.MattA.__Parent_List", s_SQL_Database_Connection)

      s_Record_Set = s_SQL_Command.ExecuteReader()

      Dim parent_name As String
      Dim parent_id As Integer

      While s_Record_Set.Read
      parent_name = s_Record_Set.Item("Parent Name")
      Me.cbo_parent.Items.Add(New ListItem(parent_name))
      End While

      s_Record_Set.Close()

      s_SQL_Database_Connection.Close()

      End Sub


      Public ReadOnly Property parent_id() As String
      Get
      Dim s_parent As String

      s_parent = cbo_parent.DataValueField

      Return s_parent
      End Get
      End Property


      Basically, in the while loop, I want to set 'Parent Name' to the text field for each drop-down item and 'Parent ID' equal to the value field for each drop-down item. Any help will be greatly appreciated and I apologize if this question has been asked already (I searched but could not find anything that I could apply to my situation).










      share|improve this question
















      I am trying to populate a drop-down list with data returned by a SQL Server stored procedure but make it so that the data value text field is one field returned by the stored procedure and the data value field is another value returned by the stored procedure (the stored procedure returns a field called 'Parent Name' and another field called 'Parent ID'). The code behind is VB and I will post it below:



      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim s_SQL_Database_Connection As SqlConnection

      Dim s_Connection_String As String = Session("Connection_String")

      Dim s_SQL_Command As SqlCommand

      Dim s_Record_Set As SqlDataReader

      s_SQL_Database_Connection = New SqlConnection(s_Connection_String)

      s_SQL_Database_Connection.Open()

      s_SQL_Command = New SqlCommand("EXEC Krandor.MattA.__Parent_List", s_SQL_Database_Connection)

      s_Record_Set = s_SQL_Command.ExecuteReader()

      Dim parent_name As String
      Dim parent_id As Integer

      While s_Record_Set.Read
      parent_name = s_Record_Set.Item("Parent Name")
      Me.cbo_parent.Items.Add(New ListItem(parent_name))
      End While

      s_Record_Set.Close()

      s_SQL_Database_Connection.Close()

      End Sub


      Public ReadOnly Property parent_id() As String
      Get
      Dim s_parent As String

      s_parent = cbo_parent.DataValueField

      Return s_parent
      End Get
      End Property


      Basically, in the while loop, I want to set 'Parent Name' to the text field for each drop-down item and 'Parent ID' equal to the value field for each drop-down item. Any help will be greatly appreciated and I apologize if this question has been asked already (I searched but could not find anything that I could apply to my situation).







      asp.net sql-server






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 9:48









      Mohsin Mehmood

      2,3112513




      2,3112513










      asked Nov 25 '18 at 1:53









      MATTHEWMATTHEW

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can pass the Parent ID field value as a second parameter to ListItem constructor:



           While s_Record_Set.Read
          parent_name = s_Record_Set.Item("Parent Name")
          Me.cbo_parent.Items.Add(New ListItem(parent_name, s_Record_Set.Item("Parent ID")))
          End While





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464013%2fasp-net-populating-a-dropdown-with-a-datavalue-field-different-from-the-datatex%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can pass the Parent ID field value as a second parameter to ListItem constructor:



             While s_Record_Set.Read
            parent_name = s_Record_Set.Item("Parent Name")
            Me.cbo_parent.Items.Add(New ListItem(parent_name, s_Record_Set.Item("Parent ID")))
            End While





            share|improve this answer




























              0














              You can pass the Parent ID field value as a second parameter to ListItem constructor:



               While s_Record_Set.Read
              parent_name = s_Record_Set.Item("Parent Name")
              Me.cbo_parent.Items.Add(New ListItem(parent_name, s_Record_Set.Item("Parent ID")))
              End While





              share|improve this answer


























                0












                0








                0







                You can pass the Parent ID field value as a second parameter to ListItem constructor:



                 While s_Record_Set.Read
                parent_name = s_Record_Set.Item("Parent Name")
                Me.cbo_parent.Items.Add(New ListItem(parent_name, s_Record_Set.Item("Parent ID")))
                End While





                share|improve this answer













                You can pass the Parent ID field value as a second parameter to ListItem constructor:



                 While s_Record_Set.Read
                parent_name = s_Record_Set.Item("Parent Name")
                Me.cbo_parent.Items.Add(New ListItem(parent_name, s_Record_Set.Item("Parent ID")))
                End While






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 9:51









                Mohsin MehmoodMohsin Mehmood

                2,3112513




                2,3112513
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464013%2fasp-net-populating-a-dropdown-with-a-datavalue-field-different-from-the-datatex%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python