HI!!!
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
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
VB:
--------------------------------------------------------------------------------
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