Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
638 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 2000 total points
ID: 35209844
Let's concentrate first on the SQL data access part. Please refer to
this thread
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

671 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