Problem dropdownlist posting back gives gives data of its own choice :-)

vickeyurs

Member
Joined
Jun 16, 2003
Messages
8
Location
Dubai
HI!!!

I have this dropdownlist which on postback gives someother data not selected from the list.

here is the Code for aspx followed by Code behind and attach is the zip file with 3 files , tipsmastera.Mdb, dropdownlist.vb and db_pulldown.aspx

Thanks in advance my friend
Code:
<html>
<head>
    <title>ASP.NET Database Dropdown List Sample</title>
</head>
<body>
    <form runat="server">
        <asp:DropDownList id="ddlMtg" runat="server"></asp:DropDownList>
        <asp:Button id="btnSubmit" onclick="SubmitBtn_Click" runat="server" text="Submit"></asp:Button>
    </form>
    <p>
        <asp:Label id="lblSelection" runat="server"></asp:Label>
    </p>
</body>
</html>

_________________Code Behind
Code:
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
                 CONNECTDB()

                 CONNECT_MTG()


             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

    strSQLQuery = "SELECT * FROM RACE_MTG ORDER BY MTG;"

    objCmd = New OleDbCommand(strSQLQuery, myConnection)

    Try
    myConnection.Open()
    dataReader = objCmd.ExecuteReader()
             With ddlmtg
                .DataSource = dataReader
                .DataTextField = "MTG"
                .DataValueField = "V"
                .DataBind()
             End With
    dataReader.NextResult()

    Catch exc As Exception
              Response.Write(exc)

    Finally
              If Not dataReader Is Nothing Then
                dataReader.Close()
              End If
              objCmd = Nothing
              If myConnection.State = ConnectionState.Open Then
                myConnection.Close()
              End If
              myConnection.Dispose()

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


    END SUB

End Class
 

Attachments

Last edited by a moderator:
Back
Top