issue with delete operation on Dropdownlist selected item

D

Dhananjay

Hi all,

I have problem with delete functionality in a web page.First i am
populating data in Dropdownlist, and on the same page i want to remove
the data from same Dropdownlist with buttonDelete_Click event.
First what i wanted to populate data in Dropdownlist ---> it's
working fine.
next, based on the selected item from Dropdown list i wanted to remove
the data from Dropdownlist based on the button click event --- not
working .

Can any please help me to solve this issue. I am providing code also
so that you can easily troubleshoot the problem.

my code --->

Public Sub page_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Load
PanelForEdit.Visible = False

If Not Page.IsPostBack = True Then
Dim dbcon As SqlConnection
dbcon = New SqlConnection("Data Source=.
\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HRISDB.mdf;Integrated
Security=True;User Instance=True")
Dim dbcom As New SqlCommand()
dbcom.Connection = dbcon
dbcom.CommandType = CommandType.Text
dbcom.CommandText = "Select TalentTeamID,TalentTeamName
from TalentTeam "
dbcon.Open()
Dim dr As SqlDataReader
dr = dbcom.ExecuteReader()
'While dr.Read()
ddlSelectForEdit.DataSource = dr
ddlSelectForEdit.DataBind()
'End While
dbcon.Close()
ddlSelectForEdit.SelectedIndex = 0
ddlSelectForEdit.SelectedItem.Text = "Please select name"
End If

End Sub


Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles btnDelete.Click
Dim TalentTeamID As Integer = ddlSelectForEdit.SelectedValue
Dim condelete As SqlConnection
condelete = New SqlConnection("Data Source=.
\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HRISDB.mdf;Integrated
Security=True;User Instance=True")
Try
condelete.Open()
Dim cmddelete As New SqlCommand()
cmddelete.CommandType = CommandType.Text
cmddelete.CommandText = "Delete from TalentTeam where
TalentTeamID='" & TalentTeamID & "'"
cmddelete.ExecuteNonQuery()
Catch ex As Exception

Finally
condelete.Close()
End Try
End Sub


Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As
EventArgs)
Dim TalentTeamID As Integer = ddlSelectForEdit.SelectedValue
Dim conupdate As SqlConnection
conupdate = New SqlConnection("Data Source=.
\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HRISDB.mdf;Integrated
Security=True;User Instance=True")
Try
conupdate.Open()
Dim cmdupdate As New SqlCommand()
cmdupdate.Connection = conupdate
cmdupdate.CommandType = CommandType.Text
cmdupdate.CommandText = "Update TalentTeam set
TalentTeamName='" & txtForEdit.Text & "' where TalentTeamID='" &
TalentTeamID & "' "
Dim TalentTeamName As String
TalentTeamName = txtForEdit.Text.ToString()
cmdupdate.ExecuteNonQuery()
Catch ex As Exception

Finally
ddlSelectForEdit.SelectedIndex = 0
ddlSelectForEdit.SelectedItem.Text = "Please select name"
conupdate.Close()
End Try

End Sub






please provide me the solution, it's urgent.

Thanks in Advance
Dhananjay
 
P

Phil H

Hi all,

I have problem with delete functionality in a web page.First i am
populating data in Dropdownlist, and on the same page i want to remove
the data from same Dropdownlist with buttonDelete_Click event.
First what i wanted to populate data in Dropdownlist   ---> it's
working fine.
next, based on the selected item from Dropdown list i wanted to remove
the data from Dropdownlist based on the button click event   --- not
working .

Can any please help me to solve this issue. I am providing code also
so that you can easily troubleshoot the problem.

my code  --->

Public Sub page_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Load
        PanelForEdit.Visible = False

        If Not Page.IsPostBack = True Then
            Dim dbcon As SqlConnection
            dbcon = New SqlConnection("Data Source=.
\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HRISDB.mdf;Integrated
Security=True;User Instance=True")
            Dim dbcom As New SqlCommand()
            dbcom.Connection = dbcon
            dbcom.CommandType = CommandType.Text
            dbcom.CommandText = "Select TalentTeamID,TalentTeamName
from TalentTeam "
            dbcon.Open()
            Dim dr As SqlDataReader
            dr = dbcom.ExecuteReader()
            'While dr.Read()
            ddlSelectForEdit.DataSource = dr
            ddlSelectForEdit.DataBind()
            'End While
            dbcon.Close()
            ddlSelectForEdit.SelectedIndex = 0
            ddlSelectForEdit.SelectedItem.Text = "Please select name"
        End If

    End Sub

 Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles btnDelete.Click
        Dim TalentTeamID As Integer = ddlSelectForEdit.SelectedValue
        Dim condelete As SqlConnection
        condelete = New SqlConnection("Data Source=.
\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HRISDB.mdf;Integrated
Security=True;User Instance=True")
        Try
            condelete.Open()
            Dim cmddelete As New SqlCommand()
            cmddelete.CommandType = CommandType.Text
            cmddelete.CommandText = "Delete from TalentTeam where
TalentTeamID='" & TalentTeamID & "'"
            cmddelete.ExecuteNonQuery()
        Catch ex As Exception

        Finally
            condelete.Close()
        End Try
    End Sub

Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As
EventArgs)
        Dim TalentTeamID As Integer = ddlSelectForEdit.SelectedValue
        Dim conupdate As SqlConnection
        conupdate = New SqlConnection("Data Source=.
\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HRISDB.mdf;Integrated
Security=True;User Instance=True")
        Try
            conupdate.Open()
            Dim cmdupdate As New SqlCommand()
            cmdupdate.Connection = conupdate
            cmdupdate.CommandType = CommandType.Text
            cmdupdate.CommandText = "Update TalentTeam set
TalentTeamName='" & txtForEdit.Text & "' where TalentTeamID='" &
TalentTeamID & "' "
            Dim TalentTeamName As String
            TalentTeamName = txtForEdit.Text.ToString()
            cmdupdate.ExecuteNonQuery()
        Catch ex As Exception

        Finally
            ddlSelectForEdit.SelectedIndex = 0
            ddlSelectForEdit.SelectedItem.Text = "Please select name"
            conupdate.Close()
        End Try

    End Sub

please provide me the solution, it's urgent.

Thanks in Advance
Dhananjay

Dear Dhananjay

I'm puzzled by this piece of code:
'While dr.Read()
ddlSelectForEdit.DataSource = dr
ddlSelectForEdit.DataBind()
'End While

You say this works fine but I'm blowed if I can see why.

A databound DropDownList has two elements that need to be bound to
specific columns of the datasource. The DataTextField and the
DataValueField properties. Your code does not specify what goes where.
Furthermore the assignment of the DataSource property and the
execution of DataBind() method should only be done once, not repeated
for every row of the data. In any case you haven't declared a suitable
DataSource that can be used in this manner.

Since you are iterating through the table with a SqlDataReader you
might as well create and assign each item of the DropDownList
manually, e.g. like this:

'Declare some variables to hold the data
Dim TalentTeamItem as ListItem
Dim TalentTeamID as integer
Dim TalentTeamName as string

'In what follows we use the ListItem constructor thus - new
ListItem([DataText], [DataValue])
'and then use the Add(ListItem) method to build the items collection.

'Create the first (dummy) item to display the prompt
TalentTeamItem = new ListItem("Please select name", -1)
ddlSelectForEdit.items.Add(TalentTeamItem)

'Now iterate through the Data and create the items
While dr.Read()
TalentTeamID = dr.GetInt32(0)
TalentTeamName = dr.GetString(1)
TalentTeamItem = new ListItem(TalentTeamName, TalentTeamID)
ddlSelectForEdit.items.Add(TalentTeamItem)
End While

ddlSelectForEdit.SelectedIndex = 0

The result will be a properly constituted list with the names being
displayed and the numeric ID being returned by the selection, to use
as a key for data updates.

Out of time now but hope that helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top