Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now