1.

Solve : An error in Vb.net?

Answer»

this is my code:
          IMPORTS System.DATA.OleDb

Public Class frmStudent
    Dim cnn As New OleDb.OleDbConnection

    Private Sub btnClose_Click(ByVal sender As System.OBJECT, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtstdID.Clear()
        Me.txtStdName.Clear()
        Me.txtadd1.Clear()
        Me.txtadd2.Clear()
        Me.txtBusPhone.Clear()
        Me.txtPerPhone.Clear()
        Me.txtFax.Clear()
        Me.txtEmail.Clear()
        'enable button edit
        Me.btnEdit.Enabled = True
        'set button add to add label
        Me.btnAdd.Text = "Add"
        '
        Me.txtstdID.Focus()
    End Sub

    Private Sub RefreshData()
        If Not cnn.State = ConnectionState.Open Then
            'open connection
            cnn.Open()
        End If

        Dim da As New OleDb.OleDbDataAdapter("SELECT Sup_ID as [Sup_ID], " & _
                                             "Sup_Name as [Sup_Name], Sup_add1, Supp_add2, Bss_con_No,Pre_con_No,Fax,Email " & _
                                             " FROM Main_Report ORDER BY Sup_ID", cnn)
        Dim dt As New DataTable
        'fill data to datatable
        da.Fill(dt)

        'offer data in data table into datagridview
        Me.dgvData.DataSource = dt

        'close connection
        cnn.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim cmd As New OleDb.OleDbCommand
        If Not cnn.State = ConnectionState.Open Then
            'open connection if it is not yet open
            cnn.Open()
        End If

        cmd.Connection = cnn
        'check whether add new or update
        If Me.txtstdID.Tag & "" = "" Then
            'add new
            'add data to table
            cmd.CommandText = "INSERT INTO Main_Report(Sup_ID, Sup_Name, Sup_add1, Supp_add2, Bss_con_No,Pre_con_No,Fax,Email)" & _
                            " VALUES(" & Me.txtstdID.Text & ",'" & Me.txtStdName.Text & "','" & _
                            Me.txtadd1.Text & "','" & Me.txtadd2.Text & "','" & _
                            Me.txtBusPhone.Text & "','" & _
                            Me.txtPerPhone.Text & "','" & Me.txtFax.Text & "','" & Me.txtEmail.Text & "')"
            TRY
                cmd.ExecuteNonQuery()
            Catch
                'MsgBox("Can't load Web page")
            End Try

        Else
            'update data in table
            cmd.CommandText = "UPDATE Main_Report " & _
                        " SET Sup_ID=" & Me.txtstdID.Text & _
                        ", Sup_Name='" & Me.txtStdName.Text & "'" & _
                        ", Sup_add1='" & Me.txtadd1.Text & "'" & _
                        ", Supp_add2='" & Me.txtadd2.Text & "'" & _
                        ", Bss_con_No='" & Me.txtBusPhone.Text & "'" & _
                        ", Pre_con_No='" & Me.txtPerPhone.Text & "'" & _
                        ", Fax='" & Me.txtFax.Text & "'" & _
                        ", Email='" & Me.txtEmail.Text & "'" & _
                        " WHERE Sup_ID=" & Me.txtstdID.Tag
            cmd.ExecuteNonQuery()
        End If
        'refresh data in LIST
        RefreshData()
        'clear form
        Me.btnClear.PerformClick()

        'close connection
        cnn.Close()
    End Sub

    Private Sub frmStudent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cnn = New OleDb.OleDbConnection
        cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\DCSD_1.mdb"
        '
        'get data into list
        Me.RefreshData()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'check for the selected item in list
        If Me.dgvData.Rows.Count > 0 Then
            If Me.dgvData.SelectedRows.Count > 0 Then
                Dim intSup_ID As Char = Me.dgvData.SelectedRows(0).Cells("Sup_ID").Value
                'get data from database followed by student id
                'open connection
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If
                'get data into datatable
                Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Main_Report " & _
                                                     " WHERE Sup_ID=" & intSup_ID, cnn)
                Dim dt As New DataTable
                da.Fill(dt)

                Me.txtstdID.Text = intSup_ID.ToString
                Me.txtStdName.Text = dt.Rows(0).Item("Sup_Name").ToString
                Me.txtadd1.Text = dt.Rows(0).Item("Sup_add1").ToString
                Me.txtadd2.Text = dt.Rows(0).Item("Supp_add2").ToString
                Me.txtBusPhone.Text = dt.Rows(0).Item("Bss_con_No")
                Me.txtPerPhone.Text = dt.Rows(0).Item("Pre_con_No")
                Me.txtFax.Text = dt.Rows(0).Item("Fax")
                Me.txtEmail.Text = dt.Rows(0).Item("Email").ToString
                '
                'hide the id to be edited in TAG of txtstdid in case id is changed
                Me.txtstdID.Tag = intSup_ID
                'change button add to update
                Me.btnAdd.Text = "Update"
                'disable button edit
                Me.btnEdit.Enabled = False
                'close connection
                cnn.Close()
            End If
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'check for the selected item in list
        If Me.dgvData.Rows.Count > 0 Then
            If Me.dgvData.SelectedRows.Count > 0 Then
                Dim intSup_ID As Char = Me.dgvData.SelectedRows(0).Cells("Sup_ID").Value
                'open connection
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If

                'delete data
                Dim cmd As New OleDb.OleDbCommand
                cmd.Connection = cnn
                cmd.CommandText = "DELETE FROM Main_Report WHERE Sup_ID=" & intSup_ID
                cmd.ExecuteNonQuery()
                'refresh data
                Me.RefreshData()

                'close connection
                cnn.Close()
            End If
        End If
    End Sub
End Class
 please help me. i have code in 2010 vb.net i have attach my files

[year+ old attachment deleted by admin]



Discussion

No Comment Found