locked
display id and name via sql select statement RRS feed

  • Question

  • Hi experts

    - i have 2 dropdown lists, 1 label & 1 textbox.

    - on selection of 'product categories' @ 1st ddl, 2nd ddl displays all product categories.

    - question is, how may i display the product category id @ the label, and name @ the textbox, on load/selection of 2nd ddl?

    - i have the following code:

      

     

     

    Dim myConn As New

    SqlConnection

     

     

    Dim myCmd As New

    SqlCommand

     

     

    Dim dtrReader As

    SqlDataReader 

     

     

    PublicSub

     

    FilterProductCategory()

     

      

     

       

    myConn.ConnectionString = ConfigurationManager.ConnectionStrings(

    "ConnectionString").ConnectionString    

     

    myConn.Open()

    dtrReader = myCmd.ExecuteReader()

     

    If dtrReader.HasRows

     

    DropDownList2.Items.Clear()

    DropDownList2.DataSource = dtrReader

    DropDownList2.DataValueField =

    "product_category_name"
     

    DropDownList2.DataBind()

     

    End If
     

    dtrReader.Close()

    myConn.Close()

     

    Catch
     

     

    End Try
     

    Sub

     

    End

     

    Then

    Try

    myCmd = myConn.CreateCommand

    myCmd.CommandText =

    "SELECT product_category_id, product_category_name FROM ProductCategory ORDER BY product_category_name"
    • Edited by miiikewong Thursday, December 29, 2011 11:44 AM
    • Moved by Bob Beauchemin Thursday, December 29, 2011 4:39 PM Moved to a more appropriate forum (From:.NET Framework inside SQL Server)
    Thursday, December 29, 2011 11:43 AM

Answers

  • Hi miiikewong,

    Welcome to MSDN Forum.

    Tiya01's and Syed's answer are correct, you need to add two more lines of code in your data binding.

    Please refer to the code below

    //Original
    DropDownList2.DataSource = dtrReader;
    DropDownList2.DataValueField = "product_category_name";
    DropDownList2.DataBind();
    
    
    //Modified
    DropDownList2.DataSource = dtrReader;
    DropDownList2.DataValueField = "product_category_id";
    DropDownList2.DataTextField = "product_category_name"
    DropDownList2.DataBind();

    Then you can get the

    lblId.text=ddlProductCategory.SelectedItem.Value.tostring();

    txtName.text=ddlProductCategory.SelectedItem.text.trim();

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Allen_MSDN Thursday, January 5, 2012 2:06 AM
    Monday, January 2, 2012 3:12 AM

All replies

  • Hi experts

    - i have 2 dropdown lists, 1 label & 1 textbox.

    - on selection of 'product categories' @ 1st ddl, 2nd ddl displays all product categories.

    - question is, how may i display the product category id @ the label, and name @ the textbox, on load/selection of 2nd ddl?

     

    Hi,

     

    The code you provided is of DAL layer. This will be fired when you will change index of your first dropdown.

    Before hitting the DB to fetch data for second drop down you have to as follows (provided code is for UI only)

    I am not very familiar with VB.net.

    Here is how you can do it in C#,

     

    lblId.text=ddlProductCategory.SelectedItem.Value.tostring();

    txtName.text=ddlProductCategory.SelectedItem.text.trim();

     

    Hope this helps!


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful
    • Edited by Tiya01 Thursday, December 29, 2011 11:54 AM
    Thursday, December 29, 2011 11:51 AM
  • Thank you for the reponse Tiya01.

    What i actually hope to achieve is the display of product_category_id at the label, product_category_name at the textbox, after a selection @ the 2nd ddl.

    Is that possible? :)

    Thursday, December 29, 2011 12:04 PM
  • Hi,

    Double click you Second DropDowsList and write the below code in DropDownSelectedIndexChangedEvent

    lblId.text=ddlProductCategory.SelectedItem.Value.tostring();

    txtName.text=ddlProductCategory.SelectedItem.text.trim();

     


    PS.Shakeer Hussain
    Thursday, December 29, 2011 12:55 PM
  • Thank you for the response also.

    Your suggestion is the same as Tiya01's. I still cannot achieve what i need with that.

    Say if i select 'abc' @ the 2nd ddl, the id and name of the selection should appear at the label & textbox.

    Any ideas? :(

    Thursday, December 29, 2011 1:02 PM
  • Hi miiikewong,

    Welcome to MSDN Forum.

    Tiya01's and Syed's answer are correct, you need to add two more lines of code in your data binding.

    Please refer to the code below

    //Original
    DropDownList2.DataSource = dtrReader;
    DropDownList2.DataValueField = "product_category_name";
    DropDownList2.DataBind();
    
    
    //Modified
    DropDownList2.DataSource = dtrReader;
    DropDownList2.DataValueField = "product_category_id";
    DropDownList2.DataTextField = "product_category_name"
    DropDownList2.DataBind();

    Then you can get the

    lblId.text=ddlProductCategory.SelectedItem.Value.tostring();

    txtName.text=ddlProductCategory.SelectedItem.text.trim();

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Allen_MSDN Thursday, January 5, 2012 2:06 AM
    Monday, January 2, 2012 3:12 AM