Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在文件系统中插入图像文件_C#_Asp.net_Sql_Listview_Guid - Fatal编程技术网

C# 在文件系统中插入图像文件

C# 在文件系统中插入图像文件,c#,asp.net,sql,listview,guid,C#,Asp.net,Sql,Listview,Guid,我得到这个错误: 不允许从数据类型sql_variant隐式转换为uniqueidentifier。使用CONVERT函数运行此查询 矿山代码如下: 源代码: <asp:ListView ID="ListView1" runat="server" DataKeyNames="UserId" DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" > <InsertItemTemplate>

我得到这个错误:

不允许从数据类型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>
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保存在数据库中


非常有帮助。谢谢。

SqlDataSource1\u插入
将set UserId值添加为插入参数

e.Command.Parameters["@UserId"].Value =(Guid)Membership.GetUser().ProviderUserKey;

您的表中是否有uniqueidentifier列?您在
InsertParameters
中给出的类型是什么?应该是
DbType=“Guid”
@Damith,谢谢您的回答,先生,我表中的uniqueidentifier是UserId,我给的类型是object。您能用设置UserId和type参数值的代码更新问题吗?@Damith;就这么做了。请看一看。
e.Command.Parameters["@UserId"].Value =(Guid)Membership.GetUser().ProviderUserKey;