1.11M Members

unable to save data when I close the application

 
0
 

I can successfuly insert data but when I close the app the data is not save please help me..and how to retrieve the BLOB image in ms access and display it to datagridview.

this is my code when I insert data

 Private Sub addStudent()
        Dim insertQuery As String = "INSERT INTO Student(ID,FamilyName,FirstName,MiddleName,Gender,CivilStatus,Age,Address,Course,Major,Contact,[Image],CreatedAt,SchoolYear) " & _
                                    "VALUES(@ID,@familyName,@firstName,@middleName,@gender,@civilStatus,@age,@address,@course,@major,@contact,@image,@createdAt,@schoolYear)"

        Using connection As New OleDbConnection(connString)
            Dim command As New OleDbCommand(insertQuery, connection)

           Dim ImageBytes As Byte()
            Dim SampleImage As Bitmap = New Bitmap(PictureBox1.Image)
            Dim mStream As New MemoryStream
            SampleImage.Save(mStream, Imaging.ImageFormat.Jpeg)
            ImageBytes = mStream.ToArray




            command.Parameters.AddWithValue("@ID", TextBox2.Text)
            command.Parameters.AddWithValue("@familyName", txtFamilyName.Text)
            command.Parameters.AddWithValue("@firstName", txtFirstName.Text)
            command.Parameters.AddWithValue("@middleName", txtMiddleName.Text)
            command.Parameters.AddWithValue("@gender", Gender)
            command.Parameters.AddWithValue("@civilStatus", cmbStatus.Text)
            command.Parameters.AddWithValue("@age", txtAge.Text)
            command.Parameters.AddWithValue("@address", txtAddress.Text)
            command.Parameters.AddWithValue("@course", cmbCourse.Text)
            command.Parameters.AddWithValue("@major", cmbMajor.Text)
            command.Parameters.AddWithValue("@contact", txtContact.Text)
            command.Parameters.AddWithValue("@image", ImageBytes)
            command.Parameters.AddWithValue("@createdAt", Convert.ToDateTime(DateTimePicker2.Value.Date))
            command.Parameters.AddWithValue("@schoolYear", txtSchoolYear.Text)


            Try
                connection.Open()
                command.ExecuteNonQuery()
                MsgBox("Student Added successfully")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try


        End Using

    End Sub

and this code where I display data in gridview

 Private Sub refresh_data()
        Dim query As String = "SELECT * FROM Student"
        Using connection As New OleDbConnection(connString)

            Dim da As OleDbDataAdapter = Nothing
            Dim dt As New DataTable
            Try
                connection.Open()
                da = New OleDbDataAdapter(query, connection)
                da.Fill(dt)
                connection.Close()
                RadGridView1.DataSource = dt
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End Using
    End Sub
 
1
 

I can successfuly insert data but when I close the app the data is not save

What code do you have that you think will save data for you?

Is this a WinForms application? If so, you might want to look at the FormClosing event.

 
0
 

Thanks You Sir :D

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article