Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 31st, 2008, 03:37 PM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Default Opening a New Window

HI,
I need to inform users that they can't delete a record using a New Window or Message Box; which isn't working very well for me, yet.
I have tried two processes, a) redirect to the same page which stops the processing-works wells; but the user does not see the error message. Then, I have option b) which use of the message box; which continues the processing and deletes the record-no good.
 Goal: Provide user with an error message and stop the processing, remain on the same page.

Me snippet code :

If recordbedeleted = False Then
'Option B:
                        Dim strMessage As String
                        strMessage = "The Division ID number you are attempting to delete has employee records associated to it."
                        strMessage += " Please try again."
                        'finishes server processing, returns to client.
                        Dim strScript As String = "<script language=JavaScript>"
                        strScript += "alert(""" & strMessage & """);"
                        strScript += "</script>"

                        If (Not ClientScript.IsStartupScriptRegistered("clientScri pt")) Then
                            ClientScript.RegisterClientScriptBlock(Me.GetType( ), "clientScript", strScript)
                        End If
'Option A
                        'Response.Redirect("http://localhost:2082/Inform/samepage.aspx")
                        'lblErrorMessage.Text = " The division you are attempting to delete is in use."

                    End If

Reply With Quote
  #2 (permalink)  
Old April 1st, 2008, 07:18 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You need a Return statement in there somewhere to stop execution of your code.

If (Not ClientScript.IsStartupScriptRegistered("clientScri pt")) Then
ClientScript.RegisterClientScriptBlock(Me.GetType( ), "clientScript", strScript)
Return 'Execution for this method stops here.
End If

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #3 (permalink)  
Old April 1st, 2008, 07:30 AM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Can you give me an example of the correct Return statement to use?

Thank you.



Reply With Quote
  #4 (permalink)  
Old April 1st, 2008, 07:47 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

...It is in my code example above.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #5 (permalink)  
Old April 1st, 2008, 07:58 AM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I used "Return" as you have it in your example, but it did not stop the execution of the code.

Reply With Quote
  #6 (permalink)  
Old April 1st, 2008, 08:12 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Then there is more to the situation then you are showing. For example

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim i As Integer = 0
        If i = 0 Then
            Response.Write(i)
            Return
        End If
        i = 5
        Response.Write(i)
    End Sub

The value Zero is wrote the browser and the final two statements are never hit.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #7 (permalink)  
Old April 1st, 2008, 08:15 AM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Default

This is my code in its entirety:
 If e.CommandName = "Delete" Then
            Dim DivisionCode2 As Label = CType(dvNewDivision.FindControl("DivisionCode"), Label)
            Dim Description2 As Label = CType(dvNewDivision.FindControl("Description"), Label)


            'Establish connection to the database connection
            Dim sqlcon As New SqlClient.SqlConnection(ConfigurationManager.Conne ctionStrings("AttendanceConnectionString").ToStrin g)

            sqlcon.Open()

            Using cmd As New System.Data.SqlClient.SqlCommand("SELECT DISTINCT ATTDivision.DivisionCode, ATTDivision.DivisionDescription, ATTEmployee.DivisionCode " & _
                          "FROM ATTDivision INNER JOIN " & _
                           "ATTEmployee ON ATTDivision.DivisionCode = ATTEmployee.DivisionCode " & _
                          "WHERE (ATTDivision.DivisionCode = '" & DivisionCode2.Text & " ') " & _
                          "ORDER BY ATTDivision.DivisionDescription")

                cmd.Connection = sqlcon

                Dim recordbedeleted As Boolean = cmd.ExecuteScalar Is Nothing

                If recordbedeleted = True Then
                    Using cmd2 As New System.Data.SqlClient.SqlCommand("DELETE FROM ATTDivision WHERE DivisionCode = '" & DivisionCode2.Text & " ' ")
                        cmd2.Connection = sqlcon
                        cmd2.ExecuteNonQuery()
                    End Using
                Else

                    If recordbedeleted = False Then
                        Dim strMessage As String
                        strMessage = "The Division ID number you are attempting to delete has employee records associated to it."
                        strMessage += " Please try again."
                        'finishes server processing, returns to client.
                        Dim strScript As String = "<script language=JavaScript>"
                        strScript += "alert(""" & strMessage & """);"
                        strScript += "</script>"

                        If (Not ClientScript.IsStartupScriptRegistered("clientScri pt")) Then
                            ClientScript.RegisterClientScriptBlock(Me.GetType( ), "clientScript", strScript)
                            Return
                        End If

                    End If
                    sqlcon.Close()
                End If


            End Using
            sqlcon.Close()
        End If

Reply With Quote
  #8 (permalink)  
Old April 1st, 2008, 09:08 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Your logic seems to be a bit off. Based on the code you have here I assume that execution never gets into your else statement. Also, this statement is redundant:

If recordbedeleted = False Then

It stands to reason that unless this boolean value is true you do not want to execute the delete statement. I would write that statement something like:
                Dim recordbedeleted As Boolean = cmd.ExecuteScalar Is Nothing

                If recordbedeleted = True Then
                    Using cmd2 As New System.Data.SqlClient.SqlCommand("DELETE FROM ATTDivision WHERE DivisionCode = '" & DivisionCode2.Text & " ' ")
                        cmd2.Connection = sqlcon
                        cmd2.ExecuteNonQuery()
                    End Using
                Else

                        Dim strMessage As String
                        strMessage = "The Division ID number you are attempting to delete has employee records associated to it."
                        strMessage += " Please try again."
                        'finishes server processing, returns to client.
                        Dim strScript As String = "<script language=JavaScript>"
                        strScript += "alert(""" & strMessage & """);"
                        strScript += "</script>"

                        If (Not ClientScript.IsStartupScriptRegistered("clientScri pt")) Then
                            ClientScript.RegisterClientScriptBlock(Me.GetType( ), "clientScript", strScript)
                            Return
                        End If
                End If
sqlcon.Close()

Also, I would change your first SQL Statement to something like:
"SELECT DISTINCT Count(ATTDivision.DivisionCode) " & _
                          "FROM ATTDivision INNER JOIN " & _
                           "ATTEmployee ON ATTDivision.DivisionCode = ATTEmployee.DivisionCode " & _
                          "WHERE (ATTDivision.DivisionCode = '" & DivisionCode2.Text & " ') " & _
                          "ORDER BY ATTDivision.DivisionDescription")

And then change your boolean equation to something like
Dim recordbedeleted As Boolean = (Convert.ToInt32(cmd.ExecuteScalar) = 0)

hth

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #9 (permalink)  
Old April 1st, 2008, 09:42 AM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I copied the code, ran the page and received the following error;
*ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

This is the code that is being used:
If e.CommandName = "Delete" Then
            Dim DivisionCode2 As Label = CType(dvNewDivision.FindControl("DivisionCode"), Label)
            Dim Description2 As Label = CType(dvNewDivision.FindControl("Description"), Label)


            'Establish connection to the database connection
            Dim sqlcon As New SqlClient.SqlConnection(ConfigurationManager.Conne ctionStrings("AttendanceConnectionString").ToStrin g)

            sqlcon.Open()

            Using cmd As New System.Data.SqlClient.SqlCommand("SELECT DISTINCT Count(ATTDivision.DivisionCode) " & _
                          "FROM ATTDivision INNER JOIN " & _
                           "ATTEmployee ON ATTDivision.DivisionCode = ATTEmployee.DivisionCode " & _
                          "WHERE (ATTDivision.DivisionCode = '" & DivisionCode2.Text & " ') " & _
                          "ORDER BY ATTDivision.DivisionDescription")

                cmd.Connection = sqlcon

                Dim recordbedeleted As Boolean = (Convert.ToInt32(cmd.ExecuteScalar) = 0)<<==*It blows up on this line

                If recordbedeleted = True Then
                    Using cmd2 As New System.Data.SqlClient.SqlCommand("DELETE FROM ATTDivision WHERE DivisionCode = '" & DivisionCode2.Text & " ' ")
                        cmd2.Connection = sqlcon
                        cmd2.ExecuteNonQuery()
                    End Using
                Else

                    Dim strMessage As String
                    strMessage = "The Division ID number you are attempting to delete has employee records associated to it."
                    strMessage += " Please try again."
                    'finishes server processing, returns to client.
                    Dim strScript As String = "<script language=JavaScript>"
                    strScript += "alert(""" & strMessage & """);"
                    strScript += "</script>"

                    If (Not ClientScript.IsStartupScriptRegistered("clientScri pt")) Then
                        ClientScript.RegisterClientScriptBlock(Me.GetType( ), "clientScript", strScript)
                        Return
                    End If
                End If
            End Using
            sqlcon.Close()
        End If

**It blows up when the record needs to be deleted and when it does not.

Reply With Quote
  #10 (permalink)  
Old April 1st, 2008, 09:49 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Remove the OrderBy statement, you don't need it. Count() is an aggregate and will only return 1 record, an integer value. Also, "It blows up on this line" doesn't tell me anything.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
opening a webpage in a new window riyadsmail ASP.NET 1.0 and 1.1 Basics 3 August 31st, 2007 06:56 AM
opening a new window rgpassey Javascript How-To 2 September 14th, 2005 11:38 AM
Opening New Window p_nut33 Classic ASP Basics 1 August 6th, 2004 08:11 PM
Close Parent window on opening child window pkdev Javascript How-To 8 April 11th, 2004 12:06 PM
Opening New Window james_sellwood ASP.NET 1.0 and 1.1 Basics 5 June 12th, 2003 11:43 AM



All times are GMT -4. The time now is 01:10 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.