Can't Figure out What's wrong with my code.

Hobbes

Active member
Joined
May 22, 2003
Messages
37
I got this tutorial from a text book.

Instead of doing it in sql server, I am tring to do it using MS access.

After changing every thing, I found that the delete record link button will not work.

There is no compiling error of any sort.

Please help!! :confused:

Thanks


****************heres the code************************
Code:
Sub MyDG_ItemCommand(ByVal Sender As Object, ByVal e As DataGridCommandEventArgs)
        If e.CommandSource.CommandName = "RemoveFromCat" Then
            Identify the Cell that Contain the CatID
            Dim CatIDCell As TableCell = e.Item.Cells(1)
            Dim CatID As String = CatIDCell.Text

            Dim CatThumbCell As TableCell = e.Item.Cells(4)
            Dim CatThumb As String = CatThumbCell.Text

            Dim CatImageCell As TableCell = e.Item.Cells(3)
            Dim CatImage As String = CatImageCell.Text

            USe SQL to remove the Cat with CatID = CatIDCell

            dim dbconn,sql, dbcomm,dbread
            dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
            & "data source=" & server.mappath("..\TaraStore.mdb"))
            dbconn.Open()
            sql="DELETE FROM Categories WHERE CategoryID =" & CatID

            dbcomm=New OleDbCommand(sql,dbconn)
            dbread=dbcomm.ExecuteNonQuery()
            MyDG.DataSource=dbread
            MyDG.DataBind()
            dbread.Close()
            dbconn.Close()
            Page.DataBind()

            Find path to image folder

            Dim strFilePath As String
            strFilePath = System.IO.Path.GetDirectoryName(Server.MapPath("DeletCat.aspx"))

            Delete Image
            System.IO.File.Delete((strFilePath & "\..\" & "images" & "\" & CatImage))
            Delete Thumbnail
            System.IO.File.Delete((strFilePath & "\..\" & "images" & "\" & CatThumb))

        Else

        End If
        Page.DataBind()
End Sub

**************Heres the html**********************
Code:
<body ms_positioning="GridLayout">
    <form id="Form1" method="post" runat="server">
        <asp:DataGrid id="MyDG" style="Z-INDEX: 101; LEFT: 17px; 
         POSITION: absolute; TOP: 80px" runat="server"          
         AutoGenerateColumns="False" DataKeyField="CategoryID" 
         Font-Names=" Verdana" HeaderStyle-     
         BackColor="#aaaadd" Font-Size="8pt" Font-Name="  
         Verdana" CellPadding="3" BorderWidth="1px" BorderColor="Black">
            <HeaderStyle backcolor="LightSteelBlue"></HeaderStyle>
            <Columns>
                <asp:TemplateColumn HeaderText="Select a Task">
                    <ItemTemplate>
                        <asp:LinkButton ID="RemoveButton" CommandName="RemoveFromCat" Text="Delete" ForeColor="Blue" Runat="server" />
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:BoundColumn DataField="CategoryID" SortExpression="CategoryID" HeaderText="Category ID"></asp:BoundColumn>
                <asp:BoundColumn DataField="CategoryName" SortExpression="CategoryName" HeaderText="Category Name"></asp:BoundColumn>
                <asp:BoundColumn DataField="CatImage" SortExpression="CatImage" HeaderText="Image"></asp:BoundColumn>
                <asp:BoundColumn DataField="Thumbnail" SortExpression="Thumbnail" HeaderText="Thumbnail"></asp:BoundColumn>
                <asp:BoundColumn DataField="Description" SortExpression="Description" HeaderText="Description"></asp:BoundColumn>
            </Columns>
        </asp:DataGrid>
[edit]Please use
Code:
 tags [/vb ] [/edit]
 
Last edited by a moderator:
What data type is CategoryID in the table? If its string (text) then try this...
Code:
sql="DELETE FROM Categories WHERE CategoryID = " & CatID & ""
If its numeric (integer) then..
Code:
sql="DELETE FROM Categories WHERE CategoryID = " & convert.toint32(CatID)

ALSO:
You NEED to place the following at the top of each code page...
Option Explicit On
Option Strict On

AND:
Declare your variables as types...
these are not: "Dim dbconn,sql, dbcomm,dbread"
it should be something like this...
Code:
Dim dbconn as OledbConnection
Dim sql as string
Dim dbcomm as oledbCommand
You should go back to the sample and see how they declared their variables, if the type is SqlConnection then simply replace the Sql with OleDb to get OleDbConnection
 
Thanks for the reply. :)

I have made the changes. However, the problem still exist.

Everything runs fine, except for the delete link button that is not responding. Its seems to be missing some on_click event that triggers the "MyDG_ItemCommand" sub.


I tried to add onClick event into the linkbutton tag but it gave an error that says:

Compiler Error Message: BC30408: Method Public Sub MyDG_ItemCommand(Sender As Object, e As System.Web.UI.WebControls.DataGridCommandEventArgs) does not have the same signature as delegate Delegate Sub EventHandler(sender As Object, e As System.EventArgs).

Code:
<asp:LinkButton ID="RemoveButton" CommandName="RemoveFromCat" Text="Delete" 
ForeColor="Blue" onClick= "MyDG_ItemCommand" 
Runat="server" />

Any Ideas?? Thanks!
 
I noticed that

Sub MyDG_ItemCommand(ByVal Sender As Object, ByVal e As DataGridCommandEventArgs)

is not associated w/ any event. Try this:

Sub MyDG_ItemCommand(ByVal Sender As Object, ByVal e As DataGridCommandEventArgs) Handles MyDG.ItemCommand
 
Thanks for the reply.

I tried it and got the following error.

Compiler Error Message: BC30506: Handles clause requires a WithEvents variable.


By the way, I developed it using visual studio using sqlConnection. Now I am using webmatrix to do it in MS access

Any ideas??
 
> Compiler Error Message: BC30506: Handles clause requires a WithEvents variable.

Search for the declaration of MyDG, probably something like:

Protected MyDG As DataGrid

Try changing it to:

Protected WithEvents MyDG As DataGrid
 
Back
Top