0

I am pretty new to VB and I am not sure why this is not working, basically I am trying to run a stored procedure from my web code. The sp runs just fine in SQL, I've tried it several times so I am sure that is not the problem. I don't want to return any results, I just want to see an "ok" statement if it runs and an error message if it doesn't. The code I am using for the lables (warnings and confirmation) is reused from earlier on the same page, the same goes for the validations (valUpload). I am sure the solution is simple...

Protected Sub RunValidation_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RunValidation.Click
    Try
        Dim bl As New BL.ORG
        Dim db As Database = DatabaseFactory.CreateDatabase("MyConnection")
        Dim dbCommand As DbCommand

        db = DatabaseFactory.CreateDatabase("MyConnection")
        dbCommand = db.GetStoredProcCommand("Company.dbo.uspMyStoredProcedure")
        dbCommand.CommandTimeout = 300
        db.AddInParameter(dbCommand, "ClientID", DbType.String, ddlCompany.SelectedValue)
        db.AddInParameter(dbCommand, "startPeriod", DbType.String, ddlStartPeriod.SelectedValue)
        db.ExecuteDataSet(dbCommand)

        lblWarning.Text = "Please confirm that the <strong>ClientID and startPeriod </strong> are populated in the dropdown list."
        lblWarning.Visible = True
        lblConfirmation.Visible = False

    Catch ex As Exception
        valUpload.ErrorMessage = "There has been an unexpected error generating the page<br>(" + Err.Description + ")"
        valUpload.IsValid = False
    End Try
End Sub
4
  • 2
    The solution is even simpler if you tell us in what sense it doesn't work. Do you get error messages? Which line number? What does the error say? What is the stack trace? Is your connection string correct?
    – mellamokb
    May 6, 2011 at 18:59
  • Shouldn't you be using SqlCommand instead of DbCommand? May 6, 2011 at 19:07
  • Hopefully this will answer some questions: 1, It doesn't run the stored procedure as far as I can see. No results gets into my tables even when there should (when I later run the sp in SQL it populates fine) 2, No error messages at all, it appears to run just fine and that is why it is so frustrating for me, I really don't knwo where to begin when I don't even get an error message. I can upload the pages and it all runs fin, or at least appears to run just fine. Any other info that I have left out?
    – Marie J
    May 6, 2011 at 19:49
  • Have you checked if the parameters are all correctly set? Does your procedure rely on them to do what it has to?
    – Smur
    May 6, 2011 at 20:25

2 Answers 2

0

I think the issue here is the line

db.ExecuteDataSet(dbCommand)

From what I can see, the command you want to run is

db.ExecuteNonQuery(dbCommand)

Here is an example site. The code is in C# but I think you can get the basic understanding of it. You could also use a translator on it if you really needed to.

http://msdn.microsoft.com/en-us/magazine/cc188702.aspx#S2

0

I would rewrite it to something similar to this:

Protected Sub RunValidation_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RunValidation.Click
  Using cnn As New SqlClient.SqlConnection("MyConnection")
    cnn.Open()

    Using cmd As New SqlClient.SqlCommand("Company.dbo.uspMyStoredProcedure", cnn)
      cmd.CommandTimeout = 30
      cmd.Parameters.Add(New SqlClient.SqlParameter("ClientID", SqlDbType.NVarChar, 50) With {.Value = ddlCompany.SelectedValue})
      cmd.Parameters.Add(New SqlClient.SqlParameter("startPeriod", SqlDbType.NVarChar, 50) With {.Value = ddlStartPeriod.SelectedValue})

      Try
        cmd.ExecuteNonQuery()
      Catch ex As Exception
        valUpload.ErrorMessage = "There has been an unexpected error generating the page<br>(" + Err.Description + ")"
        valUpload.IsValid = False
      End Try

      lblWarning.Text = "Please confirm that the <strong>ClientID and startPeriod </strong> are populated in the dropdown list."
      lblWarning.Visible = True
      lblConfirmation.Visible = False

    End Using
  End Using
End Sub

A few notes:

  • Wrap as little code as possible in a try-catch. In this case, only the database should be a cause of concern (granted you validated the inputs).
  • The using statement very neatly disposes your connection and command objects in case of problems.
  • You probably want to refactor the code even futher, keeping the database-calling section in a separate function/sub, and setting labels and UI messages somewhere else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.