I have Function Called GetTransNo() but when i run the code i get this error Variable 'GetTransNo' is used before it has been assigned a value. A null reference exception could result at runtime" please any one can help me for this
Function GetTransNo() As String
Try
Dim sdate As String = Now.ToString("yyyyMMdd")
cn.Open()
cm = New SqlCommand("Select * From TblCart Where TransNo Like '" & sdate & "%' Order By Id Desc", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
GetTransNo = CLng(dr.Item("Transno").ToString) 1
Else
GetTransNo = sdate & "0001"
End If
dr.Close()
cn.Close()
Catch ex As Exception
cn.Close()
MsgBox(ex.Message, vbCritical)
End Try
Return GetTransNo
End Function
here where i call the function
Private Sub BtnNewOrder_Click(sender As Object, e As EventArgs) Handles BtnNewOrder.Click
If CheckTransaction() = True Then
LblTransNo.Text = GetTransNo()
With FrmSelectTable
.LoadTable()
.ShowDialog()
End With
loadcart()
Else
MsgBox("Transaction is already closed", vbExclamation)
End If
End Sub
CodePudding user response:
In the old days, a value was returned from a function by writing functionName = theReturnValue
, as the code in the question shows with GetTransNo = sdate & "0001"
.
The modern way of returning a value is to use the Return
statement, as shown in this code:
Function GetTransNo() As String
Dim sql = "SELECT TOP (1) TransNo FROM TblCart
WHERE TransNo LIKE @TransNo
ORDER BY Id DESC"
Dim sdate As String = Now.ToString("yyyyMMdd")
Dim transNo As String = ""
Using conn As New SqlConnection("yourConnectionStingGoesHere"),
sqlCmd As New SqlCommand(sql, conn)
sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@TransNo",
.SqlDbType = SqlDbType.VarChar,
.Value = sdate & "%"})
Try
conn.Open()
Dim rslt = sqlCmd.ExecuteScalar()
If rslt Is Nothing Then
transNo = sdate & "0001"
Else
transNo = (CLng(rslt) 1).ToString()
End If
Catch ex As Exception
MsgBox(ex.Message, vbCritical)
End Try
End Using
Return transNo
End Function
The compiler is being confused by Return
with the name of the function.
Also, I used a parameter to pass the value - you should always use parameters as it is a good habit and prevents a lot of potential problems.
You must not try re-using a database connection (cn
in the code in the question) more than once, as that too can lead to confusion. The database is made to be efficient creating a new connection every time.
CodePudding user response:
It's because your function have a way where "GetTransNo" don't get any value : when it leave the try catch immediatly. There is 2 solutions :
Define a default value for GetTransNo :
Function GetTransNo() As String
GetTransNo = ""
Try
[...]
End Try
Return GetTransNo
End Function
Define a value in the catch, when the database reading fail :
Function GetTransNo() As String
Try
[...]
Catch ex As Exception
cn.Close()
MsgBox(ex.Message, vbCritical)
GetTransNo = ""
End Try
Return GetTransNo
End Function
And when you call the function, you can check if it retuns "" (= error occured), but in your case, it's not very useful.