i ma creating and restoring database dynamically but restoring time i got error
Error-->
RESTORE cannot process database 'Dbname' because it is in use by this session. It is recommended that the master database be used when performing this operation.
RESTORE DATABASE is terminating abnormally.
following my code
Dim sqlnewDbcrecon As SqlConnection = New SqlConnection("Data Source=" & cbServers.SelectedItem.ToString & "; Initial Catalog=" & NewFDb & "; user Id=; password=;")
Dim rptpath As String
If ApplicationDeployment.IsNetworkDeployed = True Then
rptpath = AppDomain.CurrentDomain.BaseDirectory.ToString & "Db\Pharma"
Else
rptpath = AppDomain.CurrentDomain.BaseDirectory.ToString
rptpath = rptpath.Substring(0, rptpath.Length - 10) + "Db\Pharma"
End If
FYear = TxtreferDbname.Text & Year(dtpstartfinyear.Text) & Year(dtpEndfinyear.Text)
Dim Splfyear() As String = FYear.Split("20")
NewFDb = TxtreferDbname.Text + Splfyear(1).TrimStart("0") + Splfyear(2).TrimStart("0")
Dim sqlDbcrecon As SqlConnection = New SqlConnection("Data Source=" & cbServers.SelectedItem.ToString & "; user Id=; password=;")
Cls.cmdNewfire("CREATE DATABASE " + NewFDb + " ON (NAME = Sales_dat,FILENAME = '" + TxtDbPathname.Text + "\" & NewFDb & ".mdf',SIZE = 10,MAXSIZE = 50,FILEGROWTH = 5)LOG ON( NAME = '" & NewFDb & "_log', FILENAME = '" + TxtDbPathname.Text + "\" & NewFDb & ".ldf',SIZE = 5MB, MAXSIZE = 25MB,FILEGROWTH = 5MB)", sqlnewDbcrecon)
Dim Oldbnam As String = "Pharma"
Dim newdbname As String = NewFDb
Dim sqlnewDbcreconn As SqlConnection = New SqlConnection("Data Source=" & cbServers.SelectedItem.ToString & "; Initial Catalog=" & NewFDb & "; user Id=; password=;")
Cls.cmdNewfire("RESTORE DATABASE " & newdbname & " FROM DISK = N'D:\Manoj\" & Oldbnam & "' WITH FILE = 1, " &
"MOVE 'VBPharma' TO N'D:\Manoj\" & Oldbnam & ".mdf', " &
"MOVE 'VBPharma_log' TO N'D:\Manoj\" & Oldbnam & "_Log.ldf', Replace", sqlnewDbcreconn)
class name
Public Function cmdNewfire(ByVal qry As String, ByVal sqlcon As SqlConnection) As String
Dim sqlcmd As New SqlCommand(qry, sqlcon)
sqlcon.Open()
cmdNewfire = sqlcmd.ExecuteNonQuery
sqlcon.Dispose()
sqlcmd.Dispose()
End Function