ToString("C") does not show expected result in VB - vb.net

The input changes to $0.00 whenever i try this coding:
Private Sub btnEnt_Click(sender As Object, e As EventArgs) Handles btnEnt.Click
Dim result As String
Dim earn As Decimal
txtEarning.Text = earn.ToString("C")
result = txtFName.Text & " " & txtLName.Text & " Worked" & vbCrLf
result &= nudHr.Value & " Hrs today"
result &= " and his earning is " & txtEarning.Text
txtRes.Text = result
End Sub
instead of displaying input value, the value automatically converts to $0.00

I think you might need this:
Dim earn as Decimal = nudHr.Value * 20
If your person earns 20 dollars an hour

Related

SUMIFS for vb.net

Good day,
How can I do like sumifs in sql query in vb.net
Private Sub txtProcYM_TextChanged(sender As Object, e As EventArgs) Handles txtProcYM.TextChanged
Try
query = "SELECT SUM(prdInput) FROM ProdOutput WHERE HELPER ='" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';"
retrieveSingleResult(query)
proc1QTYIn.Text = dt.Rows(0)("prdInput").ToString()
Catch ex As Exception
End Try
End Sub
here is my code. The plan is to take the Sum of column prdInput based on the helper which is txtlot txtPartnumber and txprocess.
I managed to get display the first prdinput the query found but I need the the sum of its since I have multiple txtlot partnumber and process.
Thanks
Edit: I already did the SUM(prdInput) on my query but nothing shows on the textbox.
You're almost there. You just needed to alias the column that you performed the sum aggregate on:
Private Sub txtProcYM_TextChanged(sender As Object, e As EventArgs) Handles txtProcYM.TextChanged
Try
query = "SELECT SUM(prdInput) AS prdInput FROM ProdOutput WHERE HELPER ='" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';"
retrieveSingleResult(query)
proc1QTYIn.Text = dt.Rows(0)("prdInput").ToString()
Catch ex As Exception
End Try
End Sub
It might be easier to see the difference with a formatted query:
SELECT
SUM(prdInput) AS prdInput --you forgot to give this column a name
FROM ProdOutput
WHERE HELPER = '" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';

I am getting "Conversion from String to type Double is invalid" in a database insert query

I'm developing an application for a store where the bill number, Customer's Name and a few such values are inserted from the Text box and other vales such as Product, Price, etc. are from the DataGridView, but there is an error when I try to save the data from the DataGridView.
I've tried many solutions provided on internet and youtube, but none worked.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim product As String
Dim price, discount, quantity As Integer
For x As Integer = 0 To DataGridView2.RowCount
product = DataGridView2.Rows(x).Cells(0).ToString
price = DataGridView2.Rows(x).Cells(1).Value.ToString
discount = DataGridView2.Rows(x).Cells(2).Value.ToString
quantity = DataGridView2.Rows(x).Cells(3).Value.ToString
query = "insert into Bill values(" + TextBox1.Text + ",'" + product + "'," + price + ",'" + TextBox3.Text + "'," + discount + "," + quantity + "," + TextBox11.Text + ")"
cmd = New OleDbCommand(query, Log)
Try
Log.Open()
cmd.ExecuteNonQuery()
MsgBox("DATA SAVED", MsgBoxStyle.Information)
Log.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
End Sub
I just want the values from the text box and data grid view to be saved in the database, but there is an error stating
Conversion from String to type Double is invalid.
The Error is on the insert query.
I've tried many solutions provided on the internet, but none worked for me.
FIRSTLY please try use stored procedures they are MUCH MUCH more easy to read and write especially when stuff like this happens
So you would have a stored procedure in your database editing software such as
CREATE PROCEDURE [dbo].[SEND_BILL_DETAILS](#PRODUCT VARCHAR(MAX),#PRICE INT,DISCOUNT VALUETYPE, #QUANTITY INT)
AS
BEGIN
insert into Bill values(#PRODUCT,#PRICE,#DISCOUNT,#QUANTITY)
END
Then in your VB you would have something similar like this to aply the variables and execute the SP.
private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim product As String
Dim price, discount, quantity As Integer
For x As Integer = 0 To DataGridView2.RowCount
product = DataGridView2.Rows(x).Cells(0).ToString
price = DataGridView2.Rows(x).Cells(1).Value.ToString
discount = DataGridView2.Rows(x).Cells(2).Value.ToString
quantity = DataGridView2.Rows(x).Cells(3).Value.ToString
dim cmd as new sqlcommand("SEND_BILL_DETAILS",con)
cmd.commandtype = commandtype.storeprocedure
cmd.parameter.add(#product,sqldbtype.varchar).value = product.tostring()
-----do one for each variable in stored procedure.
Try
log.Open()
cmd.ExecuteNonQuery()
MsgBox("DATA SAVED", MsgBoxStyle.Information)
log.Close()
cmd.parameters.clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
End Sub
Now for you could I am assuming its a case of replacing all your + in your sql query to & as the program is attempting to literally add the words together so therefore would be something like this:
FOR YOUR WAY OF DOING IT
query = "insert into Bill values( '" & TextBox1.Text &",'" & product & "'," & price & ",'" & TextBox3.Text & "'," & discount & "," & quantity & "," & TextBox11.Text & ")"

How to fix this logical error about listbox

I'd like to create a list box in vb.net that lists the item whenever the user clicks the button it updates information, my variables are a string and an integer concatenated, the problem is that when the user clicks on the button the item doesn't remove on the list
I've tried converting the integer variable to string and it still doesn't work
Private Sub btnAddChicken_Click(sender As Object, e As EventArgs) Handles btnAddChicken.Click
If cmbChicken.SelectedIndex = 0 Then
'''qfried is the quantity
'''tfried is the total amount
'''tfried and qfried are the variable i want to update
qfried = qfried + 1
tfried = tfried + pfried
If ListAllOrders.Items.Contains("Fried Chicken - P" & Convert.ToString(tfried) & " Quantity (" & Convert.ToString(qfried) & ")") Then
ListAllOrders.Items.Remove("Fried Chicken - P" & Convert.ToString(tfried) & " Quantity (" & Convert.ToString(qfried) & ")")
ListAllOrders.Items.Add("Fried Chicken - P" & tfried & " Quantity - (" & qfried & ")")
i += 1
Else
ListAllOrders.Items.Add("Fried Chicken - P" & tfried & " Quantity - (" & qfried & ")")
End If
End If
End Sub
I expect that the information would be updated
The remove option of the ListBox does not work with Strings, but with ListBoxItems. For you to remove it, you can do it like this:
For Each item In ListAllOrders.Items
If item.Contains(desiredStringToRemove) Then
ListAllOrders.Items.Remove(item)
Exit for
End If
Next

How to Group by Name in Treeview Control

I have been googling and searching youtube but find no answer, so I would like to ask for help in here.
I want to group the name in the TreeView control in VB, how can I do it?
Thank you
Public Class FrmPengingat
Private Sub FrmPengingat_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DSLap.PengingatHutang' table. You can move, or remove it, as needed.
Me.PengingatHutangTableAdapter.Fill(Me.DSLap.PengingatHutang)
'fill the tree control
Dim NmPemasok As String
Dim NoNota As String
Dim TglJatuhTempo As Date
Dim Total As Decimal
Dim tmpNmPemasok As String
Dim i As Integer
For i = 0 To DSLap.PengingatHutang.Count - 1
NmPemasok = DSLap.PengingatHutang.Rows(i).Item(0)
tmpNmPemasok = DSLap.PengingatHutang.Rows(i).Item(0)
NoNota = DSLap.PengingatHutang.Rows(i).Item(1)
TglJatuhTempo = DSLap.PengingatHutang.Rows(i).Item(2)
Total = DSLap.PengingatHutang.Rows(i).Item(3)
TreeView1.Nodes.Add(i, NmPemasok).Nodes.Add(i, NoNota & " (" & TglJatuhTempo & ") " & Total)
Next
End Sub
End Class
This line:
TreeView1.Nodes.Add(i, NmPemasok).Nodes.Add(i, NoNota & " (" & TglJatuhTempo & ") " & Total)
adds a new parent node every time. You should check if the tree view already contains a node with the same text and only add a new one if it doesn't.
I suggest you make the key the same as the text for the parent node, that way it'll be easier to check if it exists or not.
Replace the above with this:
If TreeView1.Nodes.ContainsKey(NmPemasok) Then 'The parent node already exists.
TreeView1.Nodes(NmPemasok).Nodes.Add(i, NoNota & " (" & TglJatuhTempo & ") " & Total)
Else 'The parent node doesn't exist.
TreeView1.Nodes.Add(NmPemasok, NmPemasok).Nodes.Add(i, NoNota & " (" & TglJatuhTempo & ") " & Total)
End If

If or case statement based on radio button click

I am trying to find a way to run some code based on the selection of a radio button. I have several radio buttons in a groupbox which will run different code based on there selection. Now, being a fairly new user to VB.NET, I am struggling to correctly code this.
Would I be better to use an IF statement or a SELECT CASE statement. I have tried using a flag set as a boolean to indicate if button1 is selected, set flag = true. That is as far as I have got. I am using CheckedChanged event to handle to event changes. I have included some code and would be grateful if someone could start me off. Many thanks.
Private Sub rdbBoxReturn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbBoxReturn.CheckedChanged
'code goes here
flagBoxReturn = True
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
If flagBoxReturn = True Then
MessageBox.Show(CStr(flagBoxReturn))
Return
Else
DBConnection.connect()
sql = "SELECT MAX([Id]) from Request Boxes WHERE Customer = '" & cmbCustomer.Text & "' "
'MessageBox.Show(cmbCustomer.Text)
'sql = "INSERT INTO [Requests] ("")"
'Dim sql As String = "SELECT * from Boxes WHERE Customer = ? AND Status = 'i'"
Dim cmd As New OleDb.OleDbCommand
Dim id As String
Dim requestor As String = "DEMO"
Dim intake As String = "I"
Dim status As String = "O"
'cmd.Parameters.AddWithValue("#p1", cmbCustomer.Text)
cmd.CommandText = sql
cmd.Connection = oledbCnn
dr = cmd.ExecuteReader
'lvSelectRequestItems.Items.Clear()
While dr.Read()
id = CStr(CInt(dr.Item(0).ToString))
id = String.Format("{0:D6}", (Convert.ToInt32(id) + 1))
'id = CStr(CDbl(id) + 1)
End While
MessageBox.Show(CStr(id))
dr.Close()
sql = "INSERT INTO [Request Boxes] ([Request no], Customer, Dept, [Type], [Service level], [Date-time received], [Received by], [Date-time due], Quantity, [Cust requestor], Status ) " &
"VALUES ('" & id & "', '" & cmbCustomer.Text.ToUpper & "', '" & cmbDept.Text & "', '" & intake.ToString & "', '" & rbServiceLevel.ToString & "', '" & dtpDateReceived.Value & "', '" & requestor.ToString & "', '" & dtpDateDue.Value & "', '" & txtBoxQuantity.Text & "', '" & requestor.ToString & "', '" & status & "')"
cmd.CommandText = sql
cmd.ExecuteNonQuery()
cmd.Dispose()
oledbCnn.Close()
flagBoxReturn = False
MessageBox.Show("Your record number: " & id & " Was entered successfully")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
How about something like this...
Public Enum SaveOption As Int32
DoNothing = 0
DoSomething = 1 ' Obviously rename this to something that makes sense in your situation.
End Enum
Public Function GetSaveOption() As SaveOption
Dim result As SaveOption = SaveOption.DoNothing
If rdbBoxReturn.Checked Then
result = DoSomething
End If
' Add as many if statements her to cover all your radio buttons.
Return result
End Function
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Select Case GetSaveOption
Case SaveOption.DoNothing
Exit Sub
Case SaveOption.DoSomething
' Your save code here
End Select
End Sub
This method makes your code more readable by converting UI element states into program states.
Switch statement is better if the number of comparisons is small
if you had a radio button list control, that would be much better as in that case
switch statement can be passed the index variable (SelectedIndex property of the radio
button list) but that control is available in web forms, or can be available in win forms
if you find some free user/custom control etc.
so in your case, better to use if else statements