Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 July 19th, 2003, 02:05 PM
Registered User
 
Join Date: Jul 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old July 19th, 2003, 11:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

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.
 
Old July 20th, 2003, 02:49 PM
Registered User
 
Join Date: Jul 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old July 21st, 2003, 05:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

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



Always:),
Hovik Melkomian.
 
Old September 1st, 2005, 11:14 PM
vvv vvv is offline
Registered User
 
Join Date: Sep 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to vvv Send a message via MSN to vvv Send a message via Yahoo to vvv
Default

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
 
Old September 3rd, 2005, 09:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP. Button Control not posting back innochig ASP.NET 1.0 and 1.1 Professional 16 January 15th, 2007 05:15 PM
Drop down list text value not posting back with co binici ASP.NET 2.0 Basics 0 December 12th, 2006 08:38 PM
onChange posting back to same page? busher Classic ASP Basics 1 March 1st, 2005 06:20 PM
posting back javascript data badgolfer ASP.NET 1.0 and 1.1 Basics 2 September 26th, 2004 09:05 PM





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