that's my first time here, I'm going mad because of this problem I have:
I'm developing a windows form application with VB.NET using multiple types of connections(mysql,odbc and SQL server) everything works fine until I get into MySQL..
MySQL server is a physical windows 7 pc, I connect to it through IPSEC VPN TUNNEL.
I need to perform 2 MySQL connections every x seconds, if I get some type of result after the first connection then I'll open the second one, and so on every x seconds(that's all wrote in my timer.tick event handler).
The problem is that quite often some connections on MySQL server keep staying alive(ESTABLISHED) on MySQL server and I can't find out why... code looks fine, there are both open and close methods declared at the right time, I've also tried Dispose,ClearPool and ClearAllPools methods but I keep having those connections up until I close my program or it reaches connection limit.
Here's the code:
Class connection:
Public Sub connMySQL()
Dim connstring As String
Try
If stabilimento = "1PR" Then
If cesoia = "" Then
connstring = "server=192.168.123.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
Else
connstring = "server=192.168.123.253;userid=xx;password=xx;database=xx;Connect Timeout=30"
End If
End If
If stabilimento = "2PR" Then
If cesoia = "" Then
connstring = "server=192.168.1.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
Else
connstring = "server=192.168.123.253;userid=root;password=xx;database=xx;Connect Timeout=30"
End If
End If
conMySql = New MySqlConnection(connstring)
If conMySql.State = ConnectionState.Closed Then
conMySql.Open()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Class where the iteration is performed:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
connMySQL()
comm = New MySqlCommand("SELECT count_1,count_2,start_stop,data_ora,id FROM plc_contatori where plc_nome='" + plc_nome + "' and data_ora > '" + data_ieri.ToString("yyyy/MM/dd") + "' order by data_ora desc limit 1", conMySql)
dr = comm.ExecuteReader()
While (dr.Read())
count_1(0) = dr.GetValue(0)
start_stop(0) = dr.GetValue(2)
data_ora(0) = dr.GetValue(3)
If id <> dr.GetValue(4) And count_2(0) <> dr.GetValue(1) Then
id = dr.GetValue(4)
count_2(0) = dr.GetValue(1)
Else
Exit Sub
End If
End While
dr.Close()
dr.Dispose()
conMySql.Close()
conMySql.Dispose()
conMySql.ClearPool(conMySql)
conMySql.ClearAllPools()
If Not conMySql Is Nothing Then conMySql = Nothing
comm.Dispose()
If start_stop(0) = 1 Then
Exit Sub
End If
Dim dum_count_2 As Integer = count_2(0) - 1
connMySQL()
comm = New MySqlCommand("select count_1,count_2,start_stop,data_ora from plc_contatori where plc_nome='" + plc_nome + "' and data_ora > '" + data_ieri.ToString("yyyy/MM/dd") + "' AND count_2=" + dum_count_2.ToString + " ORDER BY data_ora desc limit 1", conMySql)
dr = comm.ExecuteReader()
While (dr.Read())
count_1(1) = dr.GetValue(0)
count_2(1) = dr.GetValue(1)
start_stop(1) = dr.GetValue(2)
data_ora(1) = dr.GetValue(3)
End While
dr.Close()
dr.Dispose()
conMySql.Close()
conMySql.Dispose()
conMySql.ClearPool(conMySql)
conMySql.ClearAllPools()
If Not conMySql Is Nothing Then conMySql = Nothing
comm.Dispose()
If count_1(0) = count_1(1) And start_stop(1) <> 1 And count_2(0) <> count_2(1) Then
'sub that reads some values from an odbc connection
CheckFermo()
End If
End Sub
NOTE that variables that I have not declared in this portion of code are declared in the public class of the form.
I'm wondering what could be wrong... maybe the 2nd connection is being established before the 1st one gets closed by the server?
I changed the connMySQL method to a function that returns the connection string. I have declared several variables so the code makes sense. I made several assumptions about datatypes. You may have to change this back to String and VarChar if these values are actually stored as strings. (I hope they are not)
You could use a single connection but all the assignments and comparisons would occur with an open connection.
Private stabilimento As String
Private cesoia As String
Public Function connMySQL() As String
Dim connstring As String
Select Case True
Case stabilimento = "1PR" And cesoia = ""
connstring = "server=192.168.123.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
Case stabilimento = "2PR" And cesoia = ""
connstring = "server=192.168.1.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
Case Else
connstring = "server=192.168.123.253;userid=root;password=xx;database=xx;Connect Timeout=30"
End Select
Return connstring
End Function
Private count_1(10) As Integer
Private count_2(10) As Integer
Private start_stop(10) As Integer
Private data_ora(10) As Date
Private id As Integer
Private plc_nome As String
Private data_ieri As Date
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Using dt As New DataTable
Using cn As New MySqlConnection(connMySQL),
comm = New MySqlCommand("SELECT count_1,count_2,start_stop,data_ora,id
FROM plc_contatori
where plc_nome= #plcNome and data_ora > #dataOra
order by data_ora desc
limit 1", cn)
comm.Parameters.Add("plcNome", MySqlDbType.VarChar).Value = plc_nome
comm.Parameters.Add("dataOra", MySqlDbType.Date).Value = data_ieri '.ToString("yyyy/MM/dd")
cn.Open()
Using dr = comm.ExecuteReader
dt.Load(dr)
End Using 'closes and disposes reader
End Using 'closes and dispose connection and command
count_1(0) = CInt(dt(0)(0))
start_stop(0) = CInt(dt(0)(2))
data_ora(0) = CDate(dt(0)(3))
If id <> CInt(dt(0)(4)) AndAlso count_2(0) <> CInt(dt(0)(1)) Then
id = CInt(dt(0)(4))
count_2(0) = CInt(dt(0)(1))
Else
Exit Sub
End If
End Using 'disposes DataTable
If start_stop(0) = 1 Then
Exit Sub
End If
Dim dum_count_2 As Integer = count_2(0) - 1
Using dt As New DataTable
Using cn As New MySqlConnection(connMySQL),
comm As New MySqlCommand("select count_1,count_2,start_stop,data_ora
from plc_contatori
where plc_nome= #plcNome
and data_ora > #dataOra
AND count_2= #count2
ORDER BY data_ora desc
limit 1", cn)
comm.Parameters.Add("#plcNome", MySqlDbType.VarChar).Value = plc_nome
comm.Parameters.Add("#dataOra", MySqlDbType.Date).Value = data_ieri '.ToString("yyyy/MM/dd")
comm.Parameters.Add("#count2", MySqlDbType.Int32).Value = dum_count_2 '.ToString
cn.Open()
Using dr = comm.ExecuteReader()
dt.Load(dr)
End Using
End Using
count_1(1) = CInt(dt(0)(0))
count_2(1) = CInt(dt(0)(1))
start_stop(1) = CInt(dt(0)(2))
data_ora(1) = CDate(dt(0)(3))
End Using
If count_1(0) = count_1(1) AndAlso start_stop(1) <> 1 AndAlso count_2(0) <> count_2(1) Then
'sub that reads some values from an odbc connection
CheckFermo()
End If
End Sub
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.
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
Im trying to pull data from 2 different tables in one button click event. I've checked over everything and doesn't seem to be any typo's or anything but keep getting this error.
Below is my code for the button click event
Protected Sub btnFindRepair_Click(sender As Object, e As EventArgs) Handles btnFindRepair.Click
Dim connection As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\ITrepair.mdf;Integrated Security=True")
Dim command As New SqlCommand("SELECT * from Repair; SELECT * FROM Customer WHERE Tracking_Number = #Tracking_Number", connection)
command.Parameters.Add("#Tracking_Number", SqlDbType.Int).Value = txtTrackingNumber.Text
Dim adapter As New SqlDataAdapter(command)
Dim ds As System.Data.DataSet
Dim table As New DataTable()
adapter.Fill(table)
'Repair Details
DDLBookedInBy.SelectedItem.Text = ""
DDLDeviceType.SelectedItem.Text = ""
txtBookedInDate.Text = ""
txtDeviceName.Text = ""
DDLAccessories.SelectedItem.Text = ""
txtDevicePassword.Text = ""
DDLRepairType.Text = ""
txtTechnical.Text = ""
txtCompletedNotes.Text = ""
DDLRepairStatus.Text = ""
'Customer Details
txtFname.Text = ""
txtLname.Text = ""
txtContactNum.Text = ""
txtAltContactNum.Text = ""
txtAddress.Text = ""
If table.Rows.Count() > 0 Then
' return only 1 row
DDLBookedInBy.SelectedItem.Text = ds.tables(0).Rows(0)(2).ToString()
DDLDeviceType.SelectedItem.Text = ds.tables(0).Rows(0)(3).ToString()
txtBookedInDate.Text = ds.tables(0).Rows(0)(4).ToString()
txtDeviceName.Text = ds.tables(0).Rows(0)(5).ToString()
DDLAccessories.SelectedItem.Text = ds.tables(0).Rows(0)(6).ToString()
txtDevicePassword.Text = ds.tables(0).Rows(0)(7).ToString()
DDLRepairType.Text = ds.tables(0).Rows(0)(8).ToString()
txtTechnical.Text = ds.tables(0).Rows(0)(9).ToString()
txtCompletedNotes.Text = ds.tables(0).Rows(0)(10).ToString()
txtFname.Text = ds.tables(1).Rows(1)(4).ToString()
txtLname.Text = table.Rows(1)(5).ToString()
txtContactNum.Text = table.Rows(1)(6).ToString()
txtAltContactNum.Text = table.Rows(1)(7).ToString()
txtAddress.Text = table.Rows(1)(8).ToString()
Else
MsgBox("NO DATA found")
End If
End Sub
Replace all occurences of ds.tables(0) with table. You haven't initialized the DataSet ds but you don't need it anyway because you fill the DataTable tbl with adapter.Fill(table).
For example:
If table.Rows.Count > 0 Then
DDLBookedInBy.SelectedItem.Text = table.Rows(0)(2).ToString()
' .... '
If you want to fill the DataSet use:
Dim ds As System.Data.DataSet
Dim table As New DataTable()
ds = New DataSet()
adapter.Fill(ds)
If table.Rows.Count > 0 Then
DDLBookedInBy.SelectedItem.Text = ds.Tables(0).Rows(0)(2).ToString()
' .... '
txtFname.Text = ds.Tables(1).Rows(1)(4).ToString()
' ... '
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