odbc Exception was unhalded

  • Thread starter Thread starter alvin10
  • Start date Start date
A

alvin10

Guest
Hi,

i have stuck with this eror with

odbc Exception was unhalded






Imports System.Data.Odbc
Public Class Form2
Dim NewData As Boolean

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
NewData = True
LoadDataFrombiodata()
LoadDataFromUser()
LoadDataFromJoin()

ComboBox1.Items.Add(1)
ComboBox1.Items.Add(2)
ComboBox1.Items.Add(3)

End Sub

Sub LoadDataFrombiodata()
connect()
Dim da As OdbcDataAdapter
Dim dt As DataTable
da = New OdbcDataAdapter("SELECT * FROM biodata", connection)
dt = New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
connection.Close()
da.Dispose()
End Sub

Sub LoadDataFromUser()
connect()
Dim da As OdbcDataAdapter
Dim dt As DataTable
da = New OdbcDataAdapter("SELECT * FROM user", connection)
dt = New DataTable
da.Fill(dt)
DataGridView2.DataSource = dt
connection.Close()
da.Dispose()
End Sub

Sub LoadDataFromJoin()
connect()
Dim da As OdbcDataAdapter
Dim dt As DataTable
da = New OdbcDataAdapter("SELECT * FROM biodata as b INNER JOIN user as u ON (b.id=u.id_user) ", connection)
dt = New DataTable
da.Fill(dt)
DataGridView3.DataSource = dt
connection.Close()
da.Dispose()
End Sub

Sub clearText()

TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
ComboBox1.Text = "1"
TextBox1.Focus()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
NewData = True
clearText()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim SaveData, SaveUsers As String
Dim message As String

If NewData Then
message = MsgBox("Are you sure to add New data into database?", vbYesNo + vbInformation, "informations")
If message = vbNo Then
Exit Sub
End If
' insert new data statement into biodata tables
SaveData = "INSERT INTO biodata(id,nama,nis,kelas,alamat) Values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "')"
' insert new data statement into user table

SaveUsers = "Insert Into user(id_user,password,level) Values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & ComboBox1.Text & "')"

Else
message = MsgBox("Are you sure update this Data into Database ", vbYesNo + vbInformation, "Informations")
If message = vbNo Then
Exit Sub
End If

'to update data into biodata Tables
SaveData = "Update biodata SET nama='" & TextBox2.Text & "',nis='" & TextBox3.Text & "',kelas='" & TextBox4.Text & "',alamat='" & TextBox5.Text & "' where id='" & TextBox1.Text & "' "

'to update data into user Tables

SaveUsers = "update user SET password='" & TextBox6.Text & "',level='" & ComboBox1.Text & "' where id_user='" & TextBox1.Text & "'"
End If

RUNSql(SaveData) 'insert or update process into Biodata tabel
RUNSql(SaveUsers) 'insert or update process into user tabel

DataGridView1.Refresh() 'refresh gridview biodata
DataGridView2.Refresh() 'refresh gridview biodata
DataGridView3.Refresh() 'refresh gridview biodata

LoadDataFrombiodata() 'fill data from Biodata Tables into DataGridView 1
LoadDataFromUser() 'fill data from user Tables into DataGridView 2
LoadDataFromJoin() 'fill data from Biodata Tables and User Tables into DataGridView 3


End Sub

Private Sub RUNSql(ByVal sql As String)
Dim cmd As New OdbcCommand

connect()
Try
cmd.Connection = connection
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.ExecuteNonQuery() ''at this error
cmd.Dispose()
connection.Close()
MsgBox("Data Hasbeen Saved!", "information")
Catch ex As Exception
MsgBox("Data has no been saved")
End Try
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim deleteBiodata, DeleteUsers As String
Dim Message As String
Message = MsgBox("Are you sure want to delete this data? ", vbExclamation + vbYesNo, "warning")
If Message = vbNo Then Exit Sub

'Delete data From Biodata Tables
deleteBiodata = "DELETE FROM biodata where id='" & TextBox1.Text & "',nama='" & TextBox2.Text & "',nis='" & TextBox3.Text & "',kelas='" & TextBox4.Text & "',alamat='" & TextBox5.Text & "'"

'Delete data From User Tables
DeleteUsers = "delete from user where id='" & TextBox1.Text & "',password='" & TextBox6.Text & "',level='" & ComboBox1.Text & "' "

RUNSql(deleteBiodata) 'delete function for biodata tables
RUNSql(DeleteUsers) 'delete function for user tables

LoadDataFrombiodata() 'fill data from Biodata Tables into DataGridView 1
LoadDataFromUser() 'fill data from user Tables into DataGridView 2
LoadDataFromJoin() 'fill data from Biodata Tables and User Tables into DataGridView 3



clearText()

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
End
Me.Close()
End Sub

Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellClick
senToTextBox(e.RowIndex) 'send gridview values to textbox
NewData = False
End Sub

Sub senToTextBox(ByVal x As Integer)
'its for get value from datagridview into textbox

Try
TextBox1.Text = DataGridView3.Rows(x).Cells(0).Value
TextBox2.Text = DataGridView3.Rows(x).Cells(1).Value
TextBox3.Text = DataGridView3.Rows(x).Cells(2).Value
TextBox4.Text = DataGridView3.Rows(x).Cells(3).Value
TextBox5.Text = DataGridView3.Rows(x).Cells(4).Value
TextBox6.Text = DataGridView3.Rows(x).Cells(5).Value
ComboBox1.Text = DataGridView3.Rows(x).Cells(6).Value
Catch ex As Exception

End Try
End Sub
End Class

Continue reading...
 
Back
Top