none
必須宣告純量變數 "@Name" RRS feed

  • 問題

  • 您好,

    小弟最近剛學ASP.NET 3.5, 做了幾個FileUpload控制項, 並且同時要判斷username等資料, 然後上傳至SQL server...但是當我按下"上傳檔案"後,卻會出現底下的錯誤訊息...請教各位前輩..到底是哪裡有問題呢..?  謝謝您的指教!!

    =======================================================================

    必須宣告純量變數 "@Name"。
    描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。

    例外詳細資訊: System.Data.SqlClient.SqlException: 必須宣告純量變數 "@Name"。

    原始程式錯誤:


    行 41:                         ds.InsertParameters.Add("@UploadComputerName", My.Computer.Name)
    行 42:                         '執行新增的指令
    行 43:                         ds.Insert()
    行 44:
    行 45:                         myFL.SaveAs(savePath)

    ==========================================================================

    原始代碼:

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim saveDir As String = "~\Files\"
            Dim appPath As String = Request.PhysicalApplicationPath
            Dim myLabel As New System.Text.StringBuilder

            For i = 1 To (Request.Files.Count - 1)
                Dim myFL As New FileUpload
                myFL = CType(Page.FindControl("FileUpload" & i), FileUpload)
                Dim fileName As String = myFL.FileName

                If (myFL.HasFile) Then
                    Dim extension As String = System.IO.Path.GetExtension(fileName)
                    Dim pathToCheck As String = appPath & saveDir & fileName
                    '==================================================(判斷副檔名--Start)
                    If (extension = ".pdf") Then

                        '==================================================(判斷重複檔名--Start)
                        If (System.IO.File.Exists(pathToCheck)) Then

                            myLabel.Append("<br>檔名︰ " & fileName)
                            Label1.Text = "抱歉‥您已經上傳過此檔案!<hr>" & myLabel.ToString

                            '==================================================(判斷重複檔名--End)
                        Else
                            Dim ds As New System.Web.UI.WebControls.SqlDataSource
                            ds.ConnectionString = "Data Source=taiwan;Initial Catalog=qa;Integrated Security=True"
                            Dim insertCmd As String = ""
                            Dim savePath As String = appPath & saveDir & fileName

                            insertCmd = "Insert Into Document(Name, FileSize, UploadBy, UploadComputerName) Values(@Name, @Size, @UploadBy, @UploadComputerName)"
                            ds.InsertCommand = insertCmd
                            '建立SQL陳述句的參數
                            ds.InsertParameters.Add("@Name", myFL.FileName)
                            ds.InsertParameters.Add("@Size", myFL.PostedFile.ContentLength)
                            ds.InsertParameters.Add("@UploadBy", My.User.Name)
                            ds.InsertParameters.Add("@UploadComputerName", My.Computer.Name)
                            '執行新增的指令
                            ds.Insert()

                            myFL.SaveAs(savePath)
                            myLabel.Append("<br>檔名︰ " & fileName)
                            Label1.Text = "上傳成功!<hr>" & myLabel.ToString

                        End If

                    Else
                        Label1.Text = "抱歉‥只有.pdf的檔案類型才能上傳!"

                    End If
                    '==================================================(判斷副檔名--End)
                End If
            Next


        End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>未命名頁面</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="text-align: center">
       
            <br />
            <br />
            <asp:Label ID="Label3" runat="server" Font-Size="Small"
                Text="※請先選擇好檔案後,再按&quot;檔案上傳&quot;。"></asp:Label>
            <br />
            <br />
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <br />
            <asp:FileUpload ID="FileUpload2" runat="server" />
            <br />
            <asp:FileUpload ID="FileUpload3" runat="server" />
            <br />
            <asp:FileUpload ID="FileUpload4" runat="server" />
            <br />
            <asp:FileUpload ID="FileUpload5" runat="server" />
            <br />
            <asp:Button ID="Button1" runat="server" Text="檔案上傳" onclick="Button1_Click"
                CommandName="Insert" />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
            <br />
       
        </div>
        </form>
    </body>
    </html>


     

    2010年4月16日 下午 12:16

解答

所有回覆