閲覧総計:4561  (本日:1  昨日:1)

 ListViewでAccessデータに新たなデータを挿入する際、IDが「オートナンバ型」に
設定されていると、「挿入」ボタンをクリックした時点で「バリアント型ではない変
数に Null 値を代入しようとしました。 」のエラーが出て動きません。
 この問題の解決方法を備忘目的で記載しておきます。(3.5)  09/09/13

【原因】
 IDは「オートナンバ型」であるのに、自動生成されたInsertCommandは「ID」も更新
しようとしている事がエラー発生の原因。
InsertCommand="INSERT INTO [名簿] ([ID], [名前], [年齢]) VALUES (?, ?, ?)"

 <InsertParameters>
   <asp:Parameter Name="ID" Type="Int32" />
   <asp:Parameter Name="名前" Type="String" />
   <asp:Parameter Name="年齢" Type="Int16" />
 </InsertParameters>

【解決方法】
 InsertCommandが「ID」を更新しようとしている所の情報を削除してやる。
今回の場合は、
 nsertCommandの[ID],と?,の2ヶ所を削除
 <asp:Parameter Name="ID" Type="Int32" />の行を削除
でエラーは解消し、「挿入」機能が正しく動作する様になった。

InsertCommand="INSERT INTO [名簿] ([名前], [年齢]) VALUES (?, ?)"

 <InsertParameters>
   <asp:Parameter Name="名前" Type="String" />
   <asp:Parameter Name="年齢" Type="Int16" />
 </InsertParameters>

Accessデータベース名:MyDB.mdb
テーブル名:名簿

フィールド名データ型フィールドサイズ
ID (主キー)オートナンバー型長整数型
名前テキスト型50
年齢数値型整数型

※ASP.NET3.5ではAccess2007のDBファイル形式(*.accdb)は利用できない模様。
DBファイルは「Acces2002-2003形式(*.mdb)」で保存してASP.NET3.5で利用する。

開発環境:VWD2008
サーバ:Windows Server2003 + .NET Framework3.5

【機能】ListViewの「挿入」ボタンでAccessデータに新たなデータを挿入する。

【稼働画面】

ListViewAdd.JPG

「挿入」機能が正しく動く様になった自動生成全コード
【ListViewAdd.aspx】[3.5]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        ListViewでのAccessデータの挿入機能の確認   09/09/13<br />
        (ID:オートナンバー型)<br />
        <br />
        <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" 
            DataSourceID="AccessDataSource1" InsertItemPosition="LastItem">
            <ItemTemplate>
                <tr style="">
                    <td>
                    </td>
                    <td>
                        <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="名前Label" runat="server" Text='<%# Eval("名前") %>' />
                    </td>
                    <td>
                        <asp:Label ID="年齢Label" runat="server" Text='<%# Eval("年齢") %>' />
                    </td>
                </tr>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <tr style="">
                    <td>
                    </td>
                    <td>
                        <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="名前Label" runat="server" Text='<%# Eval("名前") %>' />
                    </td>
                    <td>
                        <asp:Label ID="年齢Label" runat="server" Text='<%# Eval("年齢") %>' />
                    </td>
                </tr>
            </AlternatingItemTemplate>
            <EmptyDataTemplate>
                <table runat="server" style="">
                    <tr>
                        <td>
                            データは返されませんでした。</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="挿入" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="クリア" />
                    </td>
                    <td>
                        &nbsp;</td>
                    <td>
                        <asp:TextBox ID="名前TextBox" runat="server" Text='<%# Bind("名前") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="年齢TextBox" runat="server" Text='<%# Bind("年齢") %>' />
                    </td>
                </tr>
            </InsertItemTemplate>
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                                <tr runat="server" style="">
                                    <th runat="server">
                                    </th>
                                    <th runat="server">
                                        ID</th>
                                    <th runat="server">
                                        名前</th>
                                    <th runat="server">
                                        年齢</th>
                                </tr>
                                <tr ID="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="">
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <EditItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                            Text="キャンセル" />
                    </td>
                    <td>
                        <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="名前TextBox" runat="server" Text='<%# Bind("名前") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="年齢TextBox" runat="server" Text='<%# Bind("年齢") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
            <SelectedItemTemplate>
                <tr style="">
                    <td>
                    </td>
                    <td>
                        <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="名前Label" runat="server" Text='<%# Eval("名前") %>' />
                    </td>
                    <td>
                        <asp:Label ID="年齢Label" runat="server" Text='<%# Eval("年齢") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/MyDB.mdb" DeleteCommand="DELETE FROM [名簿] WHERE [ID] = ?" 
            InsertCommand="INSERT INTO [名簿] ([名前], [年齢]) VALUES (?, ?)" 
            SelectCommand="SELECT [ID], [名前], [年齢] FROM [名簿]" 
            UpdateCommand="UPDATE [名簿] SET [名前] = ?, [年齢] = ? WHERE [ID] = ?">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="名前" Type="String" />
                <asp:Parameter Name="年齢" Type="Int16" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="名前" Type="String" />
                <asp:Parameter Name="年齢" Type="Int16" />
            </InsertParameters>
        </asp:AccessDataSource>
        <br />
    
    </div>
    </form>
</body>
</html>

【参考にしたページ】
1.AccsessデータにListViewで挿入できない 09/09/13
http://social.msdn.microsoft.com/Forums/ja-JP/aspnetja/thread/c5706835-7069-4073-b3ca-791767f73432


選択肢 投票
参考になった 0  
ふつう 0  
参考にならなかった 2  

添付ファイル: fileListViewAdd.JPG 589件 [詳細]

トップ   編集 凍結解除 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-03-02 (木) 20:01:53 (413d)