閲覧総計:&counter();  (本日:&counter(today);  昨日:&counter(yesterday);)

 ListViewでAccessデータに新たなデータを挿入する際、IDが「オートナンバ型」に
設定されていると、「挿入」ボタンをクリックした時点で「バリアント型ではない変
数に Null 値を代入しようとしました。 」のエラーが出て動きません。
 この問題の解決方法を備忘目的で記載しておきます。(3.5)  09/09/13
 GridViewでAccessデータを「編集」する際、「オプティミスティック同時実行制御」
を設定すると編集決定時の「更新」ボタンを押した際、「1 つ以上の必要なパラメー
タの値が設定されていません。 」のエラーが発生し動かなくなります。
 この問題の解決方法を備忘目的で記載しておきます。  09/09/17

【原因】
 IDは「オートナンバ型」であるのに、自動生成されたInsertCommandは「ID」も更新
しようとしている事がエラー発生の原因。
InsertCommand="INSERT INTO [名簿] (&color(crimson){[ID],}; [名前], [年齢]) VALUES (&color(crimson){?,}; ?, ?)" 
 自動生成されたUpdateCommandの「?」の数は7つなのに対して、自動生成された
Parameter行がは5行と2行不足している事が原因。
 UpdateCommand="UPDATE [名簿] SET [名前] = ?, [年齢] = ? WHERE [ID] = ? AND (([名前] = ?) OR ([名前] IS NULL AND ? IS NULL)) AND (([年齢] = ?) OR ([年齢] IS NULL AND ? IS NULL))"

 <InsertParameters>
   &color(crimson){<asp:Parameter Name="ID" Type="Int32" />};
   <asp:Parameter Name="名前" Type="String" />
   <asp:Parameter Name="年齢" Type="Int16" />
 </InsertParameters>
 <UpdateParameters>
     <asp:Parameter Name="名前" Type="String" />
     <asp:Parameter Name="年齢" Type="Int16" />
     <asp:Parameter Name="original_ID" Type="Int32" />
     <asp:Parameter Name="original_名前" Type="String" />
     <asp:Parameter Name="original_年齢" Type="Int16" />
 </UpdateParameters>

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

InsertCommand="INSERT INTO [名簿] ([名前], [年齢]) VALUES (?, ?)" 
 <UpdateParameters>
     <asp:Parameter Name="名前" Type="String" />
     <asp:Parameter Name="年齢" Type="Int16" />
     <asp:Parameter Name="original_ID" Type="Int32" />
     <asp:Parameter Name="original_名前" Type="String" />
     &color(crimson){<asp:Parameter Name="original_名前" Type="String" />};
     <asp:Parameter Name="original_年齢" Type="Int16" />
     &color(crimson){<asp:Parameter Name="original_年齢" Type="Int16" />};
 </UpdateParameters>

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

Accessデータベース名:MyDB.mdb
Accessデータベース名:Access01.mdb
テーブル名:名簿
|~フィールド名|~データ型|~フィールドサイズ|
|ID (主キー)|オートナンバー型|長整数型|
|名前|テキスト型|50|
|年齢|数値型|整数型|

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

開発環境:VWD2008
サーバ:Windows Server2003 + .NET Framework3.5
開発環境:VWD2005 + Access2003
サーバ:ASP.NET2.0 + Access2003

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


【稼働画面】
#ref(ListViewEditOpt.JPG);
#ref(GridViewEditOpt.JPG);

「編集」機能が正しく動く様になった自動生成全コード
【ListViewEditOpt.aspx】[3.5]
【GridViewEditOpt.aspx】
 <%@ 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">
 
     'RowCommand():GridViewのボタンクリック時発生イベント:http://www.weblio.jp/content/GridView.RowCommand+%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88
     Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
         If e.CommandName = "Insert" Then
             AccessDataSource1.InsertParameters.Clear()
             For Each key As String In Request.Form.AllKeys
                 If key.Contains("$InsertName") Then
                     AccessDataSource1.InsertParameters.Add(New ControlParameter("名前", TypeCode.String, key, "Text"))
                 End If
                 If key.Contains("$InsertAge") Then
                     AccessDataSource1.InsertParameters.Add(New ControlParameter("年齢", TypeCode.String, key, "Text"))
                 End If
             Next
             AccessDataSource1.Insert()
         End If
     End Sub
 </script>
 
 <html xmlns="http://www.w3.org/1999/xhtml">
 <html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
     <title></title>
     <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">
             <ItemTemplate>
                 <tr style="">
                     <td>
                         <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編集" />
                     </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>
                         <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編集" />
                     </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>
                         <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編集" />
                     </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" 
             ConflictDetection="CompareAllValues" DataFile="~/App_Data/MyDB.mdb" 
             DeleteCommand="DELETE FROM [名簿] WHERE [ID] = ? AND (([名前] = ?) OR ([名前] IS NULL AND ? IS NULL)) AND (([年齢] = ?) OR ([年齢] IS NULL AND ? IS NULL))" 
             InsertCommand="INSERT INTO [名簿] ([ID], [名前], [年齢]) VALUES (?, ?, ?)" 
             OldValuesParameterFormatString="original_{0}" 
             SelectCommand="SELECT [ID], [名前], [年齢] FROM [名簿]" 
             UpdateCommand="UPDATE [名簿] SET [名前] = ?, [年齢] = ? WHERE [ID] = ? AND (([名前] = ?) OR ([名前] IS NULL AND ? IS NULL)) AND (([年齢] = ?) OR ([年齢] IS NULL AND ? IS NULL))">
         AccessでのGridView編集の稼働確認 09/09/17<br />
         (「オプティミスティック同時実行制御」を設定)<br />
         &nbsp;<br />
         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
             DataKeyNames="ID" DataSourceID="AccessDataSource1" EmptyDataText="表示するデータ レコードがありません。"
             ForeColor="#333333" GridLines="None">
             <RowStyle BackColor="#E3EAEB" />
             <Columns>
                 <asp:CommandField ButtonType="Button" ShowEditButton="True" />
                 <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
                 <asp:BoundField DataField="名前" HeaderText="名前" SortExpression="名前" />
                 <asp:BoundField DataField="年齢" HeaderText="年齢" SortExpression="年齢" />
             </Columns>
             <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
             <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
             <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
             <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
             <EditRowStyle BackColor="#7C6F57" />
             <AlternatingRowStyle BackColor="White" />
         </asp:GridView>
         <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data\Access01.mdb"
             DeleteCommand="DELETE FROM [名簿] WHERE [ID] = ? AND (([名前] = ?) OR ([名前] IS NULL AND ? IS NULL)) AND (([年齢] = ?) OR ([年齢] IS NULL AND ? IS NULL))"
             InsertCommand="INSERT INTO [名簿] ([ID], [名前], [年齢]) VALUES (?, ?, ?)"
             SelectCommand="SELECT [ID], [名前], [年齢] FROM [名簿]"
             UpdateCommand="UPDATE [名簿] SET [名前] = ?, [年齢] = ? WHERE [ID] = ? AND (([名前] = ?) OR ([名前] IS NULL AND ? IS NULL)) AND (([年齢] = ?) OR ([年齢] IS NULL AND ? IS NULL))"
             ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}">
             <DeleteParameters>
                 <asp:Parameter Name="original_ID" Type="Int32" />
                 <asp:Parameter Name="original_名前" Type="String" />
                 <asp:Parameter Name="original_年齢" Type="Int16" />
             </DeleteParameters>
             <InsertParameters>
                 <asp:Parameter Name="ID" Type="Int32" />
                 <asp:Parameter Name="名前" Type="String" />
                 <asp:Parameter Name="年齢" Type="Int16" />
             </InsertParameters>
             <UpdateParameters>
                 <asp:Parameter Name="名前" Type="String" />
                 <asp:Parameter Name="年齢" Type="Int16" />
                 <asp:Parameter Name="original_ID" Type="Int32" />
                 <asp:Parameter Name="original_名前" Type="String" />
                 <asp:Parameter Name="original_名前" Type="String" />
                 <asp:Parameter Name="original_年齢" Type="Int16" />
                 <asp:Parameter Name="original_年齢" Type="Int16" />
             </UpdateParameters>
             <InsertParameters>
                 <asp:Parameter Name="ID" Type="Int32" />
                 <asp:Parameter Name="名前" Type="String" />
                 <asp:Parameter Name="年齢" Type="Int16" />
             </InsertParameters>
         </asp:AccessDataSource>
     
     </div>
     </form>
 </body>
 </html>


【参考にしたページ】
  該当なし


#comment_nospam
#vote(参考になった[0],ふつう[0],参考にならなかった[0])


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS