View Single Post
Old June 21st, 2006, 10:09 AM
GigaBear GigaBear is offline
Registered User
Join Date: Jun 2006
Location: Maassluis, , Netherlands.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts

I found the code to update the record after the file is uploaded. Uploading works, i see the new file in the directory it is supposed to be in.
Then the toruble starts. I think the code will work to update the record, but when I run the page I get an Error:

FOUT: Unable to open the physical file "H:\Visual Websites\Communicator\App_Data\KPI.mdf".
Operating system error 32: "32(Het proces heeft geen toegang tot het bestand omdat het bestand door een ander proces wordt gebruikt.)".
An attempt to attach an auto-named database for file H:\Visual Websites\Communicator\App_Data\KPI.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

I suppose this has to do with having Visual Web Developer connected to the database, but I'm not sure. Will the same problem occur when I go live and one user looks at data and another tries to update???

The code I have so far:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Uploaden KPI bestand" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If FileUpload1.HasFile Then
                FileUpload1.SaveAs("H:\\Visual websites\\Communicator\\KPI_bestanden\\" & FileUpload1.FileName)
                Label1.Text = "Het bestand " & FileUpload1.FileName & " is verstuurd."
            Catch ex As Exception
                Label1.Text = "FOUT: " & ex.Message.ToString()
            End Try
        End If
    End Sub

    Public Sub Run()
        Dim myConnection As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\KPI.mdf;Integrated Security=True;")
        Dim myCommand As SqlCommand = New SqlCommand
        Dim myTrans As SqlTransaction
        Dim strPath As String = "KPI_bestanden\" & FileUpload1.FileName
        Dim strID As Integer = Request.QueryString("kpiid")

        ' Open the connection.

        ' Assign the connection property.
        myCommand.Connection = myConnection

        ' Begin the transaction.
        myTrans = myConnection.BeginTransaction()

        ' Assign transaction object for a pending local transaction
        myCommand.Transaction = myTrans

            ' Update.
            myCommand.CommandText = "UPDATE tblKPI KPI_URL = " & strPath & " WHERE KPI_ID = " & strID & ";"

        Catch e As Exception
            Label2.Text = e.ToString()
        End Try
    End Sub

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>Je wilt een bestand toevoegen / wijzigen aan de volgende KPI:</p>

    <asp:FileUpload ID="FileUpload1" runat="server" Style="position: relative; left: 7px;" /><br />
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="left: 31px;
        position: relative; top: 0px;" Text="Bestand versturen" /><br />
    <br />
    <asp:Label ID="Label1" runat="server" Style="left: 4px; position: relative; top: 0px"
        Width="231px"></asp:Label><br />
    <asp:Label ID="Label2" runat="server" Style="left: 5px; position: relative; top: 10px"