?
Solved

ASP.net Populate Dropdown List with session variable

Posted on 2007-11-13
6
Medium Priority
?
743 Views
Last Modified: 2013-11-26
I currently have a dropdown list (txtStatusNew.text ) that holds all of the Status Types of our projects. How do I show the Current Status of the Project in the Drop down list as well as all of the other choices available.

The Datasource for the dropdown list is this:
        SelectCommand="SELECT [StatusID], [StatusType] FROM [tblStatus]"></asp:SqlDataSource>


The Project Record data is being pulled into the page like this:

         Dim SelectCommand As String
                SelectCommand = String.Format("SELECT tblProjects.ProjectID, tblProjects.PlanStartDate, tblProjects.PlanDueDate, tblProjects.Notes, tblProjects.ProjectTitle, tblDepartments.DepartmentName,tblReasonCodes.Reason, tblRequestor.FirstName + ' ' + tblRequestor.LastName AS Requestor,tblWorkers.WorkerFirstName + ' ' + tblWorkers.WorkerLastName AS Resource, tblStatus.StatusType, tblProjects.RequestedCompletionDate,tblProjects.FileLocation, tblProjects.EstimatedHours, tblProjects.ProjectActualHours FROM tblProjects INNER JOIN tblStatus ON tblProjects.StatusID = tblStatus.StatusID LEFT OUTER JOIN tblRequestor ON tblProjects.RequestorID = tblRequestor.RequestorID LEFT OUTER JOIN tblWorkers ON tblProjects.WorkerID = tblWorkers.WorkerID LEFT OUTER JOIN tblReasonCodes ON tblProjects.ReasonCode = tblReasonCodes.ReasonCode LEFT OUTER JOIN tblDepartments ON tblProjects.DepartmentID = tblDepartments.DepartmentID WHERE tblProjects.ProjectID = {0}", Request.QueryString("ProjectID"))
                Using comm As New SqlCommand(SelectCommand, conn)
                    Dim rs As SqlDataReader = comm.ExecuteReader()
                    If rs.Read() Then
                        txtProjectID.Text = rs("ProjectID").ToString()
                        txtProjectTitle.Text = rs("ProjectTitle").ToString()
                        txtResource.Text = rs("Resource").ToString()
                        txtStartDate.Text = rs("PlanStartDate").ToString()
                        txtDueDate.Text = rs("PlanDueDate").ToString()
                        txtDept.Text = rs("DepartmentName").ToString()
                        txtRequestor.Text = rs("Requestor").ToString()
                        lblOldStatus.Text = rs("StatusType").ToString()
                        lblOldReason.Text = rs("Reason").ToString()
                        txtEstHours.Text = rs("EstimatedHours").ToString()
                        txtActHours.Text = rs("ProjectActualHours").ToString()
                        txtNotes.Text = rs("Notes").ToString()
                       
                       
                        conn.Close()
                    End If
                End Using
            End Using
0
Comment
Question by:Albee_J
[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
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
anoyes earned 2000 total points
ID: 20276770
Give this a shot:

txtStatusNew.SelectedValue=rs("StatusID").ToString

where StatusID is the name of the field in the tblProjects table that stores the current status.
0
 

Author Comment

by:Albee_J
ID: 20276856
I will give this a shot tomorrow morning at work.  Won't that code only show the current status without the rest of the status types in the drop down list?
0
 
LVL 16

Expert Comment

by:anoyes
ID: 20276877
As long as you bind the drop down list to your SELECT [StatusID], [StatusType] FROM [tblStatus] first, it should work fine.  Bind first, then the .SelectedValue just specifies which one of those values should be selected when the page first loads.  Let me know if you need further clarification.

-Adam
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:Albee_J
ID: 20280242
I am getting this error

 StatusID
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: StatusID

Source Error:

Line 32:                         txtActHours.Text = rs("ProjectActualHours").ToString()
Line 33:                         txtNotes.Text = rs("Notes").ToString()
Line 34:                         txtStatusNew.SelectedValue = rs("StatusID").ToString
Line 35:                      
Line 36:                        
0
 
LVL 16

Expert Comment

by:anoyes
ID: 20281320
Sorry, I didn't look close enough at your SQL.  You should also select tblProjects.StatusID.  This will give you the StatusID that is assigned to the project so you can use it to select an option in your drop down list. So SQL should be...

SelectCommand = String.Format("SELECT tblProjects.ProjectID, tblProjects.PlanStartDate, tblProjects.PlanDueDate, tblProjects.Notes, tblProjects.ProjectTitle, tblProjects.StatusID, tblDepartments.DepartmentName,tblReasonCodes.Reason, tblRequestor.FirstName + ' ' + tblRequestor.LastName AS Requestor,tblWorkers.WorkerFirstName + ' ' + tblWorkers.WorkerLastName AS Resource, tblStatus.StatusType, tblProjects.RequestedCompletionDate,tblProjects.FileLocation, tblProjects.EstimatedHours, tblProjects.ProjectActualHours FROM tblProjects INNER JOIN tblStatus ON tblProjects.StatusID = tblStatus.StatusID LEFT OUTER JOIN tblRequestor ON tblProjects.RequestorID = tblRequestor.RequestorID LEFT OUTER JOIN tblWorkers ON tblProjects.WorkerID = tblWorkers.WorkerID LEFT OUTER JOIN tblReasonCodes ON tblProjects.ReasonCode = tblReasonCodes.ReasonCode LEFT OUTER JOIN tblDepartments ON tblProjects.DepartmentID = tblDepartments.DepartmentID WHERE tblProjects.ProjectID = {0}", Request.QueryString("ProjectID"))
0
 

Author Comment

by:Albee_J
ID: 20281634
Great! That worked! :)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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