Reputation: 75
When I call setText method it wont return anything.
setText("Sprite", "lblSpriteQty.Text", "lblSpriteTP.Text", 15)
Code I use for method setText
Public Sub setText(ByVal productname As String, ByVal qty As String, ByVal tp As String, ByVal price As Integer)
Using cs As New SqlConnection("Data Source=KARL-PC;Initial Catalog=ShawarmaToldb;Integrated Security=True")
Using cmd As New SqlCommand("SELECT SUM(quantity) FROM [tblOrderDetails] WHERE productid = (SELECT productid FROM [tblProduct] WHERE productname = @productname)", cs)
cmd.Parameters.AddWithValue("@productname", productname)
cmd.CommandType = CommandType.Text
cs.Open()
cmd.ExecuteNonQuery()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
If IsDBNull(dr.GetValue(0)) Then
qty = "0" 'but if i put lblSpriteQty.Text instead of qty it's working
tp = "0" 'same as here
Else
qty = dr.GetValue(0).ToString
tp = dr.GetValue(0).ToString * price
End If
End While
End Using
End Using
End Sub
Upvotes: 0
Views: 104
Reputation: 460138
You are modifying local variables(the method arguments) instead of returning qty
and tp
to the calling method. But even if you made them ByRef
you couldn't change them since strings are immutable. Instead either set the text in this method or make it a Function
that returns an Integer
for the quantity which is all that you need.
Public Function GetSumQuantity(ByVal productname As String) As Integer
Using cs As New SqlConnection("Data Source=KARL-PC;Initial Catalog=ShawarmaToldb;Integrated Security=True")
Using cmd As New SqlCommand("SELECT SUM(quantity) FROM [tblOrderDetails] WHERE productid = (SELECT productid FROM [tblProduct] WHERE productname = @productname)", cs)
cmd.Parameters.AddWithValue("@productname", productname)
cs.Open()
Dim sumQuantityObj = cmd.ExecuteScalar()
If DbNull.Value = sumQuantityObj Then Return 0
Return DirectCast(sumQuantityObj, Integer)
End Using
End Using
End Sub
Now you can calculate the value and assign it to the label's text in the calling method:
Dim sumQuantity As Integer = GetSumQuantity("IPhone")
Dim tp = price * sumQuantity
lblSpriteQty.Text = tp.ToString()
Upvotes: 1