Link to home
Start Free TrialLog in
Avatar of GlobaLevel
GlobaLevelFlag for United States of America

asked on

asp.net - pull rows from ms sql svr 05 DB...insert into ...with a button for the user to remove...

I need to pulls rows off db..and insert into a gridview...or label controls..doesnt matter....but on each row..I need to have a button ..so if they want to delete that row..they can press it...and only that row will be affected...I provided code..below..but its got a long way to go...
public sub page_load () handles me.load

  	 
Try
            Dim MyDropDown As DropDownList
           
            Dim myArray As New ArrayList()
            Dim dataReader As SqlDataReader
            Dim x As Integer = 0
            Dim sqlConnection As String
            
            TC_ID = Session("SessionTC_ID")
            MyDropDown = DropDownList1
            
               
            Using sqlConn As New SqlConnection
                sqlConnection = "Data Source=xx.xx.xx.xx;Initial Catalog=Terrrnert;Trusted_connection=true;"
                sqlConn.ConnectionString = sqlConnection
                Dim SQLstring As String = "SELECT distinct(campaign_name) FROM campaign WHERE TC_ID = '" & TC_ID & "'"
          
                sqlConn.Open()
                Using command As New SqlCommand(SQLstring, sqlConn)
                    dataReader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
                    If dataReader.HasRows Then
                        Do While dataReader.Read()
                            Session("sessionTO_NUMBER") = dataReader("TO_NUMBER").ToString()
                        Loop
                        
                    End If
                    dataReader.Close()
                    sqlConn.Close()
                End Using
            End Using
            
            myArray.Sort()

            MyDropDown.DataSource = myArray
            MyDropDown.DataBind()
        Catch ex As Exception
           
        End Try




        '--------------------------------

        ' POPULATE THE GRIDVIEW CONTROL..ON SELECTION FROM DROPDOWNLIST

        '--------------------------------

        Try

            Dim Go_Message As TextBox

            Dim sqlConnection As String

            Go_Message = TextBox1

            

            Using sqlConn As New SqlConnection

                sqlConnection = "Data Source=xx.xx.xx.xx;Initial Catalog=Mycomp;Trusted_connection=true;"

                sqlConn.ConnectionString = sqlConnection

                Dim SQLstring As String = "SELECT * FROM  camp WHERE TC_ID = '" & tc_id

                Dim TblAdaptar As New SqlDataAdapter(SQLstring, sqlConn)

                Dim Tbl As New DataSet

                Tbl.Clear()

                Try

                    TblAdaptar.Fill(Tbl)

                    Me.GridView1.DataSource = Nothing

                    Me.GridView1.DataSource = Tbl

                    Me.GridView1.DataBind()

                Catch ex As Exception

                End Try

            End Using

        Catch ex As Exception

        End Try

End sub

Open in new window

Avatar of gamarrojgq
gamarrojgq

Hi,

You have to add a DELETE command column to your grid, that will show a delete button to each row of your grid, after that you add your delete statment in the RowDeleting event of your grid, it will looks like this


Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

        'Get the selected row
        Dim row As GridViewRow
        row = GridView1.Rows(e.RowIndex)

        'Obtain the ID of the record to delete just that one
        'Assuming yout Record ID is in the first column
        Dim strID As String
        strID = row.Cells(0).Text

        Using sqlConn As New SqlConnection

            sqlConn.ConnectionString = "Data Source=xx.xx.xx.xx;Initial Catalog=Terrrnert;Trusted_connection=true;"

            'HERE YOU PUT THE REAL DELETE STATEMENT
            Dim SQLstring As String = "DELETE campaign WHERE TC_ID = '" & strID & "'"

            sqlConn.Open()
            Using command As New SqlCommand(SQLstring, sqlConn)
                Try
                    command.ExecuteNonQuery()
                Catch ex As Exception
                    'Some code handling the error
                End Try
                sqlConn.Close()
            End Using
        End Using

    End Sub

Open in new window

Avatar of GlobaLevel

ASKER

..looks good...fyi...I am trying to do this all manually...trying to stay away from the wizards...if it makes a difference...
Yes, do it manually will give you full control over the process, and in many cases gives you better performance since you do not use nothing you do not need.

The example I send you works manually since you are using an Event handled by you.

Adding the Delete Command Column to your grid also can be done manually, adding this line to your gridview markup

<asp:CommandField ShowDeleteButton="True" />
GridView1_RowDeleting

...just so I understand you correctly...in order to call the event...

<asp:CommandField ShowDeleteButton="True" onClick="GridView1_RowDeleting" />

...correct..?
Nope, usually you do not need to asign the event handler to the column, but if the button does not call the event, you have to add it, but, to the gridview markup

<asp:GridView ID="GridView1" runat="server"
        ...
        OnRowDeleting="GridView1_RowDeleting"
        ...

got an error, hen I tried to attach the delete...see below..
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <RowStyle BackColor="#EFF3FB" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
            <asp:CommandField ShowDeleteButton="True" />

        </asp:GridView>

Open in new window

Ok, the error is because the commandField has to be inside the <Columns> Tag, but based in your gridview markup, you  are not declaring the columns manually, and you have the AutoGenerateCoumns Property of the gridview ON.

So, if this is true, remove the  <asp:CommandField ShowDeleteButton="True" />, and set the property AUTOGENERATEDELETEBUTTON  On, that will show you the Delete Column, and the event handler for RowDeleting will work with no problem
okay...a little confused...is this what you mean...

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="None">
            <RowStyle BackColor="#EFF3FB" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
            <AUTOGENERATEDELETEBUTTON="On" />

        </asp:GridView>
ASKER CERTIFIED SOLUTION
Avatar of gamarrojgq
gamarrojgq

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I apologize, I will finish up the weekend...