Click here to Skip to main content
15,888,351 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."

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()
    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

        Try
            mySQL = "Select SM_CODE from MAS_SONGS ORDER BY SM_CODE DESC"
            ConnDB()


            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 TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged

    End Sub
End Class
Posted
Updated 20-Oct-19 7:46am

Read the error message:
Quote:
"ExecuteReader requires an open and available Connection. The connection's current state is closed."

It could not be clearer about exactly what the problem is, and even how to fix it.

I have no idea what ConnDB does - you don't show us that - but everywhere else you use myConnection you open it before you use it...
 
Share this answer
 
For the "disappearing" data, do you have your database file in your project? Click on it and look in the Properties window. Does it say "Copy Always"? If so, there's you're disappearing data problem. Every time you run your project in the debugger, the data file in your bin\Debug folder gets overwritten by the database file you have in your project.
 
Share this answer
 
Comments
Member 14621280 20-Oct-19 13:54pm    
so what is the solution please
Dave Kreskowiak 20-Oct-19 15:00pm    
Did you try clicking on "Copy Always" and see what the options are?
Member 14621280 21-Oct-19 13:02pm    
how to do it please let me know
Dave Kreskowiak 21-Oct-19 13:09pm    
Put your hand on the mouse, move it to where it says "Copy Always", click on it, and you'll get a dropdown with a list of items. Click on the one that says, "Copy if newer".

Next time, you might want to actually TRY IT before wasting your time waiting for someone to hand you an answer.
Member 14621280 21-Oct-19 13:36pm    
Pls dont mistaken i am new to this and i am learning that is what ....

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