Solved

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

Posted on 2008-10-30
2
577 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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