閲覧総計:5103 (本日:1 昨日:0)
GridViewにはデータを挿入する機能がありません。Access DB環境で、GridView
にデータを挿入する機能を↓のページを参考に作成してみました。
http://dotnetfan.org/blogs/dotnetfanblog/articles/632.aspx
また、ASP.NETが自動生成したコードは、データテーブルのIDが「オートナンバ型」
に設定されていると、「挿入」ボタンをクリックした時点で「バリアント型ではない
変数に Null 値を代入しようとしました。 」のエラーが出て動かない不具合があり
ます。この問題の解決方法も備忘目的で記載しておきます。 09/09/17
開発環境:VWD2005 + Access2003
サーバ:ASP.NET2.0 + Access2003
【機能】GridViewに新設した「挿入」ボタンでAccessデータに新たなデータを挿入する。
【稼働画面】
Accessデータベース名:MyDB.mdb
テーブル名:名簿
フィールド名 | データ型 | フィールドサイズ |
---|---|---|
ID (主キー) | オートナンバー型 | 長整数型 |
名前 | テキスト型 | 50 |
年齢 | 数値型 | 整数型 |
※VWD2005、ASP.NET2.0環境ではAccess2007のDBファイル形式(*.accdb)は利用できな
い模様。DBファイルは「Acces2002-2003形式(*.mdb)」で保存して利用する。
コントロールの設定値
コントロール | プロパティ | 値 | コメント |
---|---|---|---|
編集フィールド | ButtonType | Button | TemplateFieldに変換する前に設定 |
テンプレート | コントロール | プロパティ | 値 | コメント |
---|---|---|---|---|
Column[0] Footer | Button | CommandName | Insert | |
Text | 挿入 | |||
Column[2] Footer | TextBox | (ID) | InsertName | |
Width | 80 | |||
Column[3] Footer | TextBox | (ID) | InsertAge | |
Width | 30 |
【自動生成コードの不具合】
データテーブルのIDが「オートナンバ型」に設定されていると、「挿入」ボタンを
クリックした時点で「バリアント型ではない変数に Null 値を代入しようとしました。」のエラーが出て動かない。
【原因】
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>
「挿入」機能が正しく動く様になった自動生成全コード
【GridViewInsert.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" > <head runat="server"> <title>無題のページ</title> </head> <body> <form id="form1" runat="server"> <div> AccessでのGridView挿入の稼働確認 09/09/16<br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="AccessDataSource1" EmptyDataText="表示するデータ レコードがありません。" ShowFooter="True" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="更新" /> <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="キャンセル" /> </EditItemTemplate> <FooterTemplate> <asp:Button ID="Button3" runat="server" CommandName="Insert" Text="挿入" /> </FooterTemplate> <ItemTemplate> <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="編集" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" /> <asp:TemplateField HeaderText="名前" SortExpression="名前"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("名前") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="InsertName" runat="server" Width="80px"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("名前") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="年齢" SortExpression="年齢"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("年齢") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="InsertAge" runat="server" Width="30px"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("年齢") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data\Access01.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> <InsertParameters> <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="ID" Type="Int32" /> </UpdateParameters> </asp:AccessDataSource> <br /> </div> </form> </body> </html>
【参考にしたページ】
1.GridViewからデータを追加する 06/03/29
http://dotnetfan.org/blogs/dotnetfanblog/articles/632.aspx