Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is a code. I have a 3 textboxes. If I enter a value which is not a number into first textbox named "oznaka" the program will break. Other two textboxes (ren and rmn) are ok. And program will not crash down if I enter a numeric value into a first text field.
Why???
Thanks in advance

VB
Public Class Form1
Dim conn As New OleDb.OleDbConnection
Private Sub RefreshData()
If Not conn.State = ConnectionState.Open Then
'open connection
conn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT Oznaka as [Oznaka], " & _
"ReN as [ReN], RmN " & _
" FROM Tabela ORDER BY Oznaka", conn)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
'offer data in data table into datagridview
Me.DataGridView1.DataSource = dt
'close connection
conn.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim cmd As New OleDb.OleDbCommand
If Not conn.State = ConnectionState.Open Then
'open connection if it is not yet open
conn.Open()
End If
cmd.Connection = conn
'check whether add new or update
If Me.txtOznaka.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = "INSERT INTO Tabela(Oznaka, ReN, RmN) " & _
" VALUES(" & Me.txtOznaka.Text & ",'" & Me.txtReN.Text & "','" & _
Me.txtRmN.Text & "'')"
cmd.ExecuteNonQuery()
Else
'update data in table
cmd.CommandText = "UPDATE Tabela " & _
" SET Oznaka=" & Me.txtOznaka.Text & _
", ReN='" & Me.txtReN.Text & "'" & _
", RmN='" & Me.txtRmN.Text & "'" & _
" WHERE Oznaka=" & Me.txtOznaka.Tag
cmd.ExecuteNonQuery()
End If
'refresh data in list
RefreshData()
'clear form
'Me.btnClear.PerformClick()
'close connection
conn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase
.Load
conn = New OleDb.OleDbConnection
conn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.
StartupPath & "\baza.mdb"
'
'get data into list
Me.RefreshData()
End Sub
End Class
Posted
Comments
chandanadhikari 15-Jan-13 0:16am    
hi,
please tell us what error message you get (if any) when the crash happens and also try to debug using breakpoints to point out which line is causing the crash .
blueye89 15-Jan-13 17:34pm    
This line have a error: cmd.ExecuteNonQuery()

1 solution

There's a syntax error in insert and update query.

your insert & update statements should be changed as below,

Insert Statement -
SQL
"INSERT INTO Tabela(Oznaka, ReN, RmN) " & _
" VALUES('" & Me.txtOznaka.Text & "' ,'" & Me.txtReN.Text & "','" & _
Me.txtRmN.Text & "'')"


Update Statement -
SQL
'update data in table
cmd.CommandText = "UPDATE Tabela " & _
" SET Oznaka='" & Me.txtOznaka.Text & "'" & _
", ReN='" & Me.txtReN.Text & "'" & _
", RmN='" & Me.txtRmN.Text & "'" & _
" WHERE Oznaka=" & Me.txtOznaka.Tag


Try out with this code..
Hope this will work for u..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900