Solved

Stored Procedure when called from VB Subroutine is not working

Posted on 2012-04-06
8
342 Views
Last Modified: 2012-04-06
I have a stored procedure that deleted records from a table based on billdate field.  When I execute the stored procedure, it works fine.  However if I call it from a VB Subroutine, no rows are deleted.  I can't see anything wrong.  Can someone help?  (See attached files)

ALTER-PROCEDURE-dbo.doc

Sub-DelMonthlyHospPricingArchive.doc
0
Comment
Question by:kshumway
  • 4
  • 3
8 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 37817248
Try it as:
Sub DelMonthlyHospPricingArchive(ByVal dBillDate As Date)
        Dim cmdHP As New SqlCommand
        Try
            conn.Open()
            cmdHP.Connection = conn
            cmdHP.CommandType = CommandType.StoredProcedure
            cmdHP.Parameters.AddWithValue("@BillDate", dBilldate.ToString("yyyy-MM-dd"))
            cmdHP.CommandText = "DeleteHospPricingSQLArchive"
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
        End Try
    End Sub

Open in new window


MSDN: http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
0
 
LVL 18

Accepted Solution

by:
lludden earned 500 total points
ID: 37817293
You never call an execute method.

Sub DelMonthlyHospPricingArchive(ByVal dBillDate As Date)
        Dim cmdHP As New SqlCommand
        Try
            conn.Open()
            cmdHP.Connection = conn
            cmdHP.CommandType = CommandType.StoredProcedure
            cmdHP.Parameters.AddWithValue("@BillDate", dBilldate.ToString("yyyy-MM-dd"))
            cmdHP.CommandText = "DeleteHospPricingSQLArchive"
            cmdHP.ExecuteNonQuery
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            conn.Close()
        End Try
    End Sub
0
 

Author Comment

by:kshumway
ID: 37817314
I execute the stored procedure in SQL Server.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 37817326
Good catch, @lludden! You have to execute the SqlCommand at some point, so if your code truly has that missing then try adding that first. If it doesn't work from there, my thought was that the date conversion was causing an issue (i.e., converting incorrectly and so not finding any records to delete). If that is the case, then the fix is to use ToString() to format the date as 'yyyy-MM-dd' or '2012-04-06', which SQL Server should understand as a date properly.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:kshumway
ID: 37817333
OK - I just tried it and it still did not delete the records.  There are 4300 records - would that make a difference?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 37817347
How are you calling the sub within VB?
0
 

Author Comment

by:kshumway
ID: 37817374
Oh - now I know what you mean - I knew something was missing but just couldn't see it:

 cmdHP.ExecuteNonQuery

Yes that was it.  However I could not do the date conversion without an error.  It worked the way I originally had it - with the exeute line, of course.

THANK YOU SO MUCH!  I have been looking at this forever and could not see what was wrong
0
 

Author Closing Comment

by:kshumway
ID: 37817377
Thank you again...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

706 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

23 Experts available now in Live!

Get 1:1 Help Now