C# 当插入到我的FormView时。作为GUID的主键引发转换错误

C# 当插入到我的FormView时。作为GUID的主键引发转换错误,c#,sql,asp.net,formview,C#,Sql,Asp.net,Formview,例外情况如下: 不允许从数据类型sql_variant隐式转换为uniqueidentifier。使用CONVERT函数运行此查询 我真的不确定将Convert函数放在哪里。代码隐藏页是未触及的,由空的页面组成。大多数代码都是从工具箱中拖放的。aspx页面如下所示 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LocalDbExample.WebForm1" %

例外情况如下:

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

我真的不确定将Convert函数放在哪里。代码隐藏页是未触及的,由空的页面组成。大多数代码都是从工具箱中拖放的。aspx页面如下所示

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
Inherits="LocalDbExample.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
    <div>

        <asp:FormView ID="FormView1" runat="server" 
DataKeyNames="CustomerId" DataSourceID="customerSqlDataSource" 
AllowPaging="True">
            <EditItemTemplate>
                CustomerId:
                <asp:Label ID="CustomerIdLabel1" runat="server" Text='<%# 
Eval("CustomerId") %>' />
                <br />
                Name:
                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# 
Bind("Name") %>' />
                <br />
                Address:
                <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# 
Bind("Address") %>' />
                <br />
                City:
                <asp:TextBox ID="CityTextBox" runat="server" Text='<%# 
Bind("City") %>' />
                <br />
                State:
                <asp:TextBox ID="StateTextBox" runat="server" Text='<%# 
Bind("State") %>' />
                <br />
                PostalCode:
                <asp:TextBox ID="PostalCodeTextBox" runat="server" Text='<%# 
Bind("PostalCode") %>' />
                <br />
                Notes:
                <asp:TextBox ID="NotesTextBox" runat="server" Text='<%# 
Bind("Notes") %>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" 
CausesValidation="True" CommandName="Update" Text="Update" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
            <InsertItemTemplate>
                CustomerId:
                <asp:TextBox ID="CustomerIdTextBox" runat="server" Text='<%# 
Bind("CustomerId") %>' />
                <br />
                Name:
                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# 
Bind("Name") %>' />
                <br />
                Address:
                <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# 
Bind("Address") %>' />
                <br />
                City:
                <asp:TextBox ID="CityTextBox" runat="server" Text='<%# 
Bind("City") %>' />
                <br />
                State:
                <asp:TextBox ID="StateTextBox" runat="server" Text='<%# 
Bind("State") %>' />
                <br />
                PostalCode:
                <asp:TextBox ID="PostalCodeTextBox" runat="server" Text='<%# 
Bind("PostalCode") %>' />
                <br />
                Notes:
                <asp:TextBox ID="NotesTextBox" runat="server" Text='<%# 
Bind("Notes") %>' />
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" 
CausesValidation="True" CommandName="Insert" Text="Insert" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>
            <ItemTemplate>
                CustomerId:
                <asp:Label ID="CustomerIdLabel" runat="server" Text='<%# 
Eval("CustomerId") %>' />
                <br />
                Name:
                <asp:Label ID="NameLabel" runat="server" Text='<%# 
Bind("Name") %>' />
                <br />
                Address:
                <asp:Label ID="AddressLabel" runat="server" Text='<%# 
Bind("Address") %>' />
                <br />
                City:
                <asp:Label ID="CityLabel" runat="server" Text='<%# 
Bind("City") %>' />
                <br />
                State:
                <asp:Label ID="StateLabel" runat="server" Text='<%# 
Bind("State") %>' />
                <br />
                PostalCode:
                <asp:Label ID="PostalCodeLabel" runat="server" Text='<%# 
Bind("PostalCode") %>' />
                <br />
                Notes:
                <asp:Label ID="NotesLabel" runat="server" Text='<%# 
Bind("Notes") %>' />
                <br />
                <asp:LinkButton ID="EditButton" runat="server" 
CausesValidation="False" CommandName="Edit" Text="Edit" />
                &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" 
CausesValidation="False" CommandName="Delete" Text="Delete" />
                &nbsp;<asp:LinkButton ID="NewButton" runat="server" 
CausesValidation="False" CommandName="New" Text="New" />
            </ItemTemplate>
        </asp:FormView>

    </div>
    <asp:SqlDataSource ID="customerSqlDataSource" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT * FROM [Customers]" DeleteCommand="DELETE FROM 
[Customers] 
WHERE [CustomerId] = @CustomerId" InsertCommand="INSERT INTO [Customers] 
([CustomerId], [Name], [Address], [City], [State], [PostalCode], [Notes]) 
VALUES (@CustomerId, @Name, @Address, @City, @State, @PostalCode, @Notes)" 
UpdateCommand="UPDATE [Customers] SET [Name] = @Name, [Address] = @Address, 
[City] = @City, [State] = @State, [PostalCode] = @PostalCode, [Notes] = 
@Notes 
WHERE [CustomerId] = @CustomerId">
        <DeleteParameters>
            <asp:Parameter Name="CustomerId" Type="Object" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="CustomerId" Type="Object" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Address" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="PostalCode" Type="String" />
            <asp:Parameter Name="Notes" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Address" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="PostalCode" Type="String" />
            <asp:Parameter Name="Notes" Type="String" />
            <asp:Parameter Name="CustomerId" Type="Object" />
        </UpdateParameters>
    </asp:SqlDataSource>
</form>
</body>
</html>

我认为,由于CustomerID是自动生成的,因此需要将其从“插入参数”中删除:



数据库中CustomerId的数据类型是什么?UniqueIdentifier,在显示数据或向表中插入新数据时出现错误?这是一个SQL问题这是您需要的:
。GUID值与普通字符串不同,请正确处理它们。
 CREATE TABLE [dbo].[Customers] (
     [CustomerId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
     [Name]       VARCHAR (50)     NOT NULL,
     [Address]    VARCHAR (50)     NOT NULL,
     [City]       VARCHAR (50)     NOT NULL,
     [State]      VARCHAR (50)     NOT NULL,
     [PostalCode] VARCHAR (50)     NOT NULL,
     [Notes]      VARCHAR (MAX)    NULL,
     PRIMARY KEY CLUSTERED ([CustomerId] ASC)
 );
<InsertParameters>
    <asp:Parameter Name="CustomerId" Type="Object" />  <-- remove