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
634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 total points
ID: 35209844
Let's concentrate first on the SQL data access part. Please refer to
this thread
0
Industry Leaders: 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

730 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