Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

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

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
mamunahmed
Asked:
mamunahmed
1 Solution
 
CodeCruiserCommented:
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
 
mamunahmedAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now