EYoung
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
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
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?
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?
ASKER
The gridview has bound data using SqlDataSource.
Here is the reference to the DataSource in the .aspx code:
<asp:GridView ID="gridStoresRetailSummar y" runat="server" BackColor="LightGoldenrodY ellow"
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_C WDW_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:Connecti onString_C WDW_Retail %>"
ProviderName="<%$ ConnectionStrings:Connecti onString_C WDW_Retail .ProviderN ame %>"
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.
Here is the reference to the DataSource in the .aspx code:
<asp:GridView ID="gridStoresRetailSummar
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_C
ShowHeaderWhenEmpty="True"
Here is the SqlDataSource code in the same file:
<asp:SqlDataSource ID="DataSource_CWDW_Retail
ConnectionString="<%$ ConnectionStrings:Connecti
ProviderName="<%$ ConnectionStrings:Connecti
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.Del eteCommand = "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.Del ete()
DataSource_CWDW_Retail.Dat aBind()
'Set the Delete Statement of your SqlDataSource
DataSource_CWDW_Retail.Del
'Call the Delete Method on your SqlDatasource, this method will use the Delete method set before
DataSource_CWDW_Retail.Del
DataSource_CWDW_Retail.Dat
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?
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
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?
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.Del eteCommand = "DELETE ReportRecipients WHERE RecipientEmail = '" & Trim(txtEmailAddress.Text) & "'"
DataSource_CWDW_Retail.Del ete()
DataSource_CWDW_Retail.Dat aBind()
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
Protected Sub btnDelete_Click(sender As Object, e As System.EventArgs) Handles btnDelete.Click
DataSource_CWDW_Retail.Del
DataSource_CWDW_Retail.Del
DataSource_CWDW_Retail.Dat
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That works. Thank you for all the help.
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
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