- 追加された行はこの色です。
- 削除された行はこの色です。
閲覧総計:&counter(); (本日:&counter(today); 昨日:&counter(yesterday);)
ListViewでAccessデータに新たなデータを挿入する際、IDが「オートナンバ型」に
設定されていると、「挿入」ボタンをクリックした時点で「バリアント型ではない変
数に Null 値を代入しようとしました。 」のエラーが出て動きません。
この問題の解決方法を備忘目的で記載しておきます。(3.5) 09/09/13
【原因】
IDは「オートナンバ型」であるのに、自動生成されたInsertCommandは「ID」も更新
しようとしている事がエラー発生の原因。
InsertCommand="INSERT INTO [名簿] (&color(crimson){[ID],}; [名前], [年齢]) VALUES (&color(crimson){?,}; ?, ?)"
<InsertParameters>
&color(crimson){<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データに新たなデータを挿入する。
【稼働画面】
#ref(ListViewEditOpt.JPG);
「編集」機能が正しく動く様になった自動生成全コード
【ListViewEditOpt.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">
<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>
</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))">
<DeleteParameters>
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="original_名前" Type="String" />
<asp:Parameter Name="original_年齢" Type="Int16" />
</DeleteParameters>
<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])