Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2005, 06:10 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RPG SEARCH
Default Error Deleting

Hey there,

I am having trouble deleting from a datagrid. it throwing a datatypr mismatch error if SelectedItemID is in '', and no value passed if its not... I cant see any difference from code i used awhile back...

Code:
 Sub dgNewsDelete_Click(ByVal Sender As Object, ByVal e As DataGridCommandEventArgs)
        Dim SelectedNewsID As String = (e.Item.ItemIndex)

        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Inetpub\wwwroot\MYWEBSITE\davesdatabase.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "DELETE FROM [tbl_News] WHERE ([tbl_News].[NewsID] = 'SelectedNewsID')"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        dbConnection.Open()
        dbCommand.ExecuteNonQuery()
        dbConnection.Close()

        dgNews.DataSource() = getNews()
        dgNews.DataBind()

    End Sub
thanks for the assistance.

David Jenkins
-------------------------------------------------------------
Do you want to make extra money around your commitments?
Credit cards, bills, loans and a mortgage - all getting you down?
Is your pension going to be enough when you retire?
There is a solution visit http://www.1stmillion.co.uk
or call 01772 489521
__________________
David Jenkins
 
Old June 5th, 2005, 03:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi David,

What type is NewsID in the database? Sounds like a number....

To pass a number, you need something like this:

WHERE ([tbl_News].[NewsID] = " & SelectedNewsID & ")"

However, there are some other problems in your code. Take a look at this:

Dim SelectedNewsID As String = (e.Item.ItemIndex)

First of all, ItemIndex is a number so you should either declare SelectedNewsID as an Integer, or convert ItemIndex to a String.

This line of code returns the index of the item you're about to delete. So let's say you clicked on the second item which has an index of 1. Are you then going to delete the item with an Id of 1 in the database? Don't think so; you need to get the associated DataKey for the item you're deleting.

Did you set up DataKeys for the DataGrid?

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 5th, 2005, 06:30 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RPG SEARCH
Default

Hi Imar,

Yeah its the AutoNumber in the database and the Primary Key.

I changed over the code to an integer and altered the delete string.... and set DataKeysField to NewsID. How can i tell it to delete equal to the datakey?

Thanks.

David Jenkins
-------------------------------------------------------------
Do you want to make extra money around your commitments?
Credit cards, bills, loans and a mortgage - all getting you down?
Is your pension going to be enough when you retire?
There is a solution visit http://www.1stmillion.co.uk
or call 01772 489521
 
Old June 5th, 2005, 06:41 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi David,

You can get the data key from the DataKeys collection of the DataGrid using the ItemIndex like this:

newsId = Convert.ToInt32(myDataGrid.DataKeys[e.Item.ItemIndex])

HtH,

Imar

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 5th, 2005, 08:02 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RPG SEARCH
Default

I incorporated that, possibly not in the right way way though.

Code:
Sub dgNewsDelete_Click(ByVal Sender As Object, ByVal e As DataGridCommandEventArgs)

        Dim NewsId as Integer = Convert.ToInt32(myDataGrid.DataKeys[e.Item.ItemIndex])


        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Inetpub\wwwroot\MYWEBSITE\davesdatabase.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "DELETE FROM [tbl_News] WHERE ([tbl_News].[NewsID] = " & NewsId & ")"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        dbConnection.Open()
        dbCommand.ExecuteNonQuery()
        dbConnection.Close()

        dgNews.DataSource() = getNews()
        dgNews.DataBind()

    End Sub
visual studio says that identifier expect in errors for e.Item.ItemIndex - ran it anyway and it just postback but nothing happens...

have i put it into the code wrong?

David Jenkins
-------------------------------------------------------------
Do you want to make extra money around your commitments?
Credit cards, bills, loans and a mortgage - all getting you down?
Is your pension going to be enough when you retire?
There is a solution visit http://www.1stmillion.co.uk
or call 01772 489521
 
Old June 5th, 2005, 08:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You should use *your* DataGrid, not mine ;)

Dim NewsId as Integer = Convert.ToInt32(dgNewsDelete.DataKeys(e.Item.ItemIndex))

Also, I used C# array indexing instead of VB.NET. Sorry about that. Replace [] with ()

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 5th, 2005, 10:23 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RPG SEARCH
Default

*Blush* oh yeah!

Thanks that all works now - i can freely delete till my heart is content!

David Jenkins
-------------------------------------------------------------
Do you want to make extra money around your commitments?
Credit cards, bills, loans and a mortgage - all getting you down?
Is your pension going to be enough when you retire?
There is a solution visit http://www.1stmillion.co.uk
or call 01772 489521
 
Old June 6th, 2005, 01:06 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome. Glad it's working.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Time Is Running Out by Muse (Track 3 from the album: Absolution) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Error After Deleting Polls CrassMaestro BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 February 7th, 2007 12:18 AM
Error When Deleting Recordset arimakidd Classic ASP Databases 3 May 22nd, 2006 10:59 AM
error in deleting,updating and records in asp method Classic ASP Databases 1 May 6th, 2005 10:35 AM
Deleting duplicates prabodh_mishra SQL Server 2000 3 September 23rd, 2003 09:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.