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
632 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 500 total points
ID: 35209844
Let's concentrate first on the SQL data access part. Please refer to
this thread
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 47
ASP.net 2015 Syncing Azure web app projects on two machines 2 21
Write to a printer using vb.net 9 30
No Data for DropDown List 2 26
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

822 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