p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ADO.NET (http://p2p.wrox.com/forumdisplay.php?f=109)
-   -   Help: dropdownlist posting back gives random data (http://p2p.wrox.com/showthread.php?t=1850)

vickeyurs July 19th, 2003 02:05 PM

Help: dropdownlist posting back gives random data

I have this dropdownlist which on postback gives random data from the list not the one we select :-) . i tried all the magic but still it wont work? please help if you could.

here is the Code for aspx followed by Code behind

Thanks in advance my friend

   <title>ASP.NET Database Dropdown List Sample</title>
   <form runat="server">
       <asp:DropDownList id="ddlMtg" runat="server"></asp:DropDownList>
       <asp:Button id="btnSubmit" onclick="SubmitBtn_Click" runat="server" text="Submit"></asp:Button>
       <asp:Label id="lblSelection" runat="server"></asp:Label>


_________________Code Behind

Imports System
Imports System.Web.HttpCookie
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.text.regularexpressions
Imports System.DateTime
Imports Microsoft.VisualBasic

Public Class dropdownlist : Inherits system.Web.UI.Page

   Dim myConnection As OleDbConnection
        Dim objCmd As OleDbCommand
        Dim DataReader As OleDbDataReader
        Dim strSQLQuery As String

   Protected WithEvents ddlmtg As System.Web.UI.WebControls.dropDownList

   Protected WithEvents lblSelection As System.Web.UI.WebControls.label

   Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button

        Sub Page_Load(sender As Object, e As EventArgs)
            ' Only pull data from db on first page call.
            If Not Page.IsPostBack Then

                ' Create connection and set connection string


            End If
        End Sub

        Sub SubmitBtn_Click(sender As Object, e As EventArgs)
            lblSelection.Text = "You selected the item in position " _
                & ddlmtg.SelectedItem.Value _
                & " which corresponds to the name " _
                & ddlmtg.SelectedItem.Text _
                & "."
        End Sub

       Public Sub CONNECTDB()

   myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db/tipsmastera.mdb") & ";")

   End Sub

   Public Sub CONNECT_MTG()

   Dim strSql As String


   objCmd = New OleDbCommand(strSQLQuery, myConnection)

   dataReader = objCmd.ExecuteReader()
            With ddlmtg
               .DataSource = dataReader
               .DataTextField = "MTG"
               .DataValueField = "V"
            End With

   Catch exc As Exception

             If Not dataReader Is Nothing Then
             End If
             objCmd = Nothing
             If myConnection.State = ConnectionState.Open Then
             End If

             Trace.warn ("Mtg Text", ddlmtg.selecteditem.text)
   End Try

   End Sub

End Class

melvik July 19th, 2003 11:24 PM

I hope this sample will help u!
plz set the connection string for ur own Server!

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">
  Sub Page_Load( s As Object, e As EventArgs )
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    If Not isPostBack Then
      ' Get List of Categories From Database
      myConnection = New SQLConnection( "Server=Localhost;uid=sa;pwd=secret;Database=North wind" )
      myCommand = New SqlCommand( "Select CategoryName From Categories", myConnection )
      category.DataSource = myCommand.ExecuteReader()
      category.DataTextField = "CategoryName"
    End If
  End Sub

  Sub pickCat( s As Object, e As EventArgs )
    currentCat.Text = category.SelectedItem.Text
  End Sub


<form Runat="Server">

Please select a category:
  Runat="Server" />

  Runat="Server" />


Current Category:
  Runat="Server" />



Hovik Melkomian.

vickeyurs July 20th, 2003 02:49 PM

Thanks for that hovic,

IT works well for Text but did to try to get value also try to get value

for example make database with the following field MTG, V, ID

A | 9 | 1
B | 7 | 2
C | 9 | 3
D | 7 | 4

then try to do postback and try to get the value as well text on click, u will find something new for sure.


melvik July 21st, 2003 05:30 AM

simple use category.SelectedItem.Value insted of category.SelectedItem.Text

Hovik Melkomian.

vvv September 1st, 2005 11:14 PM

hello melvik,

i create a dropdownlist in my webform and my method is same as yours but when i try to select the value in the dropdownlist the dropdownlist will give me error "Object reference not set to an instance of an object." i have do a testing with ur code, the same error occur.
below is my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myConnection As SqlConnection
        Dim myCommand As SqlCommand
        If Not IsPostBack Then

            myConnection = New SqlConnection("Server=p866;uid=sa;pwd=;Database=su rvey")
            myCommand = New SqlCommand("Select form_id From form", myConnection)
            DropDownList1.DataSource = myCommand.ExecuteReader()
            DropDownList1.DataTextField = "form_id"
        End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = DropDownList1.SelectedItem.Value
End Sub
Please help if u could. thanks


melvik September 3rd, 2005 09:28 AM

have u used

TextBox1.Text = DropDownList1.Text
I guess it wont give error!
Bc u just set the text & not Value

DropDownList1.DataTextField = "form_id"
use for that!

DropDownList1.DataValueField = "form_id"
Call me for any problem, but please write individually;) (not reply)

Hovik Melkomian.

All times are GMT -4. The time now is 10:59 PM.

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