Close Form if SQL Query is empty - mysql

I have a form to conclude ongoing operations with an sql query that will only show me the ongoing ops on the combobox. If there are no ongoing ops the program crashes! How do i prevent it from crashing?
Public conexao As New MySqlConnection
Public comando As New MySqlCommand
Public result As MySqlDataReader
Public r3 As MySqlDataReader
Public bs As BindingSource = New BindingSource()
Public strsql As String
Private Sub editarops_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
conexao.ConnectionString = "server = localhost; user =root; database = frota;"
conexao.Open()
comando.Connection = conexao
strsql = "SELECT codtrab, carga, origem, destino, estado, dataini FROM trabalhos "
comando.Connection = conexao
strsql = "SELECT codtrab, carga, origem, destino, estado, dataini FROM trabalhos "
r3 = comando.ExecuteReader
bs.DataSource = r3
ComboBox1.Text = bs.Current(0)
tboxfunc.Text = bs.Current(1)
TextBox1.Text = bs.Current(2)
TextBox2.Text = bs.Current(3)
TextBox3.Text = bs.Current(5)
Atualizar.Enabled = True
Cancelar.Enabled = True
r3.Close()
Dim sStmt1 As String = "Select codtrab from trabalhos where estado = 'En Route' "
Dim cmd1 As New MySqlCommand(sStmt1, conexao)
Dim da1 As MySqlDataAdapter = New MySqlDataAdapter(cmd1)
Dim dt1 As New DataTable("trabalhos")
da1.Fill(dt1)
ComboBox1.DataSource = dt1
ComboBox1.DisplayMember = "codtrab"
da1.Dispose()
repor()
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
TextBox6.Enabled = False
tboxfunc.Enabled = False
End Sub

Related

Printing and storing receipt in VB.net

I am designing a POS similar program to add inventory and print and store the receipt. May I know how do I proceed? Is there a way I can store the receipt in mysql database?
Here's my screenshot of the POS and the source code
Screenshot of the sale screen: http://oi64.tinypic.com/35cpy5j.jpg
Upon clicking on the print receipt button I want to generate a pdf and send it to print as well store them so we can access them anytime. What is the best strategy to achieve the same.
Imports MySql.Data.MySqlClient
Imports System.Text.RegularExpressions
Public Class sale
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Dim iReturn As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Dim adapter As New MySqlDataAdapter("SELECT `itemname`, `itemcode`, `saleprice`, `quantity` FROM inventory WHERE quantity > 0", connection)
Dim table As New DataTable()
adapter.Fill(table)
BunifuDropdown1.DataSource = table
BunifuDropdown1.ValueMember = "itemname"
BunifuDropdown1.DisplayMember = "itemname"
End Sub
Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles BunifuTextBox4.KeyPress, BunifuTextBox5.KeyPress, BunifuTextBox6.KeyPress, BunifuTextBox7.KeyPress, BunifuTextBox8.KeyPress, BunifuTextBox14.KeyPress, BunifuTextBox15.KeyPress, BunifuTextBox20.KeyPress
If Not Char.IsNumber(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then
e.Handled = True
End If
End Sub
Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BunifuTextBox4.TextChanged, BunifuTextBox5.TextChanged, BunifuTextBox6.TextChanged, BunifuTextBox7.TextChanged, BunifuTextBox8.TextChanged, BunifuTextBox14.TextChanged, BunifuTextBox15.TextChanged, BunifuTextBox20.TextChanged
Dim digitsOnly As Regex = New Regex("[^\d]")
BunifuTextBox4.Text = digitsOnly.Replace(BunifuTextBox4.Text, "")
BunifuTextBox5.Text = digitsOnly.Replace(BunifuTextBox5.Text, "")
BunifuTextBox6.Text = digitsOnly.Replace(BunifuTextBox6.Text, "")
BunifuTextBox7.Text = digitsOnly.Replace(BunifuTextBox7.Text, "")
BunifuTextBox8.Text = digitsOnly.Replace(BunifuTextBox8.Text, "")
BunifuTextBox14.Text = digitsOnly.Replace(BunifuTextBox14.Text, "")
BunifuTextBox15.Text = digitsOnly.Replace(BunifuTextBox15.Text, "")
BunifuTextBox20.Text = digitsOnly.Replace(BunifuTextBox20.Text, "")
End Sub
Private Sub TextBoxT_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles BunifuTextBox9.KeyPress, BunifuTextBox10.KeyPress, BunifuTextBox11.KeyPress, BunifuTextBox12.KeyPress, BunifuTextBox13.KeyPress, BunifuTextBox16.KeyPress, BunifuTextBox17.KeyPress, BunifuTextBox19.KeyPress, BunifuTextBox22.KeyPress
If Not Char.IsNumber(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) AndAlso Not e.KeyChar = "." Then
e.Handled = True
End If
End Sub
Private Sub TextBoxT_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BunifuTextBox9.TextChanged, BunifuTextBox10.TextChanged, BunifuTextBox11.TextChanged, BunifuTextBox12.TextChanged, BunifuTextBox13.TextChanged, BunifuTextBox16.TextChanged, BunifuTextBox17.TextChanged, BunifuTextBox19.TextChanged, BunifuTextBox22.TextChanged
Dim digitsOnly As Regex = New Regex("[^\d]")
BunifuTextBox9.Text = digitsOnly.Replace(BunifuTextBox9.Text, "")
BunifuTextBox10.Text = digitsOnly.Replace(BunifuTextBox10.Text, "")
BunifuTextBox11.Text = digitsOnly.Replace(BunifuTextBox11.Text, "")
BunifuTextBox12.Text = digitsOnly.Replace(BunifuTextBox12.Text, "")
BunifuTextBox13.Text = digitsOnly.Replace(BunifuTextBox13.Text, "")
BunifuTextBox16.Text = digitsOnly.Replace(BunifuTextBox16.Text, "")
BunifuTextBox17.Text = digitsOnly.Replace(BunifuTextBox17.Text, "")
BunifuTextBox19.Text = digitsOnly.Replace(BunifuTextBox19.Text, "")
BunifuTextBox22.Text = digitsOnly.Replace(BunifuTextBox22.Text, "")
End Sub
Private Sub BunifuImageButton1_Click(sender As Object, e As EventArgs) Handles BunifuImageButton1.Click
ownerhome.Show()
Me.Hide()
End Sub
Private Sub BunifuImageButton2_Click(sender As Object, e As EventArgs) Handles BunifuImageButton2.Click
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim connection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Dim adapter As New MySqlDataAdapter("SELECT `itemname`, `saleprice`, `quantity` FROM inventory WHERE quantity > 0", connection)
Dim table As New DataTable()
adapter.Fill(table)
BunifuDropdown2.DataSource = table
BunifuDropdown2.ValueMember = "itemname"
BunifuDropdown2.DisplayMember = "itemname"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim connection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Dim adapter As New MySqlDataAdapter("SELECT `itemname`, `saleprice`, `quantity` FROM inventory WHERE quantity > 0", connection)
Dim table As New DataTable()
adapter.Fill(table)
BunifuDropdown3.DataSource = table
BunifuDropdown3.ValueMember = "itemname"
BunifuDropdown3.DisplayMember = "itemname"
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim connection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Dim adapter As New MySqlDataAdapter("SELECT `itemname`, `saleprice`, `quantity` FROM inventory WHERE quantity > 0", connection)
Dim table As New DataTable()
adapter.Fill(table)
BunifuDropdown4.DataSource = table
BunifuDropdown4.ValueMember = "itemname"
BunifuDropdown4.DisplayMember = "itemname"
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim connection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Dim adapter As New MySqlDataAdapter("SELECT `itemname`, `saleprice`, `quantity` FROM inventory WHERE quantity > 0", connection)
Dim table As New DataTable()
adapter.Fill(table)
BunifuDropdown5.DataSource = table
BunifuDropdown5.ValueMember = "itemname"
BunifuDropdown5.DisplayMember = "itemname"
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim connection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Dim adapter As New MySqlDataAdapter("SELECT `itemname`, `saleprice`, `quantity` FROM inventory WHERE quantity > 0", connection)
Dim table As New DataTable()
adapter.Fill(table)
BunifuDropdown6.DataSource = table
BunifuDropdown6.ValueMember = "itemname"
BunifuDropdown6.DisplayMember = "itemname"
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim connection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Dim adapter As New MySqlDataAdapter("SELECT `itemname`, `saleprice`, `quantity` FROM inventory WHERE quantity > 0", connection)
Dim table As New DataTable()
adapter.Fill(table)
BunifuDropdown7.DataSource = table
BunifuDropdown7.ValueMember = "itemname"
BunifuDropdown7.DisplayMember = "itemname"
End Sub
Private Sub BunifuDropdown1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BunifuDropdown1.SelectedIndexChanged
Dim _isFound As Boolean = False
Using SQLConnection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "SELECT `saleprice` FROM `inventory` WHERE `itemname` = #itemname"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("#itemname", BunifuDropdown1.Text)
End With
Try
SQLConnection.Open()
Dim myReader As MySqlDataReader = sqlCommand.ExecuteReader()
iReturn = True
While myReader.Read()
_isFound = True
BunifuTextBox9.Text = myReader("saleprice").ToString
End While
Catch ex As Exception
MsgBox(ex.Message.ToString)
iReturn = False
End Try
End Using
End Using
End Sub
Private Sub BunifuDropdown2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BunifuDropdown2.SelectedIndexChanged
Dim _isFound As Boolean = False
Using SQLConnection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "SELECT `saleprice` FROM `inventory` WHERE `itemname` = #itemname"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("#itemname", BunifuDropdown2.Text)
End With
Try
SQLConnection.Open()
Dim myReader As MySqlDataReader = sqlCommand.ExecuteReader()
iReturn = True
While myReader.Read()
_isFound = True
BunifuTextBox10.Text = myReader("saleprice").ToString
End While
Catch ex As Exception
MsgBox(ex.Message.ToString)
iReturn = False
End Try
End Using
End Using
End Sub
Private Sub BunifuDropdown3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BunifuDropdown3.SelectedIndexChanged
Dim _isFound As Boolean = False
Using SQLConnection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "SELECT `saleprice` FROM `inventory` WHERE `itemname` = #itemname"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("#itemname", BunifuDropdown3.Text)
End With
Try
SQLConnection.Open()
Dim myReader As MySqlDataReader = sqlCommand.ExecuteReader()
iReturn = True
While myReader.Read()
_isFound = True
BunifuTextBox11.Text = myReader("saleprice").ToString
End While
Catch ex As Exception
MsgBox(ex.Message.ToString)
iReturn = False
End Try
End Using
End Using
End Sub
Private Sub BunifuDropdown4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BunifuDropdown4.SelectedIndexChanged
Dim _isFound As Boolean = False
Using SQLConnection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "SELECT `saleprice` FROM `inventory` WHERE `itemname` = #itemname"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("#itemname", BunifuDropdown4.Text)
End With
Try
SQLConnection.Open()
Dim myReader As MySqlDataReader = sqlCommand.ExecuteReader()
iReturn = True
While myReader.Read()
_isFound = True
BunifuTextBox12.Text = myReader("saleprice").ToString
End While
Catch ex As Exception
MsgBox(ex.Message.ToString)
iReturn = False
End Try
End Using
End Using
End Sub
Private Sub BunifuDropdown6_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BunifuDropdown6.SelectedIndexChanged
Dim _isFound As Boolean = False
Using SQLConnection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "SELECT `saleprice` FROM `inventory` WHERE `itemname` = #itemname"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("#itemname", BunifuDropdown6.Text)
End With
Try
SQLConnection.Open()
Dim myReader As MySqlDataReader = sqlCommand.ExecuteReader()
iReturn = True
While myReader.Read()
_isFound = True
BunifuTextBox16.Text = myReader("saleprice").ToString
End While
Catch ex As Exception
MsgBox(ex.Message.ToString)
iReturn = False
End Try
End Using
End Using
End Sub
Private Sub BunifuDropdown7_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BunifuDropdown7.SelectedIndexChanged
Dim _isFound As Boolean = False
Using SQLConnection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "SELECT `saleprice` FROM `inventory` WHERE `itemname` = #itemname"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("#itemname", BunifuDropdown7.Text)
End With
Try
SQLConnection.Open()
Dim myReader As MySqlDataReader = sqlCommand.ExecuteReader()
iReturn = True
While myReader.Read()
_isFound = True
BunifuTextBox17.Text = myReader("saleprice").ToString
End While
Catch ex As Exception
MsgBox(ex.Message.ToString)
iReturn = False
End Try
End Using
End Using
End Sub
Private Sub BunifuDropdown5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BunifuDropdown5.SelectedIndexChanged
Dim _isFound As Boolean = False
Using SQLConnection As New MySqlConnection("server=localhost;userid=root;password=;database=chanamotors")
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "SELECT `saleprice` FROM `inventory` WHERE `itemname` = #itemname"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("#itemname", BunifuDropdown5.Text)
End With
Try
SQLConnection.Open()
Dim myReader As MySqlDataReader = sqlCommand.ExecuteReader()
iReturn = True
While myReader.Read()
_isFound = True
BunifuTextBox13.Text = myReader("saleprice").ToString
End While
Catch ex As Exception
MsgBox(ex.Message.ToString)
iReturn = False
End Try
End Using
End Using
End Sub
Private Sub BunifuButton3_Click(sender As Object, e As EventArgs) Handles BunifuButton3.Click
Dim a, b, c, d, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z As Decimal
Dim subtotal, tax, total As Decimal
If BunifuTextBox9.Text = "" Then
a = 0
Else
a = BunifuTextBox9.Text
End If
If BunifuTextBox10.Text = "" Then
b = 0
Else
b = BunifuTextBox10.Text
End If
If BunifuTextBox11.Text = "" Then
c = 0
Else
c = BunifuTextBox11.Text
End If
If BunifuTextBox12.Text = "" Then
d = 0
Else
d = BunifuTextBox12.Text
End If
If BunifuTextBox13.Text = "" Then
f = 0
Else
f = BunifuTextBox13.Text
End If
If BunifuTextBox16.Text = "" Then
g = 0
Else
g = BunifuTextBox16.Text
End If
If BunifuTextBox17.Text = "" Then
h = 0
Else
h = BunifuTextBox17.Text
End If
If BunifuTextBox19.Text = "" Then
i = 0
Else
i = BunifuTextBox19.Text
End If
If BunifuTextBox4.Text = "" Then
j = 0
Else
j = BunifuTextBox4.Text
End If
If BunifuTextBox5.Text = "" Then
k = 0
Else
k = BunifuTextBox5.Text
End If
If BunifuTextBox6.Text = "" Then
l = 0
Else
l = BunifuTextBox6.Text
End If
If BunifuTextBox7.Text = "" Then
m = 0
Else
m = BunifuTextBox7.Text
End If
If BunifuTextBox8.Text = "" Then
n = 0
Else
n = BunifuTextBox8.Text
End If
If BunifuTextBox14.Text = "" Then
o = 0
Else
o = BunifuTextBox14.Text
End If
If BunifuTextBox15.Text = "" Then
p = 0
Else
p = BunifuTextBox15.Text
End If
If BunifuTextBox20.Text = "" Then
q = 0
Else
q = BunifuTextBox20.Text
End If
If BunifuTextBox22.Text = "" Then
z = 0
Else
z = BunifuTextBox22.Text
End If
r = a * j
s = b * k
t = c * l
u = d * m
v = f * n
w = g * o
x = h * p
y = i * q
subtotal = r + s + t + u + v + w + x + y
tax = (subtotal / 100) * z
total = subtotal + tax
Label10.Text = r
Label17.Text = s
Label16.Text = t
Label15.Text = u
Label14.Text = v
Label13.Text = w
Label12.Text = x
Label11.Text = y
Label21.Text = tax
Label23.Text = "Total Tax: "
Label19.Text = "SUB TOTAL: "
Label18.Text = total & " INR"
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs)
MsgBox(DateTimePicker1.Text)
End Sub
Private Sub BunifuButton2_Click(sender As Object, e As EventArgs) Handles BunifuButton2.Click
End Sub
End Class
You can use itextsharp.dll to generate the PDF.
And for storing the file, either store the generated PDF file on the hard drive, or only store the data, and re-generate it again on demand.

how the system recognize if the question is already answered

I want to know how the system recognize if the question is already answered or not if the user want to go back in previous question. if its already answered the answer will update either the score is increase or not or if the answer is not edited it will be the same.
BUTTON1 is to go back to previous quesion
Button2 is for checking if true or false the answwer
BUTTON 3 is for Keep going the exam
Here are my code snippets:
Imports MySql.Data.MySqlClient
Imports System.Drawing
Imports System.IO
Public Class Exam
'declaring variables for connection'
Dim score As Integer
Dim rightans As String
Dim correct As Integer = 0
Dim choice As String
Dim choice1 As String
Dim choice2 As String
Dim choice3 As String
Dim con As MySqlConnection
Dim con1 As MySqlConnection
Dim COMMAND As MySqlCommand
Dim read As MySqlDataReader
Dim da As MySqlDataAdapter
Dim sql As String
Private Sub Exam_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'setting the radiobutton to false, so that when the form load there is no shaded button'
Label2.Text = 1
A.Checked = False
B.Checked = False
C.Checked = False
D.Checked = False
con = New MySqlConnection
Button1.Enabled = False
con.ConnectionString = "server=localhost;userid=root;password=;database=user;"
'calling sub
qno()
End Sub
Sub clear()
'to set the radiobutton false/no shaded.
A.Checked = False
B.Checked = False
C.Checked = False
D.Checked = False
End Sub
Sub qno()
'calling connection'
Try
con = New MySqlConnection
con.ConnectionString = "server=localhost; user id=root; password=; database=user;"
con.Open()
sql = "SELECT * FROM user.math WHERE question_id = #ID;"
COMMAND = New MySqlCommand
With COMMAND
.Connection = con
.CommandText = sql
.Parameters.Clear()
.Parameters.AddWithValue("#ID", Label2.Text)
.ExecuteNonQuery()
End With
Dim arrImage() As Byte
Dim dt As New DataTable
da = New MySqlDataAdapter
da.SelectCommand = COMMAND
da.Fill(dt)
If dt.Rows.Count > 0 Then
arrImage = dt.Rows(0).Item(7)
Dim mstream As New System.IO.MemoryStream(arrImage)
Pic1.Image = Image.FromStream(mstream)
question.Text = dt.Rows(0).Item(1)
A.Text = dt.Rows(0).Item(2)
B.Text = dt.Rows(0).Item(3)
C.Text = dt.Rows(0).Item(4)
D.Text = dt.Rows(0).Item(5)
Else
MsgBox("No results!")
End If
Catch ex As MySqlException
MsgBox(ex.Message)
Finally
con.Close()
da.Dispose()
End Try
End Sub
Sub increment()
'incrementing the score f the answer is correct'
Dim i As Integer = 0
i = Label2.Text
i = i + 1
Label2.Text = i
If Label2.Text > 1 Then
Button1.Enabled = True
End If
End Sub
Sub decrement()
'incrementing the score f the answer is correct'
Dim i As Integer = 1
i = Label2.Text
i = i - 1
Label2.Text = i
If Label2.Text = 1 Then
Button1.Enabled = False
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'checking of the user answer the questions'
If ((A.Checked = False) And (B.Checked = False) And (C.Checked = False) And (D.Checked = False)) Then
MsgBox("Please answer the question")
Else
'if the examinee answers all the examination it will call another questions from database'
If A.Checked = True Then
con.Open()
' Dim ans As String
Dim arren As String = "A"
Dim sql As String = ("select answer from user.math where question_id = '" & Label2.Text & "' ")
COMMAND = New MySqlCommand(sql, con)
Dim it As String
read = COMMAND.ExecuteReader
If read.HasRows Then
If read.Read Then
it = read.Item("answer")
If it = choice Then
correct = correct + 1
Label4.Text = correct
ElseIf it <> choice And Label2.Text <= 1 Then
correct = correct - 1
Label4.Text = correct
End If
End If
clear()
End If
If Label2.Text = 10 Then
MessageBox.Show("proceed to other subject test")
End If
con.Close()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
decrement()
qno()
End Sub
End Class
Untested since I don't have your database. Comments above and inline.
Public Class Form3
Dim correct As Integer = 0
Private Sub Exam_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'setting the radiobutton to false, so that when the form load there is no shaded button'
Label2.Text = "1"
A.Checked = False
B.Checked = False
C.Checked = False
D.Checked = False
Button1.Enabled = False
qno()
End Sub
Private Sub clear()
'to set the radiobutton false/no shaded.
A.Checked = False
B.Checked = False
C.Checked = False
D.Checked = False
End Sub
Private Sub qno()
Try
'Using...End Using blocks will close and dispose of your objects
Using con = New MySqlConnection("server=localhost;userid=root;password=;database=user;")
Using Cmd = New MySqlCommand("SELECT * FROM user.math WHERE question_id = #ID;", con)
Cmd.Parameters.AddWithValue("#ID", Label2.Text)
Dim arrImage() As Byte
Dim dt As New DataTable
Using da = New MySqlDataAdapter
da.SelectCommand = Cmd
da.Fill(dt)
If dt.Rows.Count > 0 Then
'Not at all sure about this picture code
arrImage = CType(dt.Rows(0).Item(7), Byte())
Dim mstream As New System.IO.MemoryStream(arrImage)
Pic1.Image = Image.FromStream(mstream)
question.Text = dt.Rows(0).Item(1).ToString
A.Text = dt.Rows(0).Item(2).ToString
B.Text = dt.Rows(0).Item(3).ToString
C.Text = dt.Rows(0).Item(4).ToString
D.Text = dt.Rows(0).Item(5).ToString
'guessing that answer it item 6
HiddenLabel.Text = dt.Rows(0).Item(6).ToString
Else
MsgBox("No results!")
End If
End Using
End Using
End Using
Catch ex As MySqlException
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'checking of the user answer the questions'
If ((A.Checked = False) And (B.Checked = False) And (C.Checked = False) And (D.Checked = False)) Then
MsgBox("Please answer the question")
Exit Sub
End If
'The correct answer was put in the HiddenLabel.Text (Visible set to False)
'in the qno Sub
Dim CorrectAnswer As String = HiddenLabel.Text
Dim UserChoice As String = ""
If A.Checked Then
UserChoice = "A"
ElseIf B.Checked Then
UserChoice = "B"
ElseIf C.Checked Then
UserChoice = "C"
Else
UserChoice = "D"
End If
If UserChoice = CorrectAnswer Then
correct += 1
Else
'It is very hard to get a good score if it is decremented with every wrong answer
'Why not skip this and just give a zero for a wrong answer
correct -= 1
End If
Label4.Text = correct.ToString
clear()
If Label2.Text = "10" Then
MessageBox.Show("proceed to other subject test")
Me.Hide()
exam2.Show()
Else
'Add code to keep track of question number in
'Add code to show the next question
End If
End Sub

vb.net combobox manual and load data from db

Private Sub delivery_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sel.Items.Add("allitems")
MsgBox("alert")
Me.EntryTableAdapter.Fill(Me.TailerDataSet.entry)
viewdata.Visible = False
ConnectionState = "Data Source=SWTEAM-II-5\SQLEXPRESS;Initial Catalog=tailer;User ID=sa;Password=123"
con = New SqlConnection(ConnectionState)
con.Open()
Dim sqlquery As String
sqlquery = "Select p_name from entry"
cmd = New SqlCommand(sqlquery, con)
Dim rd As SqlDataReader = cmd.ExecuteReader
Dim dt As DataTable = New DataTable
dt.Load(rd)
dt.Rows.Add("allitems")
sel.ValueMember = "p_name"
sel.DisplayMember = "p_name"
sel.DataSource = dt
only combobox showing db values not manual values
Dim sqlquery As String
sqlquery = "Select p_name from entry"
cmd = New SqlCommand(sqlquery, con)
Dim adapter As New SqlDataAdapter(cmd)
sel.Items.Add("allitems")
Dim dap As New SqlDataAdapter("SELECT * FROM entry", con)
Dim ds As New DataSet
dap.Fill(ds)
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
sel.Items.Add(ds.Tables(0).Rows(i).Item("p_name"))
Next

Unable to display records using MySQL in VB.NET

Imports MySql.Data.MySqlClient
Public Class home1
Dim id As String
Dim name1 As String
Dim name2 As String
Dim address As String
Dim age As Integer
Dim gender As String
Dim bday As Date
Dim height1 As String
Dim weight1 As String
Dim crimcase As String
Dim eye As String
Dim con As New MySqlConnection
Dim source1 As New BindingSource()
Dim source2 As New BindingSource()
Dim ds As DataSet = New DataSet
Dim tables As DataTableCollection = ds.Tables
Private Sub home1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
con.ConnectionString = "Server=localhost;User Id=root;Password='';Database=db_criminalrecord"
Catch ex As Exception
MsgBox(ex.Message)
End Try
fill()
End Sub
Public Sub fill()
Dim dt As New DataTable
Dim str As String = "SELECT ID,Criminal_Name,Alias,Address,Age,Gender,Height,Weight,Date_of_Birth,criminal_Case,Eye_Colour "
Dim da As New MySqlDataAdapter(str, con)
da.Fill(dt)
da.Dispose()
source1.DataSource = dt
DataGridView1.DataSource = dt
DataGridView1.Refresh()
DataGridView1.Columns(13).Width = 150
End Sub
Sub clear()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
End Sub
End Class
Your select statement is not containing FROM TABLE_NAME part.

Program crashes if mysql query is empty

I have this form that shows the unfinished transporting operations so that the manager can finish them, it ony shows the ongoing ops and if there is no ongoing ops the program crashes, i've tried to avoid this by putting a MsgBox but it didn't work. How can i make this msgbox solution work?
Private Sub editarops_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
conexao.ConnectionString = "server = localhost; user =root; database = frota;"
conexao.Open()
comando.Connection = conexao
strsql = "SELECT codtrab, carga, origem, destino, estado, dataini FROM trabalhos where estado = 'En Route'"
comando.Connection = conexao
strsql = "SELECT codtrab, carga, origem, destino, estado, dataini FROM trabalhos where estado = 'En Route'"
comando.CommandText = strsql
r3 = comando.ExecuteReader
If bs.Count = 0 Then
bs.DataSource = r3
ComboBox1.Text = bs.Current(0)
tboxfunc.Text = bs.Current(1)
TextBox1.Text = bs.Current(2)
TextBox2.Text = bs.Current(3)
TextBox3.Text = bs.Current(5)
Atualizar.Enabled = True
Cancelar.Enabled = True
r3.Close()
Dim sStmt1 As String = "Select codtrab from trabalhos where estado = 'En Route' "
Dim cmd1 As New MySqlCommand(sStmt1, conexao)
Dim da1 As MySqlDataAdapter = New MySqlDataAdapter(cmd1)
Dim dt1 As New DataTable("trabalhos")
da1.Fill(dt1)
If dt1.Rows.Count > 0 Then
ComboBox1.DataSource = dt1
ComboBox1.DisplayMember = "codtrab"
Else
MsgBox("Não existem Funcionários", vbExclamation)
Me.Close()
Inicio.Show()
End If
da1.Dispose()
repor()
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
TextBox6.Enabled = False
tboxfunc.Enabled = False
btcancelar.Visible = False
Else
MsgBox("Não existem Operações En Route", vbExclamation)
Me.Close()
Inicio.Show()
End If
End Sub
You should put the connection string in a Using block, or dispose of it manually. This usually won't cause a crash, though.
If Inicio is the "calling" form, you can close this form without showing Inicio.
Try adding an "Exit Sub" after Me.Close.