Working with Stored Procedures using ASP.NET 2.0 with Microsoft Data Access Application Block - Executing a stored procedure with parameters from ASP.NET 2.0 using the Data Access Application Block: the recommended approach
(Page 5 of 6 )
In this section, we shall modify the code given in the previous section to make it more readable and efficient. In this part, we will be dealing with the Data Access Application Block to add parameters (input parameters).
I shall use the same stored procedure given in the previous section. The following is the complete modified code to execute the same stored procedure:
ImportsSystem.Data
ImportsMicrosoft.Practices.EnterpriseLibrary.Data.Sql
ImportsMicrosoft.Practices.EnterpriseLibrary.Data
PartialClass Sample2
Inherits System.Web.UI.Page
Protected Sub btnConnect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConnect.Click
Try
Me.lblMsg.Text = SPExecute("p_IncEmployeeSal", 1001) & " rows affected"
Catch ex As Exception
Me.lblMsg.Text = ex.Message
End Try
End Sub
Private Function SPExecute(ByVal Name As String, ByVal ParamValue As Integer) As Integer
Try
Dim db As SqlDatabase = DirectCast(DatabaseFactory.CreateDatabase("AdventureWorks"), SqlDatabase)
Dim cmd As Common.DbCommand = db.GetStoredProcCommand(Name)
db.AddInParameter(cmd, "Empno", DbType.Int32, ParamValue)
Dim RowsAffected As Integer = db.ExecuteNonQuery(cmd)
Catch ex As Exception
Throw New Exception("Unable to execute:" & ex.Message)
End Try
End Function
EndClass
Next: Executing a stored procedure which returns a set of rows using ASP.NET 2.0 and the Data Access Application Block >>
More ASP.NET Articles
More By Jagadish Chaterjee