Solved

Dynamically default selected item of a dropdown control in vb.net

Posted on 2008-10-30
2
581 Views
Last Modified: 2012-05-05
Hello,
This was put in a class file (say the name is GenericClass.vb)
 
   Dim sqlConn As OracleConnection

    'fill Level ID COMBO BOXES
    Public Sub LevelId(ByRef ddlLvlIdFrom As DropDownList)
        Try
            sqlConn = ConnectionManager.GetNewConnection()

            Using sqlConn
                Dim command As OracleCommand = New OracleCommand("SELECT DEPTID FROM DEPT ORDER BY DEPTID", sqlConn)
                sqlConn.Open()
                Dim reader As OracleDataReader = command.ExecuteReader()

                If reader.HasRows Then
                    Do While reader.Read()
                    ddlLvlIdFrom.Items.Add(reader(0).ToString)     'ddlLvlIdFrom name of the dropdown              
                    Loop
                End If

                reader.Close()
            End Using
           Finally
            If Not (sqlConn Is Nothing) Then
                sqlConn.Close()
            End If
        End Try
    End Sub
   
    The query above retrieves data as :
   
    0000
    1234
    1345
    2111
   
Another funtion in the same class is:
 
 Public Function GetUserInfo(ByVal str As String) As String
         Dim ret As String = Nothing
 
         Try
             sqlConn = ConnectionManager.GetNewConnection()
 
             Dim sql As String = "SELECT D.DEPTID, E.AENM FROM EMP E, DEPT D" & _
                                 " WHERE E.ID = D.ID AND E.SYSNAME ='mamun'"
                 Using sqlConn
                 sqlConn.Open()
                 Dim oraCmd As OracleCommand = New OracleCommand(Sql, sqlConn)
                 Dim reader As OracleDataReader = oraCmd.ExecuteReader()
                 Dim strLevel As String = ""
                 Dim strName As String = ""
 
                 If reader.Read Then
                     If str = "LEVEL" Then ret = reader.GetString(0)
                     If str = "NAME" Then ret = reader.GetString(1)
                 End If
                 reader.Close()
             End Using
 
         Finally
             If Not (sqlConn Is Nothing) Then
                 sqlConn.Close()
             End If
         End Try
 
         Return ret
 
    End Function
   
This query returns:

1111      John Doe
1345      Mamun
2111      Plumber John

Now, in the page load event of the page1.aspx, when I display the contents of the page, I want to set the default selected value of the drop down box to be based on the user information obtained from the windows login.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim obj As New GenericClass        
        obj.LevelId(ddlLvlIdFrom) 'here I want to show the default to be selected mine which is 1345 and the rest of the data
   
   End Sub
   

How can I call the GetUserInfo for the dropdown control to show the default values selected? Any ideas will be appreciated.

I am trying to convert vb 6.0 app to ASP.NET app.

Thanks
mamun
0
Comment
Question by:mamunahmed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 22849414
Are you able to obtain the user info already? If the question is just how to select the value then there are a couple of ways.

cbxUsers.SelectedIndex = cbxUsers.FindString("User Name")

cbxUsers.SelectedValue = "Value"

etc
0
 

Author Closing Comment

by:mamunahmed
ID: 31511900
I had to change the GetUserInfo function and added another parameter as argument. Such as:
Public Function GetUserInfo(ByVal username As String, ByVal str As String) As String

Then used the suggested solution.

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question