質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

ASP.NET

ASP.NETは動的なWebサイトやWebアプリケーション、そしてWebサービスを構築出来るようにする為、Microsoftによって開発されたウェブアプリケーション開発フレームワークです。

Q&A

解決済

1回答

2590閲覧

ASP.NETでのwebformからのデータベースへの新規登録 コード添削依頼

sanezane

総合スコア91

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

ASP.NET

ASP.NETは動的なWebサイトやWebアプリケーション、そしてWebサービスを構築出来るようにする為、Microsoftによって開発されたウェブアプリケーション開発フレームワークです。

0グッド

1クリップ

投稿2018/03/31 14:40

今回、仕事でASP.NETを扱うことになり勉強中です。
まずはざっくりwebformを作ってみようと思い手をつけてみたのですが、入力したものをデータベースに新たに登録する処理ができません。具体的にはキャプチャの画面からtextboxに名前や誕生日を入力して送信ボタンを押してもなにも反応がありません。visualstudioを扱うのも初めてなのでこういったエラーの対処に対しての対応についてもご教授いただければ大変助かります。
余計なコードが入っていると思いますが、ご容赦ください。

※登録ボタンは関係ありません
イメージ説明

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="■■■.aspx.cs" Inherits="WebApplication1.■■■" %> <!DOCTYPE html> <html xmlns="http://■■■xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:Button ID="Button1" runat="server" Text="登録" /> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" ClientIDMode="AutoID" DataKeyNames="Id" DataSourceID="SqlDataSource1" EmptyDataText="表示するデータ レコードがありません。" ShowFooter="True" OnRowCommand="GridView1_RowCommand" > <Columns> <asp:TemplateField HeaderText="Id" SortExpression="Id"> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Id") %>'></asp:Label> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="name" SortExpression="name"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="InsertUserName" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></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="InsertBirthday" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("誕生日") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="性別コード" SortExpression="性別コード"> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("性別コード") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("性別コード") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="shedule" SortExpression="shedule"> <EditItemTemplate> <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("shedule") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("shedule") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton> &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="キャンセル"></asp:LinkButton> </EditItemTemplate> <FooterTemplate> <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Inseart">送信</asp:LinkButton> </FooterTemplate> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="編集"></asp:LinkButton> &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="削除"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#FFF1D4" /> <SortedAscendingHeaderStyle BackColor="#B95C30" /> <SortedDescendingCellStyle BackColor="#F1E5CE" /> <SortedDescendingHeaderStyle BackColor="#93451F" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Prefecture] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Prefecture] ([name], [誕生日], [性別コード], [shedule]) VALUES (@name, @誕生日, @性別コード, @shedule)" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT [Id], [name], [誕生日], [性別コード], [shedule] FROM [Prefecture]" UpdateCommand="UPDATE [Prefecture] SET [name] = @name, [誕生日] = @誕生日, [性別コード] = @性別コード, [shedule] = @shedule WHERE [Id] = @Id"> <DeleteParameters> <asp:Parameter Name="Id" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter DbType="Date" Name="誕生日" /> <asp:Parameter Name="性別コード" Type="String" /> <asp:Parameter Name="shedule" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter DbType="Date" Name="誕生日" /> <asp:Parameter Name="性別コード" Type="String" /> <asp:Parameter Name="shedule" Type="String" /> <asp:Parameter Name="Id" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class ■■■ : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if(e.CommandName == "Insert") { SqlDataSource1.InsertParameters.Clear(); foreach(string key in Request.Form.AllKeys) { if (key.Contains("$InsertUserName")) SqlDataSource1.InsertParameters.Add(new ControlParameter("name",TypeCode.String,key,"Text")); if (key.Contains("$InsertBirthday")) ; SqlDataSource1.InsertParameters.Add(new ControlParameter("誕生日",TypeCode.String,key,"Text")); } SqlDataSource1.Insert(); } } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/04/01 01:28

まずは質問者さんの方で Visual Studio を使ってデバッグして、問題がどこにあるのか、ある程度切り分けしてください。イベントハンドラ内の最初の行にブレークポイントを設定して、そこに制御が飛んでくるかだけでも調べることは容易にできるはず。丸投げは歓迎されません。
sanezane

2018/04/01 03:06

きちんとデバッグしていったらパラメータの名前が間違っていることに気づきました。コメントいただきありがとうございました。
guest

回答1

0

自己解決

パラメータの値が間違っていました。
些細な解決方法で大変恐縮ですが、解決しました。
今回のことを通じて正しいデバッグ方法を学ぶきっかけになりましたのでそれは一つ収穫かと思いました。
コメントいただきました方や閲覧していただいた方にお礼申し上げます

投稿2018/04/01 03:09

sanezane

総合スコア91

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問