9 Replies - 1636 Views - Last Post: 23 August 2012 - 08:39 AM Rate Topic: -----

#1 richad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-April 12

Problem filling the dropdown from a sql command

Posted 22 August 2012 - 12:19 AM

Good afternoon people have been trying to fill a dropdown using a sql command, until so good, when I click on the dropdown it shows all the items, but when I try to click on an item in the dropdown it always returns the first item in the dropdown .... follows the codes, what i want to do is get the selected value and item from the dropdown and save it on a label for future use.

I appreciate all the support possible,

<asp:DropDownList ID="DropDownPerfil" runat="server" Height="16px" Width="202px"  CssClass="DropBorderColor"> </asp:DropDownList>


 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        utilizador.Text = Me.Context.User.Identity.Name
        '   btnRefresh.Attributes.Add("onclick", "<script language=javascript1.2>window.location.reload(true);</script>")

        If (Not Page.IsPostBack) Then

            '  MsgBox(DropDownPerfil.SelectedItem.Text)
            escolhePerfil()

        End If
        'DropDownPerfil.DataBind()
        lbperfil2.Text = DropDownPerfil.SelectedItem.Text
        lbnome.Text = DropDownPerfil.SelectedValue




    End Sub


    Function escolhePerfil() As Boolean
        Dim connstring As String = "Data Source=10.2.24.17;Persist Security Info=True;User ID=sa;Password=Pr0dUn1C0$qL;database=ePrimavera"
        Dim SQLData As New System.Data.SqlClient.SqlConnection(connstring)

        Dim cmdSelect As New System.Data.SqlClient.SqlCommand("SELECT  u.WindowsUser,u.Email ,g.Description, u.Login FROM [ePrimavera].[dbo].[PLT_Users] as  u,[ePrimavera].[dbo].[PLT_UserGroups] as ug, [ePrimavera].[dbo].[PLT_Groups] as g where u.ID = ug.UserID And ug.GroupID = g.ID and u.WindowsUser like 'bancounico\" & utilizador.Text & "'", SQLData)
        SQLData.Open()

        Dim dtrReader As System.Data.SqlClient.SqlDataReader = cmdSelect.ExecuteReader()

        If dtrReader.HasRows Then


            DropDownPerfil.DataValueField = "Login"
            DropDownPerfil.DataTextField = "Description"
            DropDownPerfil.DataSource = dtrReader
            DropDownPerfil.DataBind()
            ' DropDownPerfil.Items.Insert(0, New ListItem("Seleccione o Perfil", ""))

        End If
        SQLData.Close()

        Return True
    End Function


Is This A Good Question/Topic? 0
  • +

Replies To: Problem filling the dropdown from a sql command

#2 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 395
  • Joined: 04-January 11

Re: Problem filling the dropdown from a sql command

Posted 22 August 2012 - 01:54 AM

Okay... from what I understood from your post, you are able to fill the datasource correctly on page load, is that right?

So does that mean you do not really have a problem with filling your dropdownlists? Instead you have a problem when you try to select an item.

I can see you are creating web application, so is your dropdownlist's autopostback property set to true? Have you tried inserting breakpoints and debugging your application?
Was This Post Helpful? 0
  • +
  • -

#3 richad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-April 12

Re: Problem filling the dropdown from a sql command

Posted 22 August 2012 - 02:33 AM

Yes...I am having problem selecting the dropdown

I did, when i put the postback it always return the first value, when i dont put the postback it stays in the selected item, but when i use the metod .selectedvalue it still return the first value.
Was This Post Helpful? 0
  • +
  • -

#4 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 395
  • Joined: 04-January 11

Re: Problem filling the dropdown from a sql command

Posted 22 August 2012 - 02:51 AM

Have you tried putting the following code in SelectedIndexChanged event instead of Page_Load

lbperfil2.Text = DropDownPerfil.SelectedItem.Text  
lbnome.Text = DropDownPerfil.SelectedValue  


Was This Post Helpful? 0
  • +
  • -

#5 richad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-April 12

Re: Problem filling the dropdown from a sql command

Posted 22 August 2012 - 02:53 AM

Yes i did, for some reason the dropdown returns always the first value...
Was This Post Helpful? 0
  • +
  • -

#6 richad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-April 12

Re: Problem filling the dropdown from a sql command

Posted 22 August 2012 - 05:17 AM

View PostNovember-06, on 22 August 2012 - 02:51 AM, said:

Have you tried putting the following code in SelectedIndexChanged event instead of Page_Load

lbperfil2.Text = DropDownPerfil.SelectedItem.Text  
lbnome.Text = DropDownPerfil.SelectedValue  



Yes i did, for some reason the dropdown returns always the first value...
Was This Post Helpful? 0
  • +
  • -

#7 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 395
  • Joined: 04-January 11

Re: Problem filling the dropdown from a sql command

Posted 22 August 2012 - 06:51 PM

Are lblperfil2.Text and lblnome.Text both stored with wrong values?

Have you tried SelectedItem.Value instead of SelectedValue?
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Problem filling the dropdown from a sql command

Posted 22 August 2012 - 10:19 PM

Wait a minute, it is an ASP page. Do you have any idea what Server side code means?
When you select an item and need some code to be executed, the code is executed into server and the page is reloaded again (postback). That means it wont remember any previous selection because the page loads again.
So dont bother about selected value or selected item, you have to store your data for usage after post back, you can use session to pass those data. So you will remember what you have selected, and this is why it is not advised to execute server code for events like selection changed or like, because that .net code is executed in server which will need page reload.

And because this is an ASP question, I am moving this topic to ASP.Net forum for better replies.
<Topic Moved To Asp.Net forum>
Was This Post Helpful? 0
  • +
  • -

#9 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Problem filling the dropdown from a sql command

Posted 23 August 2012 - 05:16 AM

wheres the code for the index changed event?
Was This Post Helpful? 0
  • +
  • -

#10 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: Problem filling the dropdown from a sql command

Posted 23 August 2012 - 08:39 AM

What this is getting to is that you're trying to treat server-side code as client-side code. Without a post-back, the server doesn't know that you've selected anything different. As Nakor pointed out, you need to use the server IndexChanged event to update those values. You'll need to create an event handler for the SelectedIndexChanged event, and reference it from your DDL control (onselectedIndexChanged="DropDownPerfil_SelectedIndexChanged). That is how you would handle it all server-side; you can also keep it client-side and use Javascript to update the labels, and read from them when you finally do a post-back.

EDIT: If you're trying to use labels to display the data from the dropdown, Javascript would be the better way to go. Using the selectedindexchanged event for something that simple is overkill, IMHO. When you finally submit or postback, you can use server-side code to read from the labels.

This post has been edited by h4nnib4l: 23 August 2012 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1