1 Replies - 5565 Views - Last Post: 03 May 2008 - 11:43 PM Rate Topic: -----

#1 ryan john majarais  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 03-April 08

Use Dropdownlist display picture in imagebutton

Post icon  Posted 03 May 2008 - 10:38 AM

^^ my website is voting system i have a dropdownlist inside the dropdownlist is a data and a list of surname if i select one his/her picture will display in the imagebutton. my code wont work. what is wrong??
Imports System.Data
Imports System.Data.Sql

Partial Class VotingSystem_Candidates
	Inherits System.Web.UI.Page

	Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
		Dim connString As String = _
		ConfigurationManager.ConnectionStrings("connstr").ConnectionString

		Using cnn As New SqlClient.SqlConnection(connString)
			Const sql As String = "select forSurname from VotingCandidateRegistration"


			Dim myCommand As New SqlClient.SqlCommand(sql, cnn)
			Dim myDataset As New DataSet

			Try
				cnn.Open()

			Catch ex As Exception
				MsgBox(ex.ToString)

			End Try

			Dim myAdapter As New SqlClient.SqlDataAdapter(myCommand)
			myAdapter.Fill(myDataset)

			DrpPresident.DataSource = myDataset
			DrpPresident.DataBind()

			Dim i As Integer

			For i = 0 To DrpPresident.Items.Count - 1

				DrpPresident.Items.Add(myDataset.Tables(0).Rows(0).Item("forsurname").ToString)


 Next

		End Using

 Protected Sub DrpPresident_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DrpPresident.SelectedIndexChanged
		Dim connString As String = _
						ConfigurationManager.ConnectionStrings("connstr").ConnectionString
		Dim cnn As New SqlClient.SqlConnection(connString)

		
		Try
			cnn.Open()
		Catch ex As Exception
			MsgBox(ex.ToString)
		End Try
		Dim sql As String



		sql = "SELECT forSurname from votingCandidateRegistration where  forStdntNo like  '" & DrpPresident.SelectedValue & "'"
		'Dim sql As String = "INSERT INTO StdntLogin (StdntUsername,StdntPassword,StdntFname,StdntLname,StdntEmailadd,StdntSquestion,StdntSanswer,StdntNumber,StdntCourse,StdntGender,Category) VALUES ('" & txtuser.Text & "', '" & txtpassword.Text & "','" & txtfirstname.Text & "', '" & txtlastname.Text & "','','','','" & txtstudentno.Text & "','','', 'admin') "


 ImgPresident.ImageUrl = "~/Pictures/ulosoto.JPG"


		Dim cmd As New SqlClient.SqlCommand(sql, cnn)
 Dim myDataSet As New DataSet

		Dim myAdapter As New SqlClient.SqlDataAdapter(cmd)
		myAdapter.Fill(myDataSet)

	End Sub

This post has been edited by jayman9: 03 May 2008 - 10:51 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Use Dropdownlist display picture in imagebutton

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Use Dropdownlist display picture in imagebutton

Posted 03 May 2008 - 11:43 PM

Well I see you're binding your DropDownList to your DataSet, then you're trying to add items to it from the DataSet (the one it's already bound to). You either want to bind or add like you're doing in your For loop, not both. Also, all your work is being done outside your Try...Catch block which doesnt make any sense. The Try...Catch block is for trapping any exceptions that may occur.



Imports System.Data
Imports System.Data.Sql

Partial Class VotingSystem_Candidates
    Inherits System.Web.UI.Page

    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
            Try
                   Dim connString As String = _
                   ConfigurationManager.ConnectionStrings("connstr").ConnectionString

                    Using cnn As New SqlClient.SqlConnection(connString)
                            Const sql As String = "select forSurname from VotingCandidateRegistration"

                             Dim myCommand As New SqlClient.SqlCommand(sql, cnn)
                             Dim myDataset As New DataSet
                             cnn.Open()
                             Dim myAdapter As New SqlClient.SqlDataAdapter(myCommand)
                             myAdapter.Fill(myDataset)

                              For i As Integer = 0 To myDataset.Tables(0).Rows.Count - 1
                                      DrpPresident.Items.Add(myDataset.Tables(0).Rows(0).Item("forsurname").ToString)
                              Next 
                   End Using
            Catch ex As Exception
                MsgBox(ex.ToString)

            End Try            
End Sub


Protected Sub DrpPresident_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DrpPresident.SelectedIndexChanged
        Try
                 Dim connString As String = _
                 ConfigurationManager.ConnectionStrings("connstr").ConnectionString
                 Dim cnn As New SqlClient.SqlConnection(connString)
                 cnn.Open()
                 Dim sql As String
                 sql = "SELECT forSurname from votingCandidateRegistration where  forStdntNo like  '" & DrpPresident.SelectedValue & "'"
                 ImgPresident.ImageUrl = "~/Pictures/ulosoto.JPG"
                 Dim cmd As New SqlClient.SqlCommand(sql, cnn)
                 Dim myDataSet As New DataSet
                 Dim myAdapter As New SqlClient.SqlDataAdapter(cmd)
                 myAdapter.Fill(myDataSet)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
End Sub




Doing it this way will at least let you know if you have error's that are occurring.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1