SQL テーブルの背後にページと SQL DB があり、アドレスの重複した名前は許可されていません。私はそれをdbに接続し、それが機能しているtry catch stamenを実装しようとしていますが、何らかの理由でエラーメッセージを表示したくありません
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdmAddStreet.aspx.cs" Inherits="AdmAddStreet" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
add new address<br />
<asp:TextBox ID="txbAddress" runat="server" Height="22px" Width="348px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" Width="353px" />
<br />
<br />
<asp:Label ID="DisplayMessage" runat="server" style="color: #FF0000" Visible="false" />
<asp:Label ID="DisplayMessage0" runat="server" style="color: #FF0000" Visible="false" />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TicketsConnectionString %>" DeleteCommand="DELETE FROM [Streets] WHERE [StrID] = @original_StrID" InsertCommand="INSERT INTO [Streets] ([StrName]) VALUES (@StrName)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Streets]" UpdateCommand="UPDATE [Streets] SET [StrName] = @StrName WHERE [StrID] = @original_StrID">
<DeleteParameters>
<asp:Parameter Name="original_StrID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="StrName" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="StrName" Type="String" />
<asp:Parameter Name="original_StrID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="StrID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="StrName" HeaderText="StrName" SortExpression="StrName" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
および.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class AdmAddStreet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
string connectionString = ConfigurationManager.ConnectionStrings["TicketsConnectionString"].ConnectionString;
protected void Button1_Click1(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("InsertIntoStreets", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@StrName", SqlDbType.NVarChar).Value = txbAddress.Text;
cmd.Parameters["@StrName"].Value = txbAddress.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
DisplayMessage.Text = "Record inserted.";
DisplayMessage.Visible = true;
GridView1.DataBind();
txbAddress.Text = string.Empty;
}
catch
{
DisplayMessage0.Text = "Record already exist.";
DisplayMessage.Visible = true;
}
}
}
try...catch のどこに問題があるのか、それに対する可能な解決策を教えてください。