issue with delete operation on Dropdownlist selected item

Discussion in 'ASP .Net Web Controls' started by Dhananjay, Jan 14, 2008.

  1. Dhananjay

    Dhananjay Guest

    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
    Dhananjay, Jan 14, 2008
    #1
    1. Advertising

  2. Dhananjay

    Phil H Guest

    On 14 Jan, 11:33, Dhananjay <> wrote:
    > 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
    Phil H, Jan 16, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iain
    Replies:
    3
    Views:
    911
  2. Ratko
    Replies:
    7
    Views:
    269
    Reedick, Andrew
    Jul 17, 2008
  3. Nathan Sokalski
    Replies:
    0
    Views:
    592
    Nathan Sokalski
    Oct 5, 2008
  4. mldardy
    Replies:
    0
    Views:
    948
    mldardy
    Sep 28, 2010
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    276
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page