Unable to display records using MySQL in VB.NET - mysql

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.

Related

Printing and storing receipt in VB.net

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

vb.net combobox manual and load data from db

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

Mysql query showing empty result on vb.net

I have an vb.net application which use the mysql for database.
I am facing a problem which is the result of query is always empty.
Below are my codes.
Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click
Dim con As String = ConfigurationManager.ConnectionStrings("prcConnectionString").ConnectionString
Dim Sql As New MySqlConnection(con)
Dim reader2 As Object
Sql.Open()
Dim theQuery2 As String = "Select max(shipmentdate) from prc.tbsrparts t where Substring(partsn, 17, 11) =' " & tbPartSN.Text.Substring(16, 11) & "' " 'get latest shipment date from database
Dim command2 As New MySqlCommand(theQuery2, Sql)
reader2 = command2.ExecuteScalar
lbl90days.Text = reader2.ToString
End Sub
The result should be shown at label lbl90days.
But mine is always empty.
Can anyone guide me?
Thanks in advance.
Try this :
Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click
Dim con As String = ConfigurationManager.ConnectionStrings("prcConnectionString").ConnectionString
Dim Sql As New MySqlConnection(con)
Dim reader2 As Object
Sql.Open()
Dim theQuery2 As String = "Select max(shipmentdate) from prc.tbsrparts t where Substring(partsn, 16, 11) = #PartSN"
Dim command2 As New MySqlCommand(theQuery2, Sql)
command2.CommandType = CommandType.Text
command2.Parameters.AddWithValue("#PartSN", tbPartSN.Text.Substring(16, 11))
reader2 = command2.ExecuteScalar
lbl90days.Text = reader2.ToString
Sql.close()
End Sub
Able to solve by
Dim PartSN As String = tbPartSN.Text.Substring(16, 11)
And replace theQuery2 code with
Dim theQuery2 As String = "SELECT Max(shipmentdate) FROM prc.tbsrparts t WHERE Substring(partsn, 17, 11) = '" + PartSN.ToString + "'"

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.

DataGridview and MySQL Deleting rows with Checkbox

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