Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have done entry from Richtextbox1 and when i click saved it was successfully saved but when i closed the debug and run again that was not there in Datagridview1 as well as Ms Access database please help me on this...

Note : When i click AddNew button without entering value it says "ExecuteReader requires an open and available Connection. The connection's current state is closed."

or kindly suggest to add, edit, update, delete record new source code

What I have tried:

Imports System.Data.OleDb

Public Class SongEntry
    Dim Provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection



    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
    End Sub

    Private Sub SongEntry_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       

    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

        Provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
        dataFile = "C:\Users\User\Desktop\Worship\Worship\bin\Debug\Song.accdb"
        connString = Provider & dataFile
        myConnection.ConnectionString = connString
        myConnection.Open()


        Dim str As String
        str = "Insert into MAS_SONGS([SM_CODE],[SM_EDESC],[SM_TDESC],[SM_SONGL1],[SM_SONGL2]) values (?,?,?,?,?)"
        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)


        cmd.Parameters.Add(New OleDbParameter("SM_CODE", CType(TextBox3.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("SM_EDESC", CType(TextBox1.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("SM_TDESC", CType(TextBox2.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("SM_SONGL1", CType(TextBox4.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("SM_SONGL2", CType(TextBox5.Text, String)))



        Try

            cmd.ExecuteNonQuery()
            cmd.Dispose()
            myConnection.Close()

            TextBox1.Clear()
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            TextBox5.Clear()

        Catch ex As Exception
            MsgBox("saved")

        End Try


    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Me.Close()

    End Sub

    Private Sub SongEntry_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown

        If e.KeyCode = Keys.Escape Then
            Me.Close()

        End If
    End Sub

    Private Sub TextBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox4.KeyPress
        If e.KeyChar = Convert.ToChar(1) Then
            DirectCast(sender, TextBox).SelectAll()
            e.Handled = True

        End If
    End Sub

    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Convert.ToChar(1) Then
            DirectCast(sender, TextBox).SelectAll()
            e.Handled = True

        End If
    End Sub

    Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
        If e.KeyChar = Convert.ToChar(1) Then
            DirectCast(sender, TextBox).SelectAll()
            e.Handled = True

        End If
    End Sub

    Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
        If e.KeyChar = Convert.ToChar(1) Then
            DirectCast(sender, TextBox).SelectAll()
            e.Handled = True

        End If
    End Sub

    Private Sub TextBox5_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox5.KeyPress
        If e.KeyChar = Convert.ToChar(1) Then
            DirectCast(sender, TextBox).SelectAll()
            e.Handled = True

        End If
    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        MASSONGSBindingSource.RemoveCurrent()

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        'for add
        TextBox1.Enabled = True
        TextBox2.Enabled = True
        TextBox3.Enabled = True
        TextBox4.Enabled = True
        TextBox5.Enabled = True
        TextBox6.Enabled = True
        ConnDB()

        Try
            mySQL = "Select SM_CODE from MAS_SONGS ORDER BY SM_CODE DESC"

            myCommand = New OleDbCommand(mySQL, myConnection)

            myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

            If myReader.Read = True Then
                TextBox3.Text = Val(myReader(0)) + 1
            Else
                TextBox3.Text = 300001

            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            myCommand.Dispose()
            myConnection.Close()


        End Try
    End Sub

    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged



    End Sub

    Private Sub TextBox6_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox6.KeyPress
        If e.KeyChar = Convert.ToChar(1) Then
            DirectCast(sender, TextBox).SelectAll()
            e.Handled = True

        End If
    End Sub
End Class
Posted
Updated 23-Oct-19 10:06am
v3
Comments
Richard Deeming 23-Oct-19 15:58pm    
Is there a blank version of your Access database as part of your project, set to always copy to the output directory?
Member 14621280 23-Oct-19 16:08pm    
i have changed copy to output directory to copy always but same error
RickZeeland 23-Oct-19 16:12pm    
Not good, use only copy if newer or your database data will be overwritten !
It would be safer to put your database in a safe location e.g. C:\MyDatabase
Member 14621280 24-Oct-19 3:22am    
Shall i give Copy newer... and my database was in \bin\debug\mydatabase.accdb
RickZeeland 24-Oct-19 3:59am    
That's ok, as long as you realize that on a Rebuild your changes will be lost as your database in the bin directory will be deleted.

1 solution

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