bretbel
asked on
Execute SQL stored procedure from Button Click Event
1) Please I need to execute a SQL stored procedure using the "Button_Click" event in asp vb code page. The stored procedure has no parameters, it just updates SQL tables. The connection string is already contained in the web.config file.
2) I also want to display in text box that process was completed successfully. ie: "Completed"
2) I also want to display in text box that process was completed successfully. ie: "Completed"
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the procedure has to either return a recordset, or provide a output parameter so you can get the value.
in the first case, use oCmd.ExecuteReader() with then returns the SqlReader object, in the second case, you have to set up a SqlParameter object (before calling ExecuteNonQuery()), and collect the value after the Execute.
in the first case, use oCmd.ExecuteReader() with then returns the SqlReader object, in the second case, you have to set up a SqlParameter object (before calling ExecuteNonQuery()), and collect the value after the Execute.
If you change the stored procedure to select just one value you can do this easily.
For example, if we have this proc:
CREATE PROCEDURE [dbo].[testproc]
AS
SELECT MAX(PRICE) FROM items
You can then just do this in .net:
Try
Dim oConn As New SqlClient.SqlConnection
Dim oCmd As New SqlClient.SqlCommand
oConn.ConnectionString = ConfigurationManager.Conne ctionStrin gs("myconn string").C onnectionS tring
oConn.Open()
oCmd.Connection = oConn
oCmd.CommandText = "mystoredproc"
oCmd.CommandType = CommandType.StoredProcedur e
textbox1.text = "Success: " + oCmd.ExecuteScalar
oConn.Close()
Catch ex As Exception
textbox1.text = "Failure: " + ex.Message
End Try
For example, if we have this proc:
CREATE PROCEDURE [dbo].[testproc]
AS
SELECT MAX(PRICE) FROM items
You can then just do this in .net:
Try
Dim oConn As New SqlClient.SqlConnection
Dim oCmd As New SqlClient.SqlCommand
oConn.ConnectionString = ConfigurationManager.Conne
oConn.Open()
oCmd.Connection = oConn
oCmd.CommandText = "mystoredproc"
oCmd.CommandType = CommandType.StoredProcedur
textbox1.text = "Success: " + oCmd.ExecuteScalar
oConn.Close()
Catch ex As Exception
textbox1.text = "Failure: " + ex.Message
End Try
ASKER
thanks again.