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
 
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

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 )
      myConnection.Open()
      category.DataSource = myCommand.ExecuteReader()
      category.DataTextField = "CategoryName"
      category.DataBind()
      myConnection.Close()
    End If
  End Sub

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


<html>
<head><title>Categories</title></head>
<body>

<form Runat="Server">

Please select a category:
<br>
<asp:DropDownList
  id="category"
  Runat="Server" />

<asp:button
  Text="Select!"
  onClick="pickCat"
  Runat="Server" />

<p>

Current Category:
<asp:Label
  id="currentCat"
  Runat="Server" />

</form>


</body>
</html>


Always:),
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

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.

Bye


melvik July 21st, 2003 05:30 AM

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



Always:),
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)
            myConnection.Open()
            DropDownList1.DataSource = myCommand.ExecuteReader()
            DropDownList1.DataTextField = "form_id"
            DropDownList1.DataBind()
            myConnection.Close()
        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




wei

melvik September 3rd, 2005 09:28 AM

have u used
Code:

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

DropDownList1.DataTextField = "form_id"
use for that!
Code:

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

Always:),
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.