INSERT声明遗漏了一件

时间:2011-08-24 19:05:02

标签: asp.net sql-server vb.net

我有一个应该上传文件的模态弹出窗口。它做得很好,除了它没有给他们一个标题,因此我的页面上没有任何内容显示,因为标题是它们在列表中显示的方式。我应该用什么来替换LinkTitle.Text才能使它工作? 我试图解决这个人的代码,因为它没有一个正常。我在下面添加了一条评论,其中包含参数化的新代码。这是在使用Microsoft SQL Server的ASP.net 4.0 VB中。

    Protected Sub SubmitDocument_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SubmitDocument.Click
    DocumentModal.Hide()
    'Builds the full absolute URL to be inserted into the database. 
    Dim hostURL As String = Request.Url.Scheme & "://" & Request.Url.Host & ":" & Request.Url.Port & Request.ApplicationPath
    Dim sqlFileHREF As String = "INSERT INTO Marketing (ProductID, MarketingTypeID, MarketingTitle, MarketingData) VALUES (" & ProductID.Value & " ,4, '" & LinkTitle.Text & "', '" & hostURL & "uploads/" & ProductID.Value & "/" & DocumentUpload.FileName & "')"
    sqlFileHREF.Replace("'", "''")
    'Create SQL Connection
    Dim SqlConnection As New SqlConnection("****************************************")
    SqlConnection.Open()
    Dim sqlCommand As New SqlCommand(sqlFileHREF, SqlConnection)
    sqlCommand.ExecuteNonQuery()
    SqlConnection.Close()
    Response.Redirect(Request.RawUrl)
End Sub

       <!-- Add a Document -->
    <li>
        <asp:LinkButton ID="DocumentButton" runat="server">Document</asp:LinkButton>
        <asp:Panel ID="DocumentPanel" runat="server" CssClass="modalPopup" Style="display:none">
            <asp:FileUpload ID="DocumentUpload" runat="server" />
            <asp:Button ID="SubmitDocument" runat="server" Text="Upload" onclick="SubmitDocument_Click" /><asp:Button ID="CancelDocument" runat="server" Text="Cancel" /><asp:HiddenField ID="filename" runat="server" />
        </asp:Panel>       
        <asp:ModalPopupExtender ID="DocumentModal" runat="server" DropShadow="True" DynamicServicePath="" Enabled="True" PopupControlID="DocumentPanel" TargetControlID="DocumentButton"></asp:ModalPopupExtender>
    </li>

1 个答案:

答案 0 :(得分:1)

这是我现在的代码,感谢上面评论过的人们!

<!-- Add a Document -->
    <li>
        <asp:LinkButton ID="DocumentButton" runat="server">Document</asp:LinkButton>
        <asp:Panel ID="DocumentPanel" runat="server" CssClass="modalPopup" Style="display:none">
            Title:<asp:TextBox ID="DocumentTitle" runat="server"></asp:TextBox>
            <asp:FileUpload ID="DocumentUpload" runat="server" />
            <asp:Label ID="DocumentLabel" runat="server"></asp:Label>
            <asp:Button ID="SubmitDocument" runat="server" Text="Upload" onclick="SubmitDocument_Click" />
<asp:Button ID="CancelDocument" runat="server" Text="Cancel" />
<asp:HiddenField ID="filename" runat="server" />
        </asp:Panel>
        <asp:ModalPopupExtender ID="DocumentModal" runat="server" DropShadow="True" DynamicServicePath="" Enabled="True" PopupControlID="DocumentPanel" TargetControlID="DocumentButton"></asp:ModalPopupExtender>
      </li>

Protected Sub SubmitDocument_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SubmitDocument.Click
    DocumentModal.Hide()
    'Builds the full absolute URL to be inserted into the database. 
    Dim hostURL As String = Request.Url.Scheme & "://" & Request.Url.Host & ":" & Request.Url.Port & Request.ApplicationPath

    'SQL INSERT: Marketing Table
    Dim strSQL As String = "INSERT INTO Picklist (Title, Data) VALUES (@Title, @Data);INSERT INTO Marketing (ProductID, MarketingTypeID, MarketingTitle, MarketingData) VALUES (@ProductID,4, 'Document', scope_identity())"
    DocumentUpload.PostedFile.SaveAs(Server.MapPath(String.Format("/uploads/{0}/{1}", ProductID.Value, DocumentUpload.PostedFile.FileName)))

    Using cn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)

        Using cmd As New SqlCommand(strSQL, cn)
            cmd.Parameters.Add(New SqlParameter("@ProductID", ProductID.Value))
            cmd.Parameters.Add(New SqlParameter("@Title", DocumentTitle.Text))
            cmd.Parameters.Add(New SqlParameter("@Data", hostURL & "uploads/" & ProductID.Value & "/" & DocumentUpload.FileName))

            cn.Open()

            cmd.ExecuteNonQuery()
        End Using
    End Using
    Response.Redirect(Request.RawUrl)
End Sub