2017-07-21 7 views
0

をスロー例外がUNIQUEIDENTIFIERのデータ型sql_variant型からの暗黙的な変換が許可されていないFormViewに挿入するとき。 GUIDである主キーは、変換誤差を

を読み出します。この問合せを実行するには、CONVERTファンクションを使用します。

を変換する場所はわかりません。コードビハインドページは、空のPage_Loadで構成されています。ほとんどのコードはツールボックスからドラッグアンドドロップされていました。 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> 

データベースは、以下のように表記されています。得意先コードが自動生成されるので、

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) 
); 
+0

データベース内のCustomerIdのデータ型は何ですか? –

+0

UNIQUEIDTITIER – Roger

+0

そして、データの表示中または新しいデータをテーブルに挿入する際にエラーが発生していますか? –

答えて

0

私が思うに、あなたは「挿入パラメータ」からそれを削除する必要があります。

<InsertParameters> 
    <asp:Parameter Name="CustomerId" Type="Object" /> <-- remove 
関連する問題