Different output from same coding vb.net - visual-studio

im new here and want asking one question.
i have form with button that when clicked will fill database, but when im using keydown event the result is different.
here is the result, red box from click button and the green one from keydown event.
enter image description here
here is my code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Masukan kode dan nama sub kelompok terlebih dahulu")
Else
For baris As Integer = 0 To DataGridView1.Rows.Count - 2
Dim inputsub As String = "insert into mastersubkelompok values('" & DataGridView1.Rows(baris).Cells(0).Value & "','" & DataGridView1.Rows(baris).Cells(1).Value & "','" & DataGridView1.Rows(baris).Cells(2).Value & "','" & TextBox1.Text & "')"
CMD = New OleDbCommand(inputsub, CONN)
CMD.ExecuteNonQuery()
MsgBox("Data berhasil di input")
Call kondisiawal()
Next
End If
End Sub
Private Sub SubKelompok_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown, DataGridView1.KeyDown
If e.KeyCode = Keys.F2 Then
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Masukan kode dan nama sub kelompok terlebih dahulu")
Else
For baris As Integer = 0 To DataGridView1.Rows.Count - 2
Dim inputsub As String = "insert into mastersubkelompok values('" & DataGridView1.Rows(baris).Cells(0).Value & "','" & DataGridView1.Rows(baris).Cells(1).Value & "','" & DataGridView1.Rows(baris).Cells(2).Value & "','" & TextBox1.Text & "')"
CMD = New OleDbCommand(inputsub, CONN)
CMD.ExecuteNonQuery()
MsgBox("Data berhasil di input")
Call kondisiawal()
Next
End If
End If
End Sub

Related

How can I convert varchar to numeric in visual studio?

Please help me,Im trying to do a fixed asset program in visual basic studio using sql server, Im stack in this.
I checked if my sql table data types match with the visual studio data types.
Public Class Equipos_de_Computacion
Dim sql As String = ""
Private Sub BtnInsertar_Click(sender As Object, e As EventArgs) Handles btnInsertar.Click
If (Me.txtCodigoInterno.Text = "") Then
MsgBox("El campo idententificacion no puede estar vacio", MsgBoxStyle.Critical, "Atencion")
Me.txtCodigoInterno.Select()
Else
Dim CodigoInterno As Integer
Dim NumerodeSerie As Integer
Dim NumerodeFactura As Integer
Dim FechadeCompra As Date
Dim Precio As Decimal
Dim Acargode As String = ""
Dim Estado As String = ""
Dim Depreciacion As Decimal
Dim Caracteristicas As String = ""
CodigoInterno = Me.txtCodigoInterno.Text
NumerodeSerie = Me.txtNumerodeSerie.Text
NumerodeFactura = Me.txtNumerodeFactura.Text
FechadeCompra = Me.DateTimePicker1.MinDate
Precio = CDec(txtPrecio.Text)
Acargode = Me.txtACargode.Text
Estado = Me.txtEstado.Text
Depreciacion = CDec(txtDepreciacion.Text)
Caracteristicas = Me.txtCaracteristicas.Text
cmd.CommandType = CommandType.Text
cmd.Connection = conn
sql = "INSERT INTO [Equipos de Computacion] (CodigoInterno, NumerodeSerie, NumerodeFactura, FechadeCompra, Precio, Acargode, Estado, Caracteristicas, Depreciacion) "
sql += "Values('" & CodigoInterno & "','" & NumerodeSerie & "','" & NumerodeFactura & "','" & FechadeCompra & "','" & CDec(Precio) & "','" & Acargode & "','" & Estado & "','" & Caracteristicas & "','" & CDec(Depreciacion) & ")"
MsgBox(sql)
cmd.CommandText = sql
Try
cmd.ExecuteNonQuery()
MsgBox("Registro insertado correctamente")
Catch ex As Exception
If ex.ToString.Contains("duplicate") Then
MsgBox("El registro ya existe en la base de datos")
Else
MsgBox(ex.ToString)
End If
End Try
End If
End Sub
it sends me converting varchar to numeric error.
Iam only able to insert integer numbers.With Cint
`Imports System.Data.SqlClient
Public Class Equipos_de_Computacion
Dim sql As String = ""
Private Sub BtnInsertar_Click(sender As Object, e As EventArgs) Handles btnInsertar.Click
If (Me.txtCodigoInterno.Text = "") Then
MsgBox("El campo idententificacion no puede estar vacio", MsgBoxStyle.Critical, "Atencion")
Me.txtCodigoInterno.Select()
Else
Dim CodigoInterno As Integer
Dim NumerodeSerie As Integer
Dim NumerodeFactura As Integer
Dim FechadeCompra As Date
Dim Precio As Decimal
Dim Acargode As String = ""
Dim Estado As String = ""
Dim Depreciacion As Decimal
Dim Caracteristicas As String = ""
CodigoInterno = CInt(Me.txtCodigoInterno.Text)
NumerodeSerie = CInt(Me.txtNumerodeSerie.Text)
NumerodeFactura = CInt(Me.txtNumerodeFactura.Text)
FechadeCompra = CDate(Me.DateTimePicker1.Text)
Precio = Me.nudPrecio.Value
Acargode = Me.txtACargode.Text
Estado = Me.txtEstado.Text
Depreciacion = Me.NudDepreciacion.Value
Caracteristicas = Me.txtCaracteristicas.Text
cmd.CommandType = CommandType.Text
cmd.Connection = conn
sql = "INSERT INTO [Equipos de Computacion] (CodigoInterno, NumerodeSerie, NumerodeFactura, FechadeCompra, Precio, Acargode, Estado, Caracteristicas, Depreciacion)"
sql += "Values( '" & CodigoInterno & "' , '" & NumerodeSerie & "' , '" & NumerodeFactura & "' ,'" & FechadeCompra & "', '" & Precio & "' ,'" & Acargode & "','" & Estado & "','" & Caracteristicas & "','" & Depreciacion & "' )"
MsgBox(sql)
cmd.CommandText = sql
Try
cmd.ExecuteNonQuery()
MsgBox("Registro insertado correctamente")
Catch ex As Exception
If ex.ToString.Contains("duplicate") Then
MsgBox("El registro ya existe en la base de datos")
Else
MsgBox(ex.ToString)
End If
End Try
End If
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEquiposdeComputacion.CellContentClick
End Sub
Private Sub BtnMostrar_Click(sender As Object, e As EventArgs) Handles btnMostrar.Click
Dim DS As New DataSet
Dim DA As New SqlDataAdapter("Select * from [Equipos de Computacion]", conn)
DA.Fill(DS)
dgvEquiposdeComputacion.DataSource = DS.Tables(0)
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
End Sub
Private Sub BtnTotalDepreciacion_Click(sender As Object, e As EventArgs) Handles btnTotalDepreciacion.Click
NudDepreciacion.Value = CDec(FormatNumber(nudPrecio.Value * 0.25, 2))
End Sub
Private Sub TxtPrecio_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub txtPrecio_KeyPress(sender As Object, e As KeyPressEventArgs) Handles nudPrecio.KeyPress
nudPrecio.DecimalPlaces = 2
End Sub
Private Sub txtPrecio_LostFocus(sender As Object, e As EventArgs)
Dim VarMonedaDolares As Double
VarMonedaDolares = nudPrecio.Value
nudPrecio.Value = CDec(FormatCurrency(VarMonedaDolares, 2))
End Sub
End Class`

i got no error, but : "OleDbException was unhandled Syntax error (missing operator) in query expression" how to solve this?

I am new in creating an application using Visual Stuido 2010 C# and Microsft Access 2007. I am planning to create an application where the user can add data to the database(MS-Access). But I got an error stating that "Syntax Error (missing operator) In Query Expression". I really can't find what's the problem with my code. im using panel for page 2 of my form.
i got the problem from Sub Button1 "rd = cmd.ExecuteReader"
This is my code in adding data to the database:
Imports System.Data.OleDb
Public Class Form1
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim str As String
Dim cmd As OleDbCommand
Dim rd As OleDbDataReader
Dim tanya As String
Sub koneksi()
str = "provider=microsoft.jet.oledb.4.0;data source=Database1.mdb"
conn = New OleDbConnection(str)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
End Sub
Sub tampilkan()
da = New OleDbDataAdapter("select * from tb_bpl", conn)
ds = New DataSet
da.Fill(ds, "tb_bpl")
DataGridView1.DataSource = ds.Tables("tb_bpl")
DataGridView1.Columns(1).Width = 250
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call koneksi()
Call tampilkan()
End Sub
Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Close()
End Sub
Private Sub bersih()
TextBox1.Clear()
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
TextBox11.Text = ""
TextBox12.Text = ""
TextBox13.Text = ""
TextBox14.Text = ""
TextBox15.Text = ""
TextBox16.Text = ""
TextBox17.Text = ""
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
cmd = New OleDbCommand("select * tb_bpl where periode='" + TextBox1.Text + "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If Not rd.HasRows Then 'jika data/baris tidak ditemukan
Dim simpan As String = "insert into tb_bpl(periode, reviewer, pemeriksa, paraf1, paraf2, y1, kkp1, ket1, y2, kkp2, ket2, y3, kkp3, ket3, y4, kkp4, ket4) values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox11.Text & "','" & TextBox12.Text & "','" & TextBox13.Text & "','" & TextBox14.Text & "','" & TextBox15.Text & "','" & TextBox16.Text & "','" & TextBox17.Text & "')"
cmd = New OleDbCommand(simpan, conn)
cmd.ExecuteNonQuery()
MsgBox("Data Baru Tersimpan", vbInformation, "Pemberitahuan!!!")
Call bersih()
Else
MsgBox("Data Sudah Ada", vbInformation, "Maaf!!!")
End If
tampilkan()
End Sub
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
cmd = New OleDbCommand("select * from tb_bpl where periode='" & TextBox1.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows Then
Dim sqledit As String = "update tb_bpl set reviewer='" & TextBox2.Text & "',pemeriksa='" & TextBox3.Text & "',paraf1='" & TextBox4.Text & "',paraf2='" & TextBox5.Text & "',y1='" & TextBox6.Text & "' ,kkp1='" & TextBox7.Text & "' ,ket1='" & TextBox8.Text & "',y2='" & TextBox9.Text & "',kkp2='" & TextBox10.Text & "',ket2='" & TextBox11.Text & "',y3='" & TextBox12.Text & "',kkp3='" & TextBox13.Text & "',ket3='" & TextBox14.Text & "',y4='" & TextBox15.Text & "',kkp4='" & TextBox16.Text & "',ket4='" & TextBox17.Text & "' where periode='" & TextBox1.Text & "'"
cmd = New OleDbCommand(sqledit, conn)
cmd.ExecuteNonQuery()
End If
MsgBox("Data Terubah", vbOKOnly, "Sukses")
Call bersih()
Call tampilkan()
End Sub
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
cmd = New OleDbCommand("select * from tb_bpl where periode='" & TextBox1.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows Then
tanya = MsgBox("Anda Yakin Akan Menghapus Data?", vbYesNo, "Perhatian")
If tanya = vbYes Then
Dim sqlhapus As String = "delete * from tb_bpl where periode='" & TextBox1.Text & "'"
cmd = New OleDbCommand(sqlhapus, conn)
cmd.ExecuteNonQuery()
MsgBox("Data Terhapus", vbOKOnly, "Sukses")
Call bersih()
End If
End If
Call tampilkan()
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
cmd = New OleDbCommand("select * from tb_bpl where periode='" & TextBox1.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows Then
TextBox2.Text = rd.Item("reviewer")
TextBox3.Text = rd.Item("pemeriksa")
TextBox4.Text = rd.Item("paraf1")
TextBox5.Text = rd.Item("paraf2")
TextBox6.Text = rd.Item("y1")
TextBox7.Text = rd.Item("kkp1")
TextBox8.Text = rd.Item("ket1")
TextBox9.Text = rd.Item("y2")
TextBox10.Text = rd.Item("kkp2")
TextBox11.Text = rd.Item("ket2")
TextBox12.Text = rd.Item("y3")
TextBox13.Text = rd.Item("kkp3")
TextBox14.Text = rd.Item("ket3")
TextBox15.Text = rd.Item("y4")
TextBox16.Text = rd.Item("kkp4")
TextBox17.Text = rd.Item("ket4")
End If
End Sub
End Class

My code throws an error that the connection associated with data reader is not closed

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Con = New MySqlConnection
Con.ConnectionString = "server = localhost; user = root; database= employee"
Try
Con.Open()
If TextBox1.Text = "" And TextBox2.Text = "" And TextBox3.Text = "" Then
MsgBox("Please fill-up all fields!", MsgBoxStyle.Exclamation, "Add New Customer!")
Else
Dim theQuery As String = "SELECT * FROM accounts WHERE EmpNo=#EmpNo "
Dim cmd1 As MySqlCommand = New MySqlCommand(theQuery, Con)
cmd1.Parameters.AddWithValue("#EmpNo", TextBox1.Text)
Using reader As MySqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
' User already exists
MsgBox("User Already Exist!", MsgBoxStyle.Exclamation, "Add New User!")
reader.Close()
Else
' User does not exist, add them
Dim cmd As MySqlCommand = New MySqlCommand("Insert into [ordering].[dbo].[accounts] ([EmpNo],[Username],[password]) values ('" + TextBox1.Text + "','" + TextBox2.Text + "', '" + TextBox3.Text + "')", Con)
cmd.ExecuteNonQuery()
MsgBox("Records Successfully Added!", MsgBoxStyle.Information, "Add New Customer!")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End If
End Using
End If
Con.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
Con.Dispose()
End Try
End Sub

Trouble displaying the content of DataGrid

Hi can i ask someone on how to easily display the content of my database using dataGridView, I already managed to update the inputted datas into the database. can you also give me some tips on my project.
I am using access as My database
Imports System.Data.OleDb
Imports System.Data
Public Class stor
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Validate()
Me.StorBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Database7DataSet)
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Then
MsgBox("Please Complete all the Information")
Else
Try
Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Gui\Documents\Database7.accdb;")
Dim insert As String = "Insert into stor values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "');"
Dim cmd As New OleDbCommand(insert, conn)
conn.Open()
cmd.ExecuteNonQuery()
MsgBox("create success")
Catch ex As Exception
MsgBox("error")
End Try
End If
End Sub
Private Sub stor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Database7DataSet.stor' table. You can move, or remove it, as needed.
Me.StorTableAdapter.Fill(Me.Database7DataSet.stor)
End Sub
End Class
Click Here to view my project

Pop up alert vb 6

Can someone help me. I'm having a hard time with this. All I need is to display an alert message whenever the medicines expired.My problem is when I got two or more expired medicines it doesn't alert all.Instead it alerts one medicine.Please help.
here is my code
Private Sub Form_Activate()
On Error Resume Next
With Main
.Text4 = Adodc1.Recordset.Fields("MedicineName")
.Text1.Text = Adodc1.Recordset.Fields("genericname")
.Text3.Text = Adodc1.Recordset.Fields("StockQuantity")
.Combo3 = Adodc1.Recordset.Fields("Expmonth")
.Combo4 = Adodc1.Recordset.Fields("Expday")
.Combo5 = Adodc1.Recordset.Fields("Expyear")
End With
Dim expirationdate As Date
expirationdate = CDate(Combo3 & "/" & Combo4 & "/" & Combo5)
datepicker.Value = Format(Now, "MMM-DD-yyyy")
If datepicker > expirationdate Then
MsgBox Text4.Text & " is expired ", vbExclamation, "Warning!"
If MsgBox("Do you want to dispose " & Text4 & "?", vbQuestion + vbYesNo, "Message") = vbYes Then
Adodc1.Recordset.Delete
ElseIf vbNo Then
Exit Sub
End If
End If
End Sub
Private Sub Form_Load()
Adodc1.CommandType = adCmdUnknown
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\clinic.mdb" & ";Persist Security Info=False"
Adodc1.RecordSource = "select * from inventory order by Expyear asc"
Adodc1.Refresh
Adodc1.Refresh
End sub
You need to loop through all of the records in your recordset. Currently, you're operating on the FIRST record only.
Do Until Adodc1.Recordset.EOF
' Assign values to textboxes, test date, etc.
' Fetch the next record...
Adodc1.Recordset.MoveNext
Loop
Bond is correct, you need to iterate over your recordset to display the message for each record that is expired.
Private Sub Form_Activate()
Dim expirationdate As Date
On Error Resume Next '<-- this is going to cause a problem if you never check for errors
Adodc1.Recordset.MoveFirst 'make sure the control is positioned on the first record
Do While Adodc1.Recordset.EOF = False 'loop over all of the records
With Main
.Text4.Text = Adodc1.Recordset.Fields("MedicineName")
.Text1.Text = Adodc1.Recordset.Fields("genericname")
.Text3.Text = Adodc1.Recordset.Fields("StockQuantity")
.Combo3 = Adodc1.Recordset.Fields("Expmonth")
.Combo4 = Adodc1.Recordset.Fields("Expday")
.Combo5 = Adodc1.Recordset.Fields("Expyear")
End With
expirationdate = CDate(Combo3 & "/" & Combo4 & "/" & Combo5)
datepicker.Value = Format(Now, "MMM-DD-yyyy")
If datepicker > expirationdate Then
MsgBox Text4.Text & " is expired ", vbExclamation, "Warning!"
If MsgBox("Do you want to dispose " & Text4 & "?", vbQuestion + vbYesNo, "Message") = vbYes Then
Adodc1.Recordset.Delete
End If
End If
Adodc1.Recordset.MoveNext
Loop
End Sub

Resources