Link to home
Start Free TrialLog in
Avatar of EYoung
EYoungFlag for United States of America

asked on

How delete a record using ASP 4.0 then refresh GridView

I am writing an asp 4.0 web site that will allow the users to add, change, and delete records from certain SQL  tables.  On one of the pages there is a GridView control with a "Select" link control on each row.  When the user clicks the Select control, that row's cells values are filled into some text boxes below the gridview and the user can then add, change, or delete the record.

I have tried using the Add, Change, and Delete features of the gridview control, but do not find them easy for the users.  So I am filling in these text boxes below the gridview and allowing the users to make their changes.

With that said, how do I delete a record in a SQL table when the user clicks the Delete button?  Once the user clicks the Delete button, the record should be removed from the SQL table and the gridview refreshed.

I am using the VB Code Behind method.   Here is the event that is triggered when the user clicks the Delete button:

    Protected Sub btnDelete_Click(sender As Object, e As System.EventArgs) Handles btnDelete.Click
         (What goes here?)
    End Sub


Thank you
Avatar of gamarrojgq
gamarrojgq

Hi,

based on you comments, you should have to run a Delete Statement against your DB and delete the Row, and after that Rebind your datagrid.

if will be something like this

 
Dim conn As New SqlClient.SqlConnection
        Dim comm As New SqlClient.SqlCommand

        conn.ConnectionString = "YOUR CONNECTION STRING HERE"
        Try
            conn.Open()
            comm.CommandText = "DELETE YourTableName WHERE YourPrimaryKeyField = " & "THE PRIMARY KEY EDITED"
            comm.Connection = conn
            comm.ExecuteNonQuery()
            comm.Dispose()
        Catch ex As Exception
            'Do something about the error 
        Finally
            conn.Close()
        End Try

        conn.Dispose()

Open in new window


And after that you rebind your Gridview like this (assuming that you gridview names is Gridview1, and that you are setting the data source manually)

Gridview1.DataSource = YOURDATASOURCE
Gridview1.DataBind()

Hope that helps
Avatar of EYoung

ASKER

Is the code you provided able to run in the code behind file in ASP?  It looks more like straight vb.net code?
are you using any built-in DataSource? How are you databinding your GV?
Yes, you can run the code in codebehind or an aspx file, as long that your aspx file use vb.net if you want I can send you the code in C#.net

Perhaps you are using a SqlDataSource to bind your GridView and that is why you see the code odd, so what are you using for your GridView? a dataset? a datatable? as SqlDataSource?
Avatar of EYoung

ASKER

The gridview has bound data using SqlDataSource.

Here is the reference to the DataSource in the .aspx code:

                             <asp:GridView ID="gridStoresRetailSummary" runat="server" BackColor="LightGoldenrodYellow"
                                ForeColor="Black" AllowPaging="True"
                                style="position:absolute; top:40px; left:30px; width: 834px;"
                                BorderColor="Maroon" BorderWidth="1px" AutoGenerateColumns="False"
                                CellPadding="2" DataKeyNames="mKey" DataSourceID="DataSource_CWDW_Retail"
                                ShowHeaderWhenEmpty="True" AllowSorting="True" PageSize="9">

Here is the SqlDataSource code in the same file:

                            <asp:SqlDataSource ID="DataSource_CWDW_Retail" runat="server"
                                ConnectionString="<%$ ConnectionStrings:ConnectionString_CWDW_Retail %>"
                                ProviderName="<%$ ConnectionStrings:ConnectionString_CWDW_Retail.ProviderName %>"                                
                                SelectCommand="SELECT * FROM [ReportRecipients] ORDER BY [RecipientEmail]">
                            </asp:SqlDataSource>


So do I just put the above suggested code from gamarrojgq into the aspx.vb file?

Thanks for the help.
ok, if you are using a SqlDataSource you can do this (just change YourPrimaraKeyField  for the real primary key field name

'Set the Delete Statement of your SqlDataSource
DataSource_CWDW_Retail.DeleteCommand = "DELETE FROM ReportRecipients WHERE YourPrimaryKeyField = @YourPrimaryKeyField;"

'Call the Delete Method on your SqlDatasource, this method will use the Delete method set before
DataSource_CWDW_Retail.Delete()
DataSource_CWDW_Retail.DataBind()
Avatar of EYoung

ASKER

This is the code I just tried and it works:

    Protected Sub btnDelete_Click(sender As Object, e As System.EventArgs) Handles btnDelete.Click
        Dim connSQL As New SqlClient.SqlConnection()
        Dim cmdSQL As New SqlClient.SqlCommand
        Dim mKey As String

        mKey = Trim(txtEmailAddress.Text)
        connSQL.ConnectionString = "Data Source=TTA-CWDW;Integrated Security=SSPI;Initial Catalog=Retail"

        Try
            connSQL.Open()
            cmdSQL.CommandText = "DELETE ReportRecipients WHERE RecipientEmail = '" & mKey & "'"
            cmdSQL.Connection = connSQL
            cmdSQL.ExecuteNonQuery()
            cmdSQL.Dispose()
        Catch ex As Exception
            'Do something about the error
        Finally
            connSQL.Close()
        End Try

        connSQL.Dispose()
    End Sub


The above is called when the "Delete" button is clicked on the ASP form.  Since I am new to ASP coding, should I change the above to the way you suggested or does it matter?
Avatar of EYoung

ASKER

OK, I see now.  Using the method you suggested uses far fewer lines of code and looks more efficient.  Here is what I have now:

    Protected Sub btnDelete_Click(sender As Object, e As System.EventArgs) Handles btnDelete.Click
        DataSource_CWDW_Retail.DeleteCommand = "DELETE ReportRecipients WHERE RecipientEmail = '" & Trim(txtEmailAddress.Text) & "'"
        DataSource_CWDW_Retail.Delete()
        DataSource_CWDW_Retail.DataBind()
    End Sub


After the above code is executed, how do I get the gridview to refresh on the screen?  It does not look like it is reloading the records or refreshing?

Thanks
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
Avatar of EYoung

ASKER

That works.  Thank you for all the help.