Solved

ASP.net Populate Dropdown List with session variable

Posted on 2007-11-13
6
734 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
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
anoyes earned 500 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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

13 Experts available now in Live!

Get 1:1 Help Now