Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check if conditions are OK for deleting records with gridview control in asp.net 3.5 web page

Posted on 2011-03-23
6
Medium Priority
?
639 Views
Last Modified: 2012-05-11
I am very new to creating asp.net websites, new to Visual Studio 2008 in general, and new to coding in VB.   I am creating a web application to replace an MS Access app.

Right now I am seeking help with what code to place in an event handler, such as a Gridview's RowDeleting event, to peek around to see if the record about to be deleted is referenced in other tables.

In Access, for example on a form where the CustContactID to be deleted had been chosen from a combo box called cmbSelCustContactID, I would have had some code run on the form's BeforeDelConfirm event, something like:
Dim x as Integer
x = Dcount("ProjectID", "ProjectContacts", "CustContactID=" & me.cmbSelCustContactID)
If x > 0 then
MsgBox "The contact is associated with Project data.  You may not delete them."
Exit Sub
End If

What can I put in the Gridview control's RowDeleting event handler to achieve the same result?   Some way to instantiate a recordset in the code, then evaluate the recordset and use the results to drive subsequent code?
0
Comment
Question by:Alastair_White
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:bitref
ID: 35206056
In the RowDeleting event handler you should connect to the required database and check whatever checks you need. If everything goes fine the delete operation will complete successfully, else you may use "e.cancel" to cancel the delete operation.
0
 

Author Comment

by:Alastair_White
ID: 35209775
Sounds good.  How do I do that?  As I say, in Access I would have used DCount or DLookup to check my various checks in the event handler.  How do I do that in the VB code-behind of the asp.net webpate, within the RowDeleting event handler of the gridview?  Presumably write some SQL, but in what way, precisely?  Do I have to set something up like a container or something? Can you give me a step-by-step example of how to connect to the required database and check stuff?

Also, once the delete request has passed all my checks and is about to execute, how do I create a pop up for users to confirm the delete?  I tried a msgbox, but because it was server-side (so I've read) it didn't work when I tried to access the app thru IIS.  I've read that I need a (javascript?) client-side confirmation box, and I'm trying to get that to work, but no luck so far.   Do you have any advice?  I know nothing about javascript.
0
 
LVL 5

Accepted Solution

by:
bitref earned 2000 total points
ID: 35209844
Let's concentrate first on the SQL data access part. Please refer to
this thread
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 83

Expert Comment

by:CodeCruiser
ID: 35217117
I would do something like below for SQL Server

Dim dbcon As New SqlConnection("Connection string")
dbcon.Open()
Dim dbcmd As New SqlCommand
dbcmd.Connection = dbcon
dbcmd.CommandText = "Select Count (*) From tablename where columnname=" & somevalue
Dim Count As Integer = dbcmd.ExecuteScalar()
dbcmd.Dispose
dbcon.Dispose

If Count > 0 Then
   'Can not delete
End If
0
 

Author Comment

by:Alastair_White
ID: 35232324
Hi - sorry, been out of town for a few days.  Thank you for your responses, I will have some time today to try implementing them, and will post back as soon as I can!
0
 

Author Comment

by:Alastair_White
ID: 35265763
Thanks very much for your help.  Both of you provided the same solution; bitref got to it a day earlier so points to bitref.

One small thing: I started trying this out in my RowDeleting event handler, but SqlConnection and SqlCommand were not coming up in IntelliSense.  Help suggested I "import  System.Data.SqlClient" , which is apparently the .NET framework provider for SQL Server.  Funny that wasn't already in there.  I imported it and the classes were recognized right away, and everything worked great.

I used e.cancel = True to abort the delete action based on the value of the variable that was populated via the execution of code provided here.

Thank you very much.  I will no doubt use this code very frequently.

Now, I'm going to post a new question about trying to get a client-side confirmation box to come up so the user can confirm to proceed when the value of my variable, as discovered above, results in allowing the execution of the delete query.  I'd also like to be able to display a client-side message explaining why not if the delete is disallowed.  Y'all are encouraged to come get more points and help me with that!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

783 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