First of all, do you notice any repetition in your code?
Generally if you have to code anything that is similar more than once, then you need to write a function/sub or use some loops to handle it all, instead of re-writing it 15 times and just changing the numbers each time.
Here's a simple suggestion. Put each type of control in a List. Then, you can just write for loops to cycle through each of them. Here's a simple example. It's a form with 9 CheckBoxes and 9 TextBoxes on it.
Public Class Form1
Private checkBoxes As List(Of CheckBox)
Private textBoxes As List(Of TextBox)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
checkBoxes = New List(Of CheckBox)
textBoxes = New List(Of TextBox)
For i = 1 To 9
checkBoxes.Add(Me.Controls.Find("CheckBox" & i, False)(0))
Next
For i = 1 To 9
textBoxes.Add(Me.Controls.Find("TextBox" & i, False)(0))
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i = 0 To checkBoxes.Count - 1
If checkBoxes(i).Checked Then
MessageBox.Show(textBoxes(i).Text)
End If
Next
End Sub
End Class
As far as your question goes, here would be one option. You could have the function return an Exception. Then, you just have it look like:
Public Function rtrnQry(ByVal usrNm As String, ByVal psWd As String, ByVal fNm As String, _
ByVal lNm As String, ByVal empId As Integer, ByVal proNm As String, _
ByVal aDm As String, ByVal rowno As Integer) As Exception
Using cn As New SqlClient.SqlConnection(sqlConnStr)
Try
cn.Open()
Catch ex As Exception
MessageBox.Show("Error Opening Connection")
Return ex
End Try
Using cmd As New SqlClient.SqlCommand("InsertLogin", cn)
With cmd
.CommandType = CommandType.StoredProcedure
.Parameters.AddWithValue("@empid", empId)
.Parameters.AddWithValue("@username", usrNm)
.Parameters.AddWithValue("@password", psWd)
.Parameters.AddWithValue("@firstname", fNm)
.Parameters.AddWithValue("@lastname", lNm)
.Parameters.AddWithValue("@adminpri", aDm)
.Parameters.AddWithValue("@proName", proNm)
Try
.ExecuteNonQuery()
Catch ex As SqlClient.SqlException
Return ex
End Try
End With
End Using
End Using
Return Nothing
End Function
Then, when you call it, change it to:
Dim returnEx As Exception
returnEx = rtrnQry(txtUsrNm_1.Text, txtPswd_1.Text, txtFName_1.Text, txtLName_1.Text, _
txtEmpID_1.Text, cmbProNm1.SelectedItem, cmbAdmin1.SelectedItem, _
1)
If returnEx IsNot Nothing Then
If TypeOf returnEx Is SqlClient.SqlException Then
If CType(returnEx, SqlClient.SqlException).Number = 2627 Then
ElseIf CType(returnEx, SqlClient.SqlException).Number = 208 Then
End If
End If
End If
As a final thought, you are misusing the Try/Catch block. If you want the old VB6
On Error GoTo
, you can still use it...they've carried it over to .Net. Try/Catch is meant to catch a specific statement. Using it the way you've done won't always tell you the information you need if you had an error. It takes a little bit more effort, but it's the better way to go.