Solved

ASP.net Populate Dropdown List with session variable

Posted on 2007-11-13
6
732 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

20 Experts available now in Live!

Get 1:1 Help Now