0

我收到此错误:

不允许从数据类型 sql_variant 到 uniqueidentifier 的隐式转换。使用 CONVERT 函数运行此查询。

我的代码如下:

源代码 :

<asp:ListView ID="ListView1" runat="server" DataKeyNames="UserId" DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" >

       <InsertItemTemplate>

            <asp:TextBox ID="UserPicUrlTextBox" runat="server" Text='<%# Bind("UserPicUrl") %>' />
            <asp:FileUpload ID="FileUpload1" runat="server" />

            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CausesValidation="false" CommandName="Cancel" Text="Clear" />

        </InsertItemTemplate>
</asp:ListView>

矿井插入参数为:

<InsertParameters>
            <asp:Parameter Name="UserId" Type="Object" />
            <asp:Parameter Name="Category" Type="String" />
            <asp:Parameter Name="Class" Type="Int32" />
            <asp:Parameter Name="UserPicUrl" Type="String" />
              .
              .
           </InsertParameters>

C# :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class userProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        // Get a reference to the currently logged on user
        MembershipUser currentUser = Membership.GetUser();
        // Determine the currently logged on user's UserId value
        Guid currentUserId = (Guid)currentUser.ProviderUserKey;
        // Assign the currently logged on user's UserId to the @UserId parameter
        e.Command.Parameters["@UserId"].Value = currentUserId;
    }

    protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
        string virtualFolder = "~/UserPics/";
        string physicalFolder = Server.MapPath(virtualFolder);
        string fileName = Guid.NewGuid().ToString();
        string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));

        string UserPicUrl = virtualFolder + fileName + extension;
        e.Command.Parameters["@UserPicUrl"].Value = UserPicUrl;
    }
}

目标:我正在尝试将图像存储在文件夹中并将相应的 Url 保存在数据库中。

将是一个很大的帮助。谢谢。

4

1 回答 1

0

SqlDataSource1_Inserting添加设置 UserId 值作为插入参数

e.Command.Parameters["@UserId"].Value =(Guid)Membership.GetUser().ProviderUserKey;
于 2013-06-14T08:58:25.063 回答