Solved

Delete records in repeater

Posted on 2004-10-18
11
264 Views
Last Modified: 2011-09-20
Hello,
I have a data repeater that displays database records in a table.  I want to add a "delete" button in the repeater so that a user can press the button to delete the corresponding record but I do not know how to pass that value back to the SQL "DELETE" statement.  Can someone help?

Thanks.
0
Comment
Question by:liquid22
  • 6
  • 5
11 Comments
 
LVL 20

Expert Comment

by:jitganguly
ID: 12340345
onclick of Delete call a procedure and isse Delete statement on that

Post code
0
 
LVL 1

Author Comment

by:liquid22
ID: 12340601
Yeah that's what I tried to do but didn't know how to pass the record ID value into the SQL statement.

Here is the code that builds the repeater table:
Sub Page_Load(Source As Object, E As EventArgs)
Dim objConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings.Get("ConnectionString"))
Dim sSQL As String = "SELECT FILE_ID, FILE_NAME FROM tblMgmtReview ORDER BY FILE_NAME"
Dim objCmd As New SqlDataAdapter(sSQL,objConn)
Dim objDS As New DataSet()
objCmd.Fill(objDS, "tblMgmtReview")
MgmtOutput.DataSource = objDS.Tables("tblMgmtReview").DefaultView
MgmtOutput.DataBind()
objConn.Close
End Sub
------DELETE CODE HERE----------
Sub cmdDelete_OnClick(Source As Object, E As EventArgs)
Dim objConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings.Get("ConnectionString"))
Dim sSQL As String = "DELETE FROM tblMgmtReview WHERE FILE_ID = @FileID"      
Dim objCmd As SqlCommand = New SqlCommand(sSQL,objConn)
objCmd.Parameters.Add(New SqlParameter("@FileID", <not sure what to put here>))
Try
objConn.Open()
objCmd.ExecuteNonQuery
lblMessage.Text = "Record successfully deleted.<br />"
Catch SqlEx As exception
lblMessage.Text = "There was an error:&nbsp;" & SqlEx.ToString()
Finally
objConn.Close()
End Try
End Sub
------------------ REPEATER TABLE -------------------
<form runat="server">
<asp:Repeater ID="MgmtOutput" OnItemCommand="onClick_Delete" runat="server">
<headertemplate>
<table width="100%" style="border:1px solid #000000;" cellpadding="0" cellspacing="0">
<tr>
<td style="font-weight:bold;background-color:#666666;color:#FFFFFF;">ID:</td>
<td style="font-weight:bold;background-color:#666666;color:#FFFFFF;">File Name:</td>
<td style="font-weight:bold;background-color:#666666;color:#FFFFFF;">&nbsp;</td>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td style="border:1px solid #000000;"><%#DataBinder.Eval(Container.DataItem,"FILE_ID")%></td>
<td style="border:1px solid #000000;"><%#DataBinder.Eval(Container.DataItem,"FILE_NAME")%></td>
<td style="border:1px solid #000000;"><asp:Button ID="btnDelete" OnClick="cmdDelete_OnClick" Text="Delete" runat="server" /></td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:Repeater>
</form>

I'm new to ASP.NET so please excuse my code if it's "messy".
0
 
LVL 20

Expert Comment

by:jitganguly
ID: 12340860
<td style="border:1px solid #000000;"><asp:Button ID="btnDelete" OnClick="cmdDelete"  CommandArgument='<%#Container.DataItem(FILE_ID")%>' Text="Delete" runat="server" /></td>


Sub cmdDelete(Source As Object, E As EventArgs)
Dim objConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings.Get("ConnectionString"))
Dim sSQL As String = "DELETE FROM tblMgmtReview WHERE FILE_ID = "  & FILE_ID
Dim objCmd As SqlCommand = New SqlCommand(sSQL,objConn)
objCmd.Parameters.Add(New SqlParameter("@FileID", <not sure what to put here>))
Try
objConn.Open()
objCmd.ExecuteNonQuery
lblMessage.Text = "Record successfully deleted.<br />"
Catch SqlEx As exception
lblMessage.Text = "There was an error:&nbsp;" & SqlEx.ToString()
Finally
objConn.Close()
End Try
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:liquid22
ID: 12341080
I get this error now:
Method 'Public Sub onClick_Delete(Source As Object, e As System.EventArgs)' does not have the same signature as delegate 'Delegate Sub RepeaterCommandEventHandler(source As Object, e As System.Web.UI.WebControls.RepeaterCommandEventArgs)'.
0
 
LVL 1

Author Comment

by:liquid22
ID: 12341122
Actually disregard that last error.  Here is the error I get now:

There was an error: System.Data.SqlClient.SqlException: Syntax error converting the nvarchar value 'FILE_ID' to a column of data type int. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
0
 
LVL 20

Expert Comment

by:jitganguly
ID: 12341140
Oh put it within quotes

Dim sSQL As String = "DELETE FROM tblMgmtReview WHERE FILE_ID = '"  & FILE_ID & "'"

This has
1 single quote after =
then a double quote
Then & File_ID & and then a double quoute
then a single quote
then a double quote

0
 
LVL 1

Author Comment

by:liquid22
ID: 12341189
Hi again,
I had some syntax wrong but when I re-tried the code it delete everything in the entire table, not just the record I wanted to delete.  Not sure what happened there but here was the delete code I used:

Sub onClick_Delete(Source As Object, e As EventArgs)
Dim objConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings.Get("ConnectionString"))
Dim sSQL As String = "DELETE FROM tblMgmtReview WHERE FILE_ID = " & "FILE_ID" '
Dim objCmd As SqlCommand = New SqlCommand(sSQL,objConn)
Try
objConn.Open()
objCmd.ExecuteNonQuery
lblMessage.Text = "Record successfully deleted.<br />"
Catch SqlEx As exception
lblMessage.Text = "There was an error:&nbsp;" & SqlEx.ToString()
Finally
objConn.Close()
End Try
End Sub
0
 
LVL 20

Expert Comment

by:jitganguly
ID: 12341207
Again
Dim sSQL As String = "DELETE FROM tblMgmtReview WHERE FILE_ID = " & "FILE_ID" '
is wrong
do exactly as I described above

The reason for deleteting all records is all of them got File_ID = passed id. Check that
0
 
LVL 1

Author Comment

by:liquid22
ID: 12341302
Okay I corrected the syntax but still receive this error:

There was an error: System.Data.SqlClient.SqlException: Syntax error converting the varchar value 'FILE_ID' to a column of data type int. at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

I'll be sure to increase my points if we can get this fixed.
0
 
LVL 20

Accepted Solution

by:
jitganguly earned 200 total points
ID: 12341321
Whats the data type of File_ID

If its int then use

Dim sSQL As String = "DELETE FROM tblMgmtReview WHERE FILE_ID = " & FILE_ID

if its char use
Dim sSQL As String = "DELETE FROM tblMgmtReview WHERE FILE_ID = '" & FILE_ID & "'"


0
 
LVL 1

Author Comment

by:liquid22
ID: 12341404
That did it - thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in query expression 3 54
Select record with the most recent date 14 71
Downside of adding characters set in ASP pages 6 36
Html split(text) Part2 6 20
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question