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
630 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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
Let's concentrate first on the SQL data access part. Please refer to
this thread
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now