How to use System.IDisposable.Dispose? - vb.net

Im getting a warning in my object which transfers value from one form to another
The warning said Button1 must call System.IDisposable.Dispose on Object Form1 before references to it are of scope
This is my code on my button1:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Form1 As New Form1
Form1.a1 = TextBox1.Text
Form1.a2 = TextBox2.Text
Form1.a3 = TextBox3.Text
Form1.a4 = TextBox4.Text
Form1.a5 = TextBox5.Text
Form1.a6 = TextBox6.Text
Form1.a7 = TextBox7.Text
Form1.a8 = TextBox8.Text
Form1.a9 = TextBox9.Text
Form1.a10 = TextBox10.Text
Form1.Show()
Form1.SetPrice()
Me.Close()
End Sub
How to use System.IDisposable.Dispose?

It sounds like the listed code is not in Form1. Whatever form it's in is instantiating a Form1, but not disposing it. Just wrap Form1 in a Using statement:
Using Form1 As New Form1
Form1.a1 = TextBox1.Text
Form1.a2 = TextBox2.Text
Form1.a3 = TextBox3.Text
Form1.a4 = TextBox4.Text
Form1.a5 = TextBox5.Text
Form1.a6 = TextBox6.Text
Form1.a7 = TextBox7.Text
Form1.a8 = TextBox8.Text
Form1.a9 = TextBox9.Text
Form1.a10 = TextBox10.Text
Form1.Show()
Form1.SetPrice()
End Using
Me.Close()

Related

How I Could Fix "Invalid attempt to call MetaData when reader is closed"

I want to make a search system that when i press the button, my textbox and combobox would auto fill with data from database. and at the same time, while the combobox changed, it will be get the specific data that related from the combobox to fill at my textbox2.
sqlconn is SqlConnesction
sqldr is SqlDataRead
textbox1 is the place that i used for auto id
combobox1 for data relation to textbox2
combobox2 for data relation to textbox3
textbox4 for search bar
combobox3 for search category
koneksi.connect is my module to call connect, i use it to connect my form to database
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
koneksi.connect()
strsql = "SELECT disease_name FROM tb_disease where id_disease='" & ComboBox1.SelectedItem & "'"
sqlcmd = New SqlCommand(strsql, sqlconn)
sqldr = sqlcmd.ExecuteReader
sqldr.Read()
If sqldr.HasRows Then
TextBox3.Text = sqldr.Item("disease_name")
End If
sqldr.Close()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
koneksi.connect()
strsql = "SELECT symptom_name FROM tb_gejala where id_symptom='" & ComboBox2.SelectedItem & "'"
sqlcmd = New SqlCommand(strsql, sqlconn)
sqldr = sqlcmd.ExecuteReader
TextBox5.Text = ""
If sqldr.HasRows Then
TextBox5.Text = sqldr.Item("symptom_name")
End If
sqldr.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox3.Text = "Search Category:" Then
MsgBox("Choose The Search Method First")
ElseIf ComboBox3.Text = "ID RULE" Then
sqlcmd = New SqlCommand("SELECT * FROM tb_rule WHERE id_rule Like '%" & TextBox2.Text & "%'", sqlconn)
sqldr = sqlcmd.ExecuteReader()
sqldr.Read()
If sqldr.HasRows Then
TextBox1.Text = sqldr.Item("id_rule")
ComboBox1.Text = sqldr.Item("id_disease")
\\the metadata error is under here
ComboBox2.Text = sqldr.Item("id_symptom")
//the metadata error is up here
Else
MsgBox("No Data Match To The Following ID")
End If
sqldr.Close()
End If
End Sub
The way that i ever tired is to remove one of the "Combobox index selected",any of them. but one of them wont fill the textbox2 or textbox3
the second way i ever tried is to remove
If ComboBox3.Text = "Search Category:" Then
MsgBox("Choose The Search Method First")
ElseIf ComboBox3.Text = "ID RULE" Then
sqlcmd = New SqlCommand("SELECT * FROM tb_rule WHERE id_rule Like '%" & TextBox2.Text & "%'", sqlconn)
sqldr = sqlcmd.ExecuteReader()
sqldr.Read()
If sqldr.HasRows Then
TextBox1.Text = sqldr.Item("id_rule")
ComboBox1.Text = sqldr.Item("id_disease")
ComboBox2.Text = sqldr.Item("id_symptom")
Else
to this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox3.Text = "Search Category:" Then
MsgBox("Choose The Search Method First")
ElseIf ComboBox3.Text = "ID RULE" Then
sqlcmd = New SqlCommand("SELECT * FROM tb_rule WHERE id_rule Like '%" & TextBox2.Text & "%'", sqlconn)
sqldr = sqlcmd.ExecuteReader()
sqldr.Read()
If sqldr.HasRows Then
TextBox1.Text = sqldr.Item("id_rule")
ComboBox1.Text = sqldr.Item("id_disease")
Else
but one of the combobox would empty.
Both of the solution isn't work.
anything that related to IndexChange is the troublemaker, one called, the other index changed would error, no matter which one i call first.
Sorry for my bad english :)

how to search and update data into ms access

my problem is how to search data and update it in ms access and vb.net
my code:
Imports System.Data.OleDb
Public Class Form5
Dim provider As String
Dim dataFile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
Private Sub Form5_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "C:\Users\Admin\Desktop\project database\Database5.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
myConnection.Open()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
TextBox8.Clear()
TextBox9.Clear()
TextBox10.Clear()
TextBox11.Clear()
Dim str As String
str = "SELECT * FROM registration WHERE (Name = '" & TextBox1.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
TextBox2.Text = dr("No").ToString
TextBox3.Text = dr("Name").ToString
TextBox4.Text = dr("Age").ToString
TextBox5.Text = dr("Gender").ToString
TextBox6.Text = dr("Phone").ToString
TextBox7.Text = dr("Address").ToString
TextBox8.Text = dr("Desease").ToString
TextBox9.Text = dr("RoomNo").ToString
TextBox10.Text = dr("Building").ToString
TextBox11.Text = dr("RoomType").ToString
End While
myConnection.Close()
End Sub
End Class
the error I'm getting:
"No value given for one or more required parameters."}
Isn't the issue here that when you click button3 you clear the textbox1 where I assume you type the name you wish to search. So its throwing you an error that there is nothing in that field. And even if you type something after wards, and click the button again, it will clear it again before it starts searching. so basically, you're searching for nothing.

Add rows in datagridview in vb.net from SQL Server

I am facing a problem here, the below code have no error, but I would like to fetch data from SQL Server and add rows in datagridview rather than show in the textbox and then add to datagridview.
Private Sub TextBox2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
'searchcproduct
con = connection()
con.Open()
cmd.Connection = con
cmd.CommandText = " select * from product where plucode='" & TextBox2.Text & "' or barcode='" & TextBox2.Text & "'"
Dim dbreader As SqlDataReader = cmd.ExecuteReader
If dbreader.HasRows = False Then
TextBox2.Text = ""
TextBox2.Focus()
itemnotfound.Show()
PlayBackgroundSoundFile1()
ElseIf dbreader.HasRows = True Then
While dbreader.Read()
TextBox4.Text = dbreader.Item("plu_description")
TextBox5.Text = dbreader.Item("unitprice")
TextBox7.Text = dbreader.Item("plu_qty")
TextBox8.Focus()
End While
End If
End If
'TextBox2.Text = ""
End Sub
Private Sub Button20_Click(sender As System.Object, e As System.EventArgs) Handles Button20.Click
Dim rowNumber As Integer = DataGridView1.Rows.Add()
Try
DataGridView1.Rows.Item(rowNumber).Cells(0).Value = Me.TextBox2.Text ' i want to display sql rather than textbox
DataGridView1.Rows.Item(rowNumber).Cells(1).Value = Me.TextBox4.Text
DataGridView1.Rows.Item(rowNumber).Cells(2).Value = Me.TextBox5.Text
DataGridView1.Rows.Item(rowNumber).Cells(3).Value = Me.TextBox8.Text
DataGridView1.Rows.Item(rowNumber).Cells(4).Value = Val(Me.TextBox5.Text) * Val(Me.TextBox8.Text)
If Me.DataGridView1.Rows.Count > 0 Then
Me.Label2.Text = FormatNumber(totalsales(), 2).ToString()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "check")
End Try
TextBox2.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox8.Text = ""
TextBox7.Text = ""
End Sub
After calling ExecuteReader, create a DataTable and call its Load method, passing your data reader. You can then assign that DataTable to the DataSource property of grid to display all the data, e.g.
Dim myDataReader = myCommand.ExecuteReader()
Dim myDataTable As New DataTable
myDataTable.Load(myDataReader)
myDataGridView.DataSource = myDataTable

dynamic sql generation error occured when trying to update database

I followed your code and I have put it a msgbox to confirm and yes its returns the right row number. However an error occurred here's my code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cb As New OleDb.OleDbCommandBuilder(da) 'command builder to update the database
ds.Tables("inventory_table").Rows(marker).Item(0) = TextBox1.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(1) = TextBox2.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(2) = TextBox3.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(3) = TextBox4.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(4) = TextBox5.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(5) = TextBox6.Text 'update dataset
ds.Tables("inventory_table").Rows(marker).Item(6) = TextBox7.Text 'update dataset
da.Update(ds, "inventory_table") 'this is where the updating of database takes place
MsgBox("Data updated")
End Sub
Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
TextBox1.Text = DataGridView1.SelectedRows(0).Cells(0).Value
TextBox2.Text = DataGridView1.SelectedRows(0).Cells(1).Value
TextBox3.Text = DataGridView1.SelectedRows(0).Cells(2).Value
TextBox4.Text = DataGridView1.SelectedRows(0).Cells(3).Value
TextBox5.Text = DataGridView1.SelectedRows(0).Cells(4).Value
TextBox6.Text = DataGridView1.SelectedRows(0).Cells(5).Value
TextBox7.Text = DataGridView1.SelectedRows(0).Cells(6).Value
marker = Me.DataGridView1.SelectedRows(0).Index
MsgBox(marker)
End Sub
The error says:
dynamic sql generation for the updatecommand is not supporyed against
a selectcommand that does not return any key column information
and the line da.update(ds, "inventory_table") is highlighted.
In the double click event set a global variable:
_rowIndex = DataGridView1.SelectedRows(0).Index
Then
ds.Tables("inventory_table").Rows(_rowIndex).Item(1) = TextBox1.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(2) = TextBox2.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(3) = TextBox3.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(4) = TextBox4.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(5) = TextBox5.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(6) = TextBox6.Text
ds.Tables("inventory_table").Rows(_rowIndex).Item(7) = TextBox7.Text

My If Else Statement VB.Net's weird behavior

I have this program that when I typed the same text on the form and to the other form will add one (+1) to the label of my last form..There were no errors but when I wrote the correct answers on the textboxes it just gave me a number 1 instead of 10. Please help me guys. Thanks in advance :)
here is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm4 = New Form4()
frm1 = New Form1()
Dim Textbox1 As New TextBox
Dim Label3 As New Label
If Textbox1.Text = frm1.TextBox2.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox2.Text = frm1.TextBox4.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox3.Text = frm1.TextBox6.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox4.Text = frm1.TextBox8.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox5.Text = frm1.TextBox10.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox6.Text = frm1.TextBox12.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox7.Text = frm1.TextBox14.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox8.Text = frm1.TextBox16.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox9.Text = frm1.TextBox18.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox10.Text = frm1.TextBox20.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
End If
End If
End If
End If
End If
End If
End If
End If
End If
frm4.Show()
Else
frm4.Label3.Text = frm4.Label3.Text
frm4.Show()
End If
End Sub
Form 2 codes:
Public Class Form2
Private frm1 As Form1 ' remove the new here because it creates a new instance of Form1'
Private frm3 As Form3
Public lbl As New Label ' not needed?'
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lbl)
End Sub
Public Sub New(ByVal callerInstance As Form1)
' Call required if you add your constructor manually
InitializeComponent()
' save the instance of the Me variable passed to this constructor
frm1 = callerInstance
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Dim Label21 As Label = New Label ' this label is not needed'
' Dim Textbox1 As TextBox = New TextBox ' this textbox is not needed'
' Create a new instance of Form3, do not use the automatic Form3 instance
' automatically created by VB.NET
frm3 = New Form3()
' now you are referring to the caller instance of Form1 '
' where there is the textbox filled with your text '
frm3.Label21.Text = frm1.TextBox1.Text
frm3.Label22.Text = frm1.TextBox3.Text
frm3.Label23.Text = frm1.TextBox5.Text
frm3.Label24.Text = frm1.TextBox7.Text
frm3.Label25.Text = frm1.TextBox9.Text
frm3.Label26.Text = frm1.TextBox11.Text
frm3.Label27.Text = frm1.TextBox13.Text
frm3.Label28.Text = frm1.TextBox15.Text
frm3.Label29.Text = frm1.TextBox17.Text
frm3.Label30.Text = frm1.TextBox19.Text
frm3.Show()
End Sub
End Class
Form1 Codes:
Public Class Form1
Private frm2 As Form2
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If frm2 Is Nothing Then
frm2 = New Form2(Me)
AddHandler frm2.FormClosed, AddressOf Me.Form2HasBeenClosed
Dim Label21 As Label = New Label
frm2.Label21.Text = TextBox1.Text
frm2.Label21.ForeColor = Color.Black
Dim Label22 As Label = New Label
frm2.Label22.Text = TextBox2.Text
frm2.Label22.ForeColor = Color.Black
Dim Label23 As Label = New Label
frm2.Label23.Text = TextBox3.Text
frm2.Label23.ForeColor = Color.Black
Dim Label24 As Label = New Label
frm2.Label24.Text = TextBox4.Text
frm2.Label24.ForeColor = Color.Black
Dim Label25 As Label = New Label
frm2.Label25.Text = TextBox5.Text
frm2.Label25.ForeColor = Color.Black
Dim Label26 As Label = New Label
frm2.Label26.Text = TextBox6.Text
frm2.Label26.ForeColor = Color.Black
Dim Label27 As Label = New Label
frm2.Label27.Text = TextBox7.Text
frm2.Label27.ForeColor = Color.Black
Dim Label28 As Label = New Label
frm2.Label28.Text = TextBox8.Text
frm2.Label28.ForeColor = Color.Black
Dim Label29 As Label = New Label
frm2.Label29.Text = TextBox9.Text
frm2.Label29.ForeColor = Color.Black
Dim Label30 As Label = New Label
frm2.Label30.Text = TextBox10.Text
frm2.Label30.ForeColor = Color.Black
Dim Label31 As Label = New Label
frm2.Label31.Text = TextBox11.Text
frm2.Label31.ForeColor = Color.Black
Dim Label32 As Label = New Label
frm2.Label32.Text = TextBox12.Text
frm2.Label32.ForeColor = Color.Black
Dim Label33 As Label = New Label
frm2.Label33.Text = TextBox13.Text
frm2.Label33.ForeColor = Color.Black
Dim Label34 As Label = New Label
frm2.Label34.Text = TextBox14.Text
frm2.Label34.ForeColor = Color.Black
Dim Label35 As Label = New Label
frm2.Label35.Text = TextBox15.Text
frm2.Label35.ForeColor = Color.Black
Dim Label36 As Label = New Label
frm2.Label36.Text = TextBox16.Text
frm2.Label36.ForeColor = Color.Black
Dim Label37 As Label = New Label
frm2.Label37.Text = TextBox17.Text
frm2.Label37.ForeColor = Color.Black
Dim Label38 As Label = New Label
frm2.Label38.Text = TextBox18.Text
frm2.Label38.ForeColor = Color.Black
Dim Label39 As Label = New Label
frm2.Label39.Text = TextBox19.Text
frm2.Label39.ForeColor = Color.Black
Dim Label40 As Label = New Label
frm2.Label40.Text = TextBox20.Text
frm2.Label40.ForeColor = Color.Black
End If
If frm2 IsNot Nothing Then
frm2.Show(Me) 'Show Second Form
Me.Hide()
End If
End Sub
Sub Form2HasBeenClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
frm2 = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Form 3 Code:(instance calling to form1)
Public Class Form3
Private frm3 As Form3
Private frm1 As Form1
Private frm4 As Form4
Public Sub New1(ByVal callerInstance As Form1)
' Call required if you add your constructor manually
InitializeComponent()
' save the instance of the Me variable passed to this constructor
frm1 = callerInstance
End Sub
End Class
If you want show in label number of right answers then
You need compare all user answers with right answers
And count times when answers are same:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm4 = New Form4()
frm1 = New Form1()
Dim Textbox1 As New TextBox
Dim Label3 As New Label
Dim sameQnt As Int32 = 0
If Textbox1.Text = frm1.TextBox2.Text Then sameQnt += 1
If Textbox2.Text = frm1.TextBox4.Text Then sameQnt += 1
If Textbox3.Text = frm1.TextBox6.Text Then sameQnt += 1
If Textbox4.Text = frm1.TextBox8.Text Then sameQnt += 1
If Textbox5.Text = frm1.TextBox10.Text Then sameQnt += 1
If Textbox6.Text = frm1.TextBox12.Text Then sameQnt += 1
If Textbox7.Text = frm1.TextBox14.Text Then sameQnt += 1
If Textbox8.Text = frm1.TextBox16.Text Then sameQnt += 1
If Textbox9.Text = frm1.TextBox18.Text Then sameQnt += 1
If Textbox10.Text = frm1.TextBox20.Text Then sameQnt += 1
'Print reslut
frm4.Label3.Text = sameQnt.ToString()
frm4.Show()
End Sub
Here is my new answer
Public Class MainForm '<----Your main form class name
Dim frm4 = New Form4()'<----declare it here
Dim frm1 = New Form1()'<----declare it here
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Textbox1 As New TextBox
Dim Label3 As New Label
Dim i As Integer = 0
If Textbox1.Text = frm1.TextBox2.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox2.Text = frm1.TextBox4.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox3.Text = frm1.TextBox6.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox4.Text = frm1.TextBox8.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox5.Text = frm1.TextBox10.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox6.Text = frm1.TextBox12.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox7.Text = frm1.TextBox14.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox8.Text = frm1.TextBox16.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox9.Text = frm1.TextBox18.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox10.Text = frm1.TextBox20.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
frm4.Label3.Text = i.ToString()
frm4.Show()
End Sub
End Class