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)
Related
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 have 4 tables on my database mysql(Xampp),
tbl_classic,
tbl_signature,
tbl_fusion,
tbl_blends, each tables have 5 columns ID, FlavorItems, FlavorQuantity, MediumPrice, LargePrice. How to search the value of FlavorItems in all tables?
Dim sql1 As String = ("Select * From tbl_classicmilktea where FlavorItems = #FlavorItems")
cmd = New MySqlCommand(sql1, cn)
cmd.Parameters.Add("#FlavorItems", MySqlDbType.VarChar).Value = txtSearch.Text
Dim adapter As New MySqlDataAdapter(cmd)
cn.Open()
Dim table As New DataTable()
Try
adapter.Fill(table)
If table.Rows.Count() > 0 Then
' Return only 1 row.
txtNo.Text = table.Rows(0)(0).ToString()
txtFlavorItems.Text = table.Rows(0)(1).ToString()
dudQuantity.Text = table.Rows(0)(2).ToString()
txtMedium.Text = table.Rows(0)(3).ToString()
txtLarge.Text = table.Rows(0)(3).ToString()
End If
Catch Ex As Exception
MessageBox.Show("NO Data Found")
End Try
cn.Close()
End Sub
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
I have queried my MySql database and data is stored in a dataset named Search Events(I have checked this and it works)
However when i try to pass this data into a datagridview and then ultimately my array it doesn't seem to work :( The problem seems to be that no data is being passed from Search Events into Table
Any ideas??
Dim Table As New DataGridView
Table.DataSource = SearchEvents.Tables("Event ID")
Dim EventID(Table.Rows.Count - 1) As String
For i = 0 To 12
EventID(i) = Table.Rows(0).Cells(i).Value
Next
'Try the following function
Public Sub getuserlist()
Dim con As New MySqlConnection(ConnectionString)
Try
If con.State <> ConnectionState.Open Then
con.Open()
End If
Dim selecttaxinv = "SELECT * FROM user_tb WHERE user_status = 'Active' ORDER BY date_registered DESC"
Dim myCommand As New MySqlCommand(selecttaxinv, con)
Dim dr As MySqlDataReader
dr = myCommand.ExecuteReader()
yourdatagridview.Rows.Clear()
Dim b = 1
While dr.Read()
Dim fullname As String = dr("fname") & " " & dr("mname") & " " & dr("lname")
Dim row As String() = New String() {dr("user_id"), b, fullname, dr("fname"), dr("date_registered")}
yourdatagridview.Rows.Add(row) 'print list
b += 1
End While
dr.Close()
If con.State = ConnectionState.Open Then
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
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