我有頁面和SQL數據庫後面的SQL表不允許重複地址名稱。 我把它連接到數據庫,並試圖實現嘗試捕捉雄蕊它是工作,但前某些原因不希望顯示錯誤消息C#和ASP中的異常處理.Net
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;
}
}
}
你能指出我在哪裏嘗試......趕上問題,也許可能的解決辦法嗎?
是否進入嘗試捕捉?你有沒有試過設置breakpooints? (你設置DisplayMessage0文本但DisplayMessage爲可見) – Sayse
你不想顯示錯誤信息,你分配給你的顯示消息變量? – SeeSharp
作爲一般規則,您絕對不應該爲預期的程序流程使用異常 - 它們速度慢且代價高昂。相反,您應該在插入記錄之前檢查重複項。或者,根據需要使用SQL MERGE語句來執行INSERT或UPDATE。 – Keith