Printing and storing receipt in VB.net - mysql

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.

Related

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

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.

i want to show mysql data using backgroudworker and dtagridview

I have problem when i'm trying to fetch data from mysql database
I always get the last record.
How can I get all records showing on the datagridview synchronously
Here is my code
Please can someone help me
Public Class Form1
Private RowCount As Integer = 0
Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ListBox1.Items.Add(e.UserState)
ProgressBar1.Value = e.ProgressPercentage
Label1.Text = "Processing row.. " + e.ProgressPercentage.ToString()
DataGridView1.Rows.Add(New Object() {f1, f2})
Me.ProgressBar1.Maximum = RowCount
End Sub
Dim ListText As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles go.Click
go.Enabled = False
cancel.Enabled = True
ListBox1.Items.Clear()
ProgressBar1.Value = 0
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
Me.Cursor = Cursors.WaitCursor
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
BackgroundWorker1.CancelAsync()
End Sub
Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
go.Enabled = True
cancel.Enabled = False
Me.Cursor = Cursors.Arrow
DataGridView1.Refresh()
DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.Rows.Count - 1)
End Sub
Dim f1 As String
Dim f2 As String
Dim ds As New DataSet
Dim command As MySqlCommand
Dim reader As MySqlDataReader
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
GetRecordCount()
Dim connetionString As String
Dim connection As MySqlConnection
Dim i As Integer = 0
Dim sql As String
connetionString = "Server=db4free.net;User Id=mouhcindinaoui;Password=$$$$$$;Database=mouhcindinaoui"
sql = "SELECT id,nom FROM dep02 "
connection = New MySqlConnection(connetionString)
Try
connection.Open()
command = New MySqlCommand(sql, connection)
reader = command.ExecuteReader()
For Value As Integer = 0 To rowcount
If reader.HasRows Then
Do While reader.Read()
f1 = reader.GetString("id")
f2 = reader.GetString("nom")
Loop
End If
ListText = String.Concat("Sequence #", Value)
BackgroundWorker1.ReportProgress(Value, ListText)
Thread.Sleep(10)
Next
reader.Close()
command.Dispose()
connection.Close()
Catch ex As Exception
' MsgBox("Can not open connection ! ")
End Try
End Sub
Private Sub GetRecordCount()
Dim connetionString As String
Dim connection As MySqlConnection
Dim command As MySqlCommand
Dim sql As String
connetionString = "Server=db4free.net;User Id=mouhcindinaoui;Password=dinaouimouhcin1991;Database=mouhcindinaoui"
sql = "Select count(*) from dep02"
connection = New MySqlConnection(connetionString)
command = New MySqlCommand(sql, connection)
connection.Open()
RowCount = CInt(command.ExecuteScalar())
End Sub
End Class
You are calling ReportProgress only at the end of the loop on the DataReader. Of course, at that point the two variables f1 and f2 used to set items in your grid contain the value of the last record read by the DataReader.
You need to move the call inside the DataReader loop
For Value As Integer = 0 To rowcount
If reader.HasRows Then
Do While reader.Read()
f1 = reader.GetString("id")
f2 = reader.GetString("nom")
ListText = String.Concat("Sequence #", Value)
BackgroundWorker1.ReportProgress(Value, ListText)
Loop
End If
Thread.Sleep(10)
Next
However it is totally useless the call to GetRecordCount and the external loop on the rowcount variable (and plain wrong). The while loop on the DataReader doesn't need it and your could remove all of it replacing everything with a simple increment of a local variable to keep the progressive of the current record inside the datareader loop
Dim recNum = 1
If reader.HasRows Then
Do While reader.Read()
f1 = reader.GetString("id")
f2 = reader.GetString("nom")
ListText = String.Concat("Sequence #", recNum)
BackgroundWorker1.ReportProgress(Value, ListText)
recNum = recNum + 1
Loop
End If

There is no row at position 0 in VB.NET

i have this 3 Forms in my project, on the second Form i have this edit button where i will edit the Listview item on the third Form, but when i select an item in the Listview and press edit, an error shows. It took me hours to find what's the problem, and i ended up here. Am i missing something?
this is my first form with listview in it.
Imports MySql.Data.MySqlClient
Public Class Form5
Public cd As Integer
Dim con As MySqlConnection
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New MySqlConnection
con.ConnectionString = "server=localhost;user id=root;database=db;password=root"
con.Open()
LoadPeople()
End Sub
Public Sub LoadPeople()
Dim sConnection As New MySqlConnection
sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
sConnection.Open()
Dim sqlQuery As String = "select * from candidate where cfname<>'Select a Candidate' AND candidacy='Filed'"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim TABLE As New DataTable
Dim i As Integer
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(TABLE)
End With
LvPeople.Items.Clear()
For i = 0 To TABLE.Rows.Count - 1
With LvPeople
.Items.Add(TABLE.Rows(i)("idn"))
With .Items(.Items.Count - 1).SubItems
.Add(AddFieldValue(TABLE.Rows(i), ("cpos")))
.Add(AddFieldValue(TABLE.Rows(i), ("cfname")))
.Add(AddFieldValue(TABLE.Rows(i), ("cmname")))
.Add(AddFieldValue(TABLE.Rows(i), ("clname")))
.Add(AddFieldValue(TABLE.Rows(i), ("cparty")))
End With
End With
Next
End Sub
Private Function AddFieldValue(ByVal row As DataRow, ByVal fieldName As String) As String
If Not DBNull.Value.Equals(row.Item(fieldName)) Then
Return CStr(row.Item(fieldName))
Else
Return Nothing
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form17.Show()
End Sub
End Class
my 2nd Form
Imports MySql.Data.MySqlClient
Public Class Form17
Public cd As Integer
Public sConnection As New MySqlConnection
Private Sub Form17_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
sConnection.Open()
End If
LoadPeople3()
End Sub
Public Sub LoadPeople3()
Dim sqlQuery As String = "select * from candidate where cfname<>'Select a Candidate'"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim TABLE As New DataTable
Dim i As Integer
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(TABLE)
End With
lvPeople3.Items.Clear()
For i = 0 To TABLE.Rows.Count - 1
With lvPeople3
.Items.Add(TABLE.Rows(i)("idn"))
With .Items(.Items.Count - 1).SubItems
.Add(AddFieldValue(TABLE.Rows(i), ("cfname")))
.Add(AddFieldValue(TABLE.Rows(i), ("cmname")))
.Add(AddFieldValue(TABLE.Rows(i), ("clname")))
.Add(AddFieldValue(TABLE.Rows(i), ("cyr")))
End With
End With
Next
End Sub
Private Function AddFieldValue(ByVal row As DataRow, ByVal fieldName As String) As String
If Not DBNull.Value.Equals(row.Item(fieldName)) Then
Return CStr(row.Item(fieldName))
Else
Return Nothing
End If
End Function
Private Sub lvPeople3_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvPeople3.MouseClick
cd = lvPeople3.SelectedItems(0).Selected
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If cd = Nothing Then
MsgBox("Please choose a record to edit.", MsgBoxStyle.Exclamation)
Else
Dim sqlQuery As String = "SELECT * FROM candidate WHERE cid = '" & lvPeople3.SelectedItems(0).Text & "'"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim sqlTabble As New DataTable
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.ExecuteNonQuery()
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(sqlTabble)
End With
Form23.cd = lvPeople3.SelectedItems(0).Text
Form23.cfname = sqlTabble.Rows(0)("cfname")
Form23.cfname = sqlTabble.Rows(0)("cmname")
Form23.cfname = sqlTabble.Rows(0)("clname")
Form23.ShowDialog()
cd = Nothing
End If
End Sub
End Class
and my third Form
Imports MySql.Data.MySqlClient
Public Class Form23
Friend cd As Integer
Friend cfname As String
Friend clname As String
Friend cmname As String
Public sConnection As New MySqlConnection
Private Sub Form23_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
sConnection.Open()
End If
TextBox2.Text = cfname
TextBox3.Text = clname
TextBox4.Text = cmname
End Sub
Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
Dim conn As New MySqlConnection
Dim cmd As New MySqlCommand
conn.ConnectionString = "server = localhost; user id = root; database = db; password = root"
cmd.Connection = conn
conn.Open()
If TextBox1.Text = "" Then
MessageBox.Show("Please complete the required fields..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Dim sqlQuery As String = "UPDATE candidate SET cpos='" & ComboBox1.Text & "', cparty='" & TextBox1.Text & "', candidacy='Filed' WHERE cid='" & cd & "'"
Dim sqlCommand As New MySqlCommand
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.ExecuteNonQuery()
End With
MsgBox("Record Updated")
Dispose()
Form5.Show()
Form17.Hide()
End If
Form5.LoadPeople()
Form17.LoadPeople3()
Me.Close()
End Sub
End Class
In Button1_Click you are accessing the first DataRow of a DataTable without checking if there is one:
Form23.cfname = sqlTabble.Rows(0)("cfname")
Form23.cfname = sqlTabble.Rows(0)("cmname")
Form23.cfname = sqlTabble.Rows(0)("clname")
You can check if there is one row by using the DataTable.Rows property:
If sqlTabble.Rows.Count > 0 Then
Form23.cfname = sqlTabble.Rows(0)("cfname")
Form23.cfname = sqlTabble.Rows(0)("cmname")
Form23.cfname = sqlTabble.Rows(0)("clname")
End If
Since you are trying to get all records according to the user's selection, i guess that you're using the wrong field. You are using the display-field but you are filtering by the ID-field:
"SELECT * FROM candidate WHERE cid = '" & lvPeople3.SelectedItems(0).Text & "'"
Maybe this is what you actually want:
"SELECT * FROM candidate WHERE cfname = '" & lvPeople3.SelectedItems(0).Text & "'"
Note that you should not use string concatenation but sql-parameters to prevent sql injection.

Close Form if SQL Query is empty

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