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.
if i remove ListBox in my form then it will run successfully..
MY CODE IS:
Try
Dim sqlconn As New OleDb.OleDbConnection
Dim sqlquery As New OleDb.OleDbCommand
Dim connString As String
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\MyLaptop\Documents\dtbsform.accdb"
sqlconn.ConnectionString = connString
sqlquery.Connection = sqlconn
sqlconn.Open()
sqlquery.Parameters.AddWithValue("#Name", nmTB.Text)
If maleRB.Checked Then
sqlquery.Parameters.AddWithValue("#gen", maleRB.Text)
Else
sqlquery.Parameters.AddWithValue("#gen", femaleRB.Text)
End If
For i As Integer = 0 To 3
If courseDDL.SelectedIndex = i Then
sqlquery.Parameters.AddWithValue("#cor", courseDDL.SelectedItem.ToString)
End If
Next
For i As Integer = 0 To langLB.Items.Count - 1
If langLB.SelectedIndex = i Then
sqlquery.Parameters.AddWithValue("#lang", langLB.SelectedItem.ToString)
MsgBox(langLB.SelectedItem.ToString)
End If
Next
For i As Integer = 1 To 2
If crktCB.Checked = True Then
sqlquery.Parameters.AddWithValue("#favs", crktCB.Text)
ElseIf ftblCB.Checked = True Then
sqlquery.Parameters.AddWithValue("#favs", ftblCB.Text)
End If
Next
For i As Integer = 0 To 2
If hobCBL.SelectedIndex = i Then
sqlquery.Parameters.AddWithValue("#hob", hobCBL.SelectedItem.ToString)
End If
Next
For i As Integer = 0 To 2
If clgRBL.SelectedIndex = i Then
sqlquery.Parameters.AddWithValue("#clg", clgRBL.SelectedItem.ToString)
End If
Next
sqlquery.CommandText = "INSERT INTO dtbs (Name,Gender,Course,Language,Sports,Hobbies,College) VALUES (#Name,#gen,#cor,#lang,#favs,#hob,#clg)"
sqlquery.ExecuteNonQuery()
sqlconn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try changing
sqlquery.Parameters.AddWithValue("#lang", langLB.SelectedItem.ToString)
to
sqlquery.Parameters.AddWithValue("#lang", langLB.SelectedItem.Text)
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
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.
I have a Datagridview and I want to delete a row in my MySQL database.
I have some code but I get an error, it says ID is null. My ID is a string which is the value of the ID where the columns are checked. My first column "column 0" is a checkbox column
Here is the code:
Feel free to ask specific questions if you don't understand what I am asking.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim RowsToDelete As New List(Of DataGridViewRow)
Try
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value = True Then
DeleteRow(row.Cells(1).Value)
RowsToDelete.Add(row)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
For Each rowtodelete In RowsToDelete
DataGridView1.Rows.Remove(rowtodelete)
next
End Sub
Private Sub DeleteRow(ByVal ID As Integer)
Dim MySQLCon As New MySqlConnection
Dim ConnectionString As String = "server=localhost;user id=root;password=;database=business elements"
MySQLCon.ConnectionString = ConnectionString
Dim CMD As MySqlCommand
MySQLCon.Open()
Try
CMD.Connection = MySQLCon
CMD.CommandText = "DELETE FROM `users` WHERE `ID` = " & ID
Catch ex As Exception
End Try
MySQLCon.Close()
MySQLCon.Dispose()
End Sub
Without seeing all of your code, something like this should work:
Dim sql as String
sql = "DELETE FROM `users` WHERE `ID` IN ("
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value = True Then
ID = row.Cells(1).Value
DeleteRow(row.Cells(1).Value)
RowsToDelete.Add(row)
sql += ID + ","
End If
Next
If sql.Left(sql.Length-1) = "," Then
CMD.CommandText = sql.Left(sql.Length-1) + ")"
CMD.Connection = MySQLCon
CMD.ExecuteNonQuery()
End If
Good luck.
Finally found the code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim RowsToDelete As New List(Of DataGridViewRow)
Try
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value = True Then
DeleteRow(row.Cells(1).Value)
RowsToDelete.Add(row)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
For Each rowtodelete In RowsToDelete
DataGridView1.Rows.Remove(rowtodelete)
Next
End Sub
Private Sub DeleteRow(ByVal ID As Integer)
Dim MySQLCon As New MySqlConnection
Dim ConnectionString As String = "server=localhost;user id=root;password=;database=business elements"
MySQLCon.ConnectionString = ConnectionString
Dim CMD As New MySqlCommand
CMD.CommandText = "DELETE FROM `users` WHERE `ID` = " & ID
MySQLCon.Open()
Try
CMD.Connection = MySQLCon
CMD.ExecuteNonQuery()
Catch ex As Exception
End Try
MySQLCon.Close()
MySQLCon.Dispose()
End Sub
For i As Integer = DataGridView2.Rows.Count - 1 To 0 Step -1
Dim c As Boolean
c = DataGridView2.Rows(i).Cells(0).Value
If c = True Then
=====================================================================================
Dim sql As String = "DELETE FROM Items WHERE ItemID=" & Int(DataGridView2(1, DataGridView2.Rows(i).Index).Value)
' ^^^^^^^^^^^^^^
'
Dim comm As New SqlCommand(sql, con)
con.Open()
Dim result As Integer = comm.ExecuteNonQuery
con.Close()
End If
Next