Validation form vb.net - vb.net

Stupid question!
I'm checking if the inputboxes are empty...but after the check, I want to navigate back to my form and give the user a second chance to change their input.
At this moment, the app will show a messagebox if it's empty, but he goes further in my code to the second check...is there a code where I can break the code an go back to the form?
My code:
If naam = "" Then
MessageBox.Show("Naam mag niet leeg zijn", "No entry",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
prijsstr = TextBox2.Text
If prijsstr = "" Then
MsgBox("Prijs mag niet leeg zijn")
ElseIf IsNumeric(prijsstr) = False Then
MsgBox("Prijs moet numeriek zijn")
Else
prijs = Integer.Parse(prijsstr)
End If
If prijs < 0 Then
MsgBox("Prijs mag niet onder 0 zijn")
End If

Couldn't you just Return?
If naam = "" Then
MessageBox.Show("Naam mag niet leeg zijn", "No entry",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
prijsstr = TextBox2.Text
If prijsstr = "" Then
MsgBox("Prijs mag niet leeg zijn")
Return
ElseIf IsNumeric(prijsstr) = False Then
MsgBox("Prijs moet numeriek zijn")
Return
Else
prijs = Integer.Parse(prijsstr)
End If
If prijs < 0 Then
MsgBox("Prijs mag niet onder 0 zijn")
Return
End If

Related

I have a problem with my if statement, doesn't execute my code correctly in my vb.net code

I'm creating a new system for a client, and I my if statement doesn't to execute correctly on my Customer Form when I press create customer.
I have tried switching to Case Statement didn't resolve my problem, then went back to if statement then I made a line for each of the required fields that needs to be filled. When I get to fill in Account Details Nothing happens as well.
VB.NET
If btnNew.Text = "New Customer" Then
cuscontrol.TabPages.Remove(cuslist)
cuscontrol.TabPages.Add(cusnew)
btnNew.Text = "Create Customer"
btnSMS.Visible = False
btnMail.Visible = False
btnDoc.Visible = False
btnClose.Text = "Cancel"
ElseIf btnNew.Text = "Create Customer" Then
If cusname.Text = "" Or custype.Text = "" Or paytype.Text = "" Or cname.Text = "" Or idtype.Text = "" Or idno.Text = "" Or title.Text = "" Or cmobile.Text = "" Or cmail.Text = "" Then
MetroMessageBox.Show(Me, "Complete Required Fields!", "BMS Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf acc.Visible = True Then
If accno.Text = "" Or accname.Text = "" Or accper.Text = "" Then
MetroMessageBox.Show(Me, "Complete Required Fields!", "BMS Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
ElseIf cntype.Enabled = True Then
If cntype.Text = "" Then
MetroMessageBox.Show(Me, "Complete Required Fields!", "BMS Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
ElseIf custype.Text = "Business" Then
If vat.Text = "" Or office.Text = "" Or mail.Text = "" Then
MetroMessageBox.Show(Me, "Complete Required Fields!", "BMS Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
ElseIf custype.Text = "Business" Then
If vat.Text = "" Or office.Text = "" Or mail.Text = "" Then
MetroMessageBox.Show(Me, "Complete Required Fields!", "BMS Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
MessageBox.Show("Success")
End If
End If
I don't get any error, nothing happens, the Success MessageBox is suppose to show if the code executed right.
I need my form when I press the Create Customer Button It needs to check if all required fields all filled (I didn't yet start with validation) in. If Null Then MessageBox.Show("Please Complete Required Fields")

Conversion from string "Label9" to type 'Double' is not valid

I have made a tableadapter query that displays the result on the label9.text
but i need to make a ifelse statement where if the label9.text <= 0 then it will display a messagebox.
Dim red = Me.SlpdetailsTableAdapter.ScalarQuery2(Label7.Text)
Dim pay = Label9.Text
If pay <= 0 Then
MessageBox.Show("This Loan is already fully paid", "Paid")
Else
Label9.Text = red
Label10.Text = Val(Label9.Text) - Val(Label8.Text)
Me.SlpsummaryTableAdapter.UpdateQuery2(Label10.Text, Label7.Text)
Me.SlpdetailsTableAdapter.InsertQuery(Label7.Text, Label6.Text, DateTimePicker1.Value.Date, Label8.Text, Label10.Text, remtxt.Text)
Try
remtxt.Text = ""
DateTimePicker1.Text = ""
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
MessageBox.Show("Loan Payment is added", "Added")
Me.Close()
SLPDetail.SlpdetailsTableAdapter.FillBy(SlpdbDataSet.slpdetails, Label7.Text)
End If
If you are sure label9.text is getting value ..then try parsing it to double as below and try
Try
Dim pay As Double = Double.Parse(Label9.Text)
If pay <= 0 Then
MessageBox.Show("This Loan is already fully paid", "Paid")
Else
''Do what ever you want
End If
Catch e As Exception
''error means label9 text is not valid numeric value
MessageBox.Show("Wrong value", "Not Valid Pay value")
End Try

How to check if a value already exists in DataGridView?

I am trying to check each row in the DataGridView whenever I add an Item in order to avoid duplicates. But my code only allows me to check the first data I added.
Here is my code given below:
For Each row In BarcodePrintListGrid.Rows
If Label44.Text = row.Cells("Barcode ID").Value Then
MetroMessageBox.Show(Me, "Item Already Added", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Label47.Text = "None"
Label44.Text = "None"
Label42.Text = "None"
Label31.Text = "None"
Label40.Text = "0"
TextBox1.Clear()
Exit For
Else
BarcodePrintListGrid.Rows.Add(Label47.Text, Label44.Text, Label42.Text, Label31.Text, Label40.Text, 1)
MetroMessageBox.Show(Me, "Item Added to List", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Label47.Text = "None"
Label44.Text = "None"
Label42.Text = "None"
Label31.Text = "None"
Label40.Text = "0"
TextBox1.Clear()
Exit For
End If
Next
You have to wait until the loop has gone all the rows of your DataGridView before deciding to add the row to it or not.
Try with this code :
Dim test As Boolean = False
For Each row In BarcodePrintListGrid.Rows
If Label44.Text = row.Cells("Barcode ID").Value Then
test=true
Exit For
End If
Next
if test=false then
BarcodePrintListGrid.Rows.Add(Label47.Text, Label44.Text, Label42.Text, Label31.Text, Label40.Text, 1)
MetroMessageBox.Show(Me, "Item Added to List", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
else
MetroMessageBox.Show(Me, "Item Already Added", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
end if
Label47.Text = "None"
Label44.Text = "None"
Label42.Text = "None"
Label31.Text = "None"
Label40.Text = "0"
TextBox1.Clear()

How to display the image of the selected data to a picture box from database vb.net

I have a datagridview filled with data. Now I want to display the image of the selected data to a picture box. The code works with text and dates no matter what data i click it changes but not on image.
I got a error message 'Unable to cast object type 'System.Byte[]' to type System.Drawing.Image '
Sample code below
Private Sub selected()
Private Sub selected()Try
If Me.dgvadt.Rows.Count > 0 Then
If Me.dgvadt.SelectedRows.Count > 0 Then
If Me.dgvadt.SelectedRows(0).Cells("idComplaints").Value Is DBNull.Value = True Then
Me.txtIDcomplaint.Text = ""
Else
Me.txtIDcomplaint.Text = Me.dgvadt.SelectedRows(0).Cells("idComplaints").Value
End If
If Me.dgvadt.SelectedRows(0).Cells("DateFiled").Value Is DBNull.Value = True Then
Me.txtDF.Text = ""
Else
Me.txtDF.Text = Me.dgvadt.SelectedRows(0).Cells("DateFiled").Value
End If
If Me.dgvadt.SelectedRows(0).Cells("AccountNumber").Value Is DBNull.Value = True Then
Me.txtAC.Text = ""
Else
Me.txtAC.Text = Me.dgvadt.SelectedRows(0).Cells("AccountNumber").Value
End If
If Me.dgvadt.SelectedRows(0).Cells("Attachment").Value Is DBNull.Value = True Then
Else
pbPicture.Image = Me.dgvadt.SelectedRows(0).Cells("Attachment").Value
End If
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
MessageBox.Show("Click to View", "PELCO III ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub

searching persons in a database from vb.net form

I am building an application in VB.net and trying to get detail information from the database from de persons name that I write in a textbox, i've the following code (i get an eeror, can not find column "the name i give in the textbox").Can somebody help me??
in the load of the form :
mijnKlant.KlantVullen()
dgvKlanten.DataSource = mijnKlant.p_klant
With Me.dgvKlanten
.Columns("klant_id").DisplayIndex = 0
.Columns("klant_id").HeaderText = "Klantnummer"
.Columns("naam").DisplayIndex = 1
.Columns("naam").HeaderText = "Naam"
.Columns("voornaam").DisplayIndex = 2
.Columns("voornaam").HeaderText = "Voornaam"
.Columns("straat_nr").DisplayIndex = 3
.Columns("straat_nr").HeaderText = "Straat en Nr."
.Columns("postcode").DisplayIndex = 4
.Columns("postcode").HeaderText = "Postcode"
.Columns("naam_gemeente").DisplayIndex = 5
.Columns("naam_gemeente").HeaderText = "Gemeente"
.Columns("landen_naam").DisplayIndex = 6
.Columns("landen_naam").HeaderText = "Land"
.Columns("code_afwijkend_postadres").DisplayIndex = 7
.Columns("code_afwijkend_postadres").HeaderText = "Postadres"
.Columns("facturatieadres_straat_nr").DisplayIndex = 8
.Columns("facturatieadres_straat_nr").HeaderText = "Facturatie Straat en Nr."
.Columns("facturatieadres_postcode").DisplayIndex = 9
.Columns("facturatieadres_postcode").HeaderText = "Facturatie Postcode"
.Columns("facturatieadres_gemeente").DisplayIndex = 10
.Columns("facturatieadres_gemeente").HeaderText = "Facturatie Gemeente"
.Columns("facturatieadres_land").DisplayIndex = 11
.Columns("facturatieadres_land").HeaderText = "Facturatie Land"
.Columns("identiteitskaart_code").DisplayIndex = 12
.Columns("identiteitskaart_code").HeaderText = "Identiteitskaartnummer"
.Columns("postcode_id").Visible = False
.Columns("land_id").Visible = False
.Columns("land_id1").Visible = False
.Columns("postcode_id1").Visible = False
.Columns("postcode_gemeente").Visible = False
.RowHeadersVisible = True
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.Fill
.AllowUserToOrderColumns = False
.AllowUserToResizeColumns = False
.AllowUserToResizeRows = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End With
on the presentation layer:
'enablen van buttons zodat de functie gebruikt kan worden
Me.btnVerwijderKlant.Enabled = True
Me.btnWijzigKlant.Enabled = True
Dim strNaam As String
strNaam = Me.txtNaam.Text 'geeft de naam in de databank
'If outindex = -1 Then Exit Sub
Dim myrw As DataRowView
'functie aanroepen op DAL
myrw = mijnKlant.klant_detail(strNaam)
'binden van opgehaalde details aan de toegewezen controls
Me.txtNaam.DataBindings.Add("Text", myrw, "naam")
Me.txtNaam.DataBindings.Clear()
Me.txtVoornaam.DataBindings.Add("Text", myrw, "voornaam")
Me.txtVoornaam.DataBindings.Clear()
Me.txtStraat.DataBindings.Add("Text", myrw, "straat_nr")
Me.txtStraat.DataBindings.Clear()
Me.cboGemeente.DataBindings.Add("Text", myrw, "naam_gemeente")
Me.cboGemeente.DataBindings.Clear()
Me.cbxAfwijkenAdres.DataBindings.Add("text", myrw, "code_afwijkend_postadres")
'omzetting van string naar boolean
If cbxAfwijkenAdres.Text = "T" Then
Me.cbxAfwijkenAdres.Checked = True
Me.cbxAfwijkenAdres.Text = "Postadres is gelijk aan het facturatieadres"
End If
If cbxAfwijkenAdres.Text = "F" Then
Me.cbxAfwijkenAdres.Checked = False
Me.cbxAfwijkenAdres.Text = "Postadres is gelijk aan het facturatieadres"
End If
'binden van opgehaalde details aan de toegewezen controls
Me.cbxAfwijkenAdres.DataBindings.Clear()
Me.txtPost.DataBindings.Add("Text", myrw, "postcode")
Me.txtPost.DataBindings.Clear()
Me.cboLand.DataBindings.Add("Text", myrw, "landen_naam")
Me.cboLand.DataBindings.Clear()
Me.txtID_Nr.DataBindings.Add("Text", myrw, "identiteitskaart_code")
Me.txtID_Nr.DataBindings.Clear()
Me.txt_F_Straat.DataBindings.Add("Text", myrw, "facturatieadres_straat_nr")
Me.txt_F_Straat.DataBindings.Clear()
Me.cbo_F_Gemeente.DataBindings.Add("Text", myrw, "facturatieadres_gemeente")
Me.cbo_F_Gemeente.DataBindings.Clear()
Me.cbo_F_Land.DataBindings.Add("Text", myrw, "facturatieadres_land")
Me.cbo_F_Land.DataBindings.Clear()
Me.txt_F_Post.DataBindings.Add("Text", myrw, "facturatieadres_postcode")
Me.txt_F_Post.DataBindings.Clear()
dgvKlanten.DataSource = mijnKlant.p_klant
myrw = Nothing
End Sub
on my datalayer:
Public Sub KlantVullen()
' methode om dataview op te bouwen die dan later als bron zal dienen voor de zoekcombo in ons
Dim sqlStr As String = "SprKlantzoeken"
Dim objConn As New SqlConnection(conStr.ConnString)
objConn.Open()
Dim adapter As New SqlDataAdapter(sqlStr, objConn)
'opvangen van foutmelding bij het ledigen van tabel klanten in dataset
Try
ds.Tables.Remove("klanten")
Catch ex As ArgumentException
End Try
adapter.Fill(ds, "klanten")
dt_klanten = ds.Tables("klanten")
dv_klanten = dt_klanten.DefaultView
dv_klanten.Sort = "klant_id"
adapter.Dispose()
adapter = Nothing
objConn.Close()
objConn.Dispose()
End Sub
' functie voor details op te vragen van klanten
Public Function klant_detail(ByVal i_index As String) As DataRowView
Dim dv As DataView
Dim anyrow As DataRowView
dv = New DataView
With dv
.Table = ds.Tables("klanten")
.AllowDelete = True
.AllowEdit = True
.AllowNew = True
.RowFilter = "naam = " & i_index
End With
anyrow = dv.Item(0) 'geeft de eerste rij van de dataview dv
' Simple bind to a TextBox control
dv = dt_klanten.DefaultView
Return anyrow
dv.Dispose()
dv = Nothing
End Function
Stored procedure in SQL database:
USE [parken3]
GO
/* Object: StoredProcedure [dbo].[SprKlantzoeken] Script Date: 07/13/2011 13:45:07 */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SprKlantzoeken]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
select * from landen_lijst l , klant k , postcode p
where k.postcode_id = p.postcode_id
and k.land_id = l.land_id
END
You need to change the line where you set .Rowfilter
With dv
.Table = ds.Tables("klanten")
.AllowDelete = True
.AllowEdit = True
.AllowNew = True
.RowFilter = "naam = " & i_index <---------
End With
Change it to this:
.RowFilter = "naam = '" & i_index.Replace("'", "''") & "'"