I am trying to run SQL SERVER 2008 stored procedure using VB.NET. I am able to connect fine but I do not want to write the code over again for every stored procedure. How can I have that code in form of a function or sub so that i can reuse the code by simply passing the stored procedure name to the code. Below is sample of what I am using.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.txtProcessDate.Text = "" Then
MsgBox("Please Enter The processing Date Before You Continue ", vbOKOnly, "SMIS")
Me.txtProcessDate.Focus()
Else
Try
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "computationsdelete"
cmd.CommandTimeout = 6000
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "deductionssmoothing"
cmd.CommandTimeout = 6000
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
MsgBox("RECORDS POSTED SUCCESSFULLY. YOU CAN NOW PRINT PAYSLIPS")
conn.Open()
sqlcmd.Parameters.AddWithValue("@d", processdate)
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "insertpaymentsanddeductions"
sqlcmd.CommandTimeout = 6000
sqlcmd.ExecuteNonQuery()
sqlcmd.Dispose()
conn.Close()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.txtProcessDate.Text = "" Then
MsgBox("Please Enter The processing Date Before You Continue ", vbOKOnly, "SMIS")
Me.txtProcessDate.Focus()
Else
Try
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "computationsdelete"
cmd.CommandTimeout = 6000
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "deductionssmoothing"
cmd.CommandTimeout = 6000
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
MsgBox("RECORDS POSTED SUCCESSFULLY. YOU CAN NOW PRINT PAYSLIPS")
conn.Open()
sqlcmd.Parameters.AddWithValue("@d", processdate)
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "insertpaymentsanddeductions"
sqlcmd.CommandTimeout = 6000
sqlcmd.ExecuteNonQuery()
sqlcmd.Dispose()
conn.Close()