Solved

ASP.net Populate Dropdown List with session variable

Posted on 2007-11-13
6
737 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
Technology Partners: 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!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

685 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