6 Replies - 832 Views - Last Post: 15 April 2013 - 09:52 AM Rate Topic: -----

#1 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Backup Creating Error In SQL Database

Posted 10 April 2013 - 02:48 AM

hello,

i have a application, with sql database, now i want to backup every week, so i have code a but it gives error:
"The process cannot access the file 'C:\Users\yogeishh\Desktop\Restaurant-Stock-Management-System\Restaurant-Stock-Management-System\DataBase\RestaurantStockDataBase.mdf' because it is being used by another process."

can anybuddy solve this error??
my code is :: "

 Private Sub btnCreateBackUpOfSetting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateBackUpOfSetting.Click
        'Try
        Dim fulfilename As String
        fulfilename = "C:\Users\yogeishh\Desktop\Restaurant-Stock-Management-System\Restaurant-Stock-Management-System\DataBase\RestaurantStockDataBase.mdf"
        Dim sfd As New SaveFileDialog
        sfd.FileName = "Restaurant Stock Backup"
        '            sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        sfd.Title = "Select Save Location..."
        sfd.Filter = "SQL Server 2005 Files (*.mdf)|*.mdf"
        If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
            My.Computer.FileSystem.CopyFile(fulfilename, sfd.FileName)
            Dim fileDetail As IO.FileInfo = My.Computer.FileSystem.GetFileInfo(sfd.FileName)
            fileDetail.IsReadOnly = True
            'fileDetail.Attributes = fileDetail.Attributes Or IO.FileAttributes.ReadOnly
            MsgBox("Backup is Created...")
        End If
        'Catch ex As Exception
        '    MsgBox(ex.Message)
        'End Try
    End Sub

    Private Sub btnRestoreBackUpOfSetting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestoreBackUpOfSetting.Click
        'Try
        Dim fulfilename As String
        fulfilename = "C:\Users\yogeishh\Desktop\Restaurant-Stock-Management-System\Restaurant-Stock-Management-System\DataBase\RestaurantStockDataBase.mdf"
        Dim ofd As New OpenFileDialog
        ofd.FileName = ""
        ofd.Filter = "SQL Server 2005 Files (*.mdf)|*.mdf"
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            My.Computer.FileSystem.DeleteFile("C:\Users\yogeishh\Desktop\Restaurant-Stock-Management-System\Restaurant-Stock-Management-System\DataBase\RestaurantStockDataBase.mdf")
            My.Computer.FileSystem.CopyFile(ofd.FileName, fulfilename)
            MsgBox("Backup is Restored...")
        End If
        'Catch ex As Exception
        '    MsgBox(ex.Message)
        'End Try
    End Sub



Help Me to solve this...

Thnks
@Nilesh

Is This A Good Question/Topic? 0
  • +

Replies To: Backup Creating Error In SQL Database

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Backup Creating Error In SQL Database

Posted 10 April 2013 - 04:35 AM

just a thought here but have you tried shutting down visual studio and going to your bin/debug folder and running the executable from there and see if it still happens? I could be that visual studio has it open or you haven't disposed of something properly that's retaining its use.
Was This Post Helpful? 0
  • +
  • -

#3 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Re: Backup Creating Error In SQL Database

Posted 10 April 2013 - 10:40 AM

View PostCharlieMay, on 10 April 2013 - 04:35 AM, said:

just a thought here but have you tried shutting down visual studio and going to your bin/debug folder and running the executable from there and see if it still happens? I could be that visual studio has it open or you haven't disposed of something properly that's retaining its use.

is there any way to do it properly??
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Backup Creating Error In SQL Database

Posted 10 April 2013 - 01:09 PM

View Postnilesh7136, on 10 April 2013 - 11:40 AM, said:

View PostCharlieMay, on 10 April 2013 - 04:35 AM, said:

just a thought here but have you tried shutting down visual studio and going to your bin/debug folder and running the executable from there and see if it still happens? I could be that visual studio has it open or you haven't disposed of something properly that's retaining its use.

is there any way to do it properly??

If you can't write the file because it is still open, or you can't read a file because it's still open, then yes, there is a way to do it properly. You need to close the file, and/or Dispose the Stream, if that's what's holding it open.

Can you tell us how you are opening the file? Are you reading it all in at once, then using it from variables?

This post has been edited by lar3ry: 10 April 2013 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Re: Backup Creating Error In SQL Database

Posted 11 April 2013 - 07:17 AM

View Postlar3ry, on 10 April 2013 - 01:09 PM, said:

View Postnilesh7136, on 10 April 2013 - 11:40 AM, said:

View PostCharlieMay, on 10 April 2013 - 04:35 AM, said:

just a thought here but have you tried shutting down visual studio and going to your bin/debug folder and running the executable from there and see if it still happens? I could be that visual studio has it open or you haven't disposed of something properly that's retaining its use.

is there any way to do it properly??

If you can't write the file because it is still open, or you can't read a file because it's still open, then yes, there is a way to do it properly. You need to close the file, and/or Dispose the Stream, if that's what's holding it open.

Can you tell us how you are opening the file? Are you reading it all in at once, then using it from variables?

i have mention all in question,, i hv created stock mngm project, in this project the one features is call backup of database, so when user click on create backup button, at that time the savefiledialogbox open and copy the project database to user selected location, and one message will focus, "backup created", now so when i create backup its work properly, and database file copied, now when i want to restore that at that time gives error like this "Access to the path 'D:\Projects\Restaurant-Stock-Management-System\Restaurant-Stock-Management-System\bin\Debug\RestaurantStockDataBase.mdf' is denied."
any solution for that???
Was This Post Helpful? 0
  • +
  • -

#6 nilesh7136  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 120
  • Joined: 03-January 13

Re: Backup Creating Error In SQL Database

Posted 15 April 2013 - 07:37 AM

View PostCharlieMay, on 10 April 2013 - 04:35 AM, said:

just a thought here but have you tried shutting down visual studio and going to your bin/debug folder and running the executable from there and see if it still happens? I could be that visual studio has it open or you haven't disposed of something properly that's retaining its use.

hey its still give error "Access to the path 'G:\My Project\Pending\Restaurant-Stock-Management-System\Restaurant-Stock-Management-System\bin\Debug\RestaurantStockDataBase.mdf' is denied."

is there any option to stop the process of that file??
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Backup Creating Error In SQL Database

Posted 15 April 2013 - 09:52 AM

View Postnilesh7136, on 15 April 2013 - 08:37 AM, said:

hey its still give error "Access to the path 'G:\My Project\Pending\Restaurant-Stock-Management-System\Restaurant-Stock-Management-System\bin\Debug\RestaurantStockDataBase.mdf' is denied."

is there any option to stop the process of that file??

You are copying the backup file, but it copies the attributes with it, including ReadOnly.

Add these lines to your btnRestoreBackUpOfSetting_Click() Sub

        Dim fileDetail As IO.FileInfo = My.Computer.FileSystem.GetFileInfo(fulfilename)
        fileDetail.IsReadOnly = False


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1