Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 28th, 2003, 10:52 AM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dropdown values

Hi,

I am using VB.NET and codebehind to try to place a dropdownlist value in a database. Seems pretty simple and I have everything displaying OK but I can't seem to pull the value. It seems to return the name of the field in the datasource(projecttypeid) rather than the actual value (ie:14)

Here is my code:

dropdownlist creation:
        'Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("D BConnection"))
        'Global Variables (Database connection,Adapter, and SQL String)
        Dim SQLConnection AS SQLConnection
        Dim SQLDataAdapter AS SQLDataAdapter
        Dim SQLCommand AS SQLCommand

        'Dropdown List Variables (Recordset)
        Dim ProjectTypeDS AS DataSet

        ' Open your database connection (only done once)
        SQLConnection = New SqlConnection(ConfigurationSettings.AppSettings("D BConnection"))
        ' Set your query string
        SQLCommand = New SQLCommand("Select * from B_Project_Type",SQLConnection)
        'Initiate the Adapter and the Dataset
        SQLDataAdapter = New SQLDataAdapter(sqlCommand)
        ProjectTypeDS = New DataSet()
        'Populate the DataSet
        SQLDataAdapter.Fill(ProjectTypeDS,"ProjectType")
        'Apply Sort
        ProjectTypeDS.Tables(0).DefaultView.Sort = "Project Type"
        ProjectTypeID.DataSource= ProjectTypeDS.Tables("ProjectType")'.DefualtView
        ProjectTypeID.DataTextField = "Project Type"
        ProjectTypeID.DataValueField = "ProjectTypeID"
        ProjectTypeID.DataBind()
        ProjectTypeID.Items.Insert(0,New ListItem())

There is a button called new. When I click it I want the value returned. This is my onclick code....

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        ProjectID.Text = ProjectTypeID.DataValueField
    End Sub

My source code for the dropdown looks like this:

<TD style="WIDTH: 186px">Bucket</TD>
                    <TD><select name="ProjectTypeID" id="ProjectTypeID">
    <option value=""></option>
    <option value="8">Cancelled</option>
    <option value="4">Completed</option>
    <option value="5">On Hold</option>
    <option value="6">Prioritized</option>
    <option value="7">Quick &amp; Dirty</option>
    <option value="10">Scheduled</option>
    <option value="9">Time Sensitive</option>
    <option value="2">To Be Prioritized</option>
    <option value="11">To Be Scheduled</option>
    <option value="3">Upcoming</option>
    <option value="1">Work In Process</option>
</select></TD>

For some reason... if I pick Work In Process and click new. The text populates as "ProjectTypeID" rather than 1.

Any ideas?

Thanks in advance.

 
Old October 28th, 2003, 10:58 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Please don't double post. Try one forum at a time. If you can't get an answer from one, then try another (particularly if you are using two very related forums: i.e. ASPX_Begginers and ASPX_Pro).

Peter
 
Old October 28th, 2003, 11:12 AM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry about that... I will delete the other one... Can't delete this one now that someone has replied =(... Do you know of anywhere I could find a solution to this?

 
Old October 28th, 2003, 11:46 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to change this:

ProjectID.Text = ProjectTypeID.DataValueField

to this:

ProjectID.Text = ProjectTypeID.SelectedItem.Value

This should do what you want.

Peter
 
Old October 28th, 2003, 12:06 PM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried that and now it is putting a blank entry in the field =(. Is there something wrong with the way I have populated the dropdown lists? Shoudl I be using a different method?

 
Old October 28th, 2003, 12:31 PM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am extremely embarrassed but it was my mistake. I forgot the If Not Page.IsPostBack code. When the page submitted it was taking the first option which was of course blank...

Thanks for your guidance though!






Similar Threads
Thread Thread Starter Forum Replies Last Post
checkbox values to dropdown december Classic ASP Professional 1 July 3rd, 2006 08:34 PM
Get The Values In The DropDown vivek_inos Access VBA 1 November 30th, 2005 04:17 PM
Get The Values In The DropDown vivek_inos Access 2 November 30th, 2005 09:45 AM
update dropdown values mateenmohd SQL Server 2000 0 October 9th, 2003 03:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.