C#和ASP.Net中的异常处理
本文关键字:异常处理 Net ASP | 更新日期: 2023-09-27 18:28:27
我有一个页面,SQL表后面的SQL DB不允许地址有重复的名称。我把它连接到数据库,并试图实现try-catch starm,它正在工作,但由于某种原因不想显示错误消息
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;
}
}
}
你能告诉我试…的问题在哪里吗。。。捕获,以及可能的解决方案?
您正在设置消息,但从未使其可见:
DisplayMessage0.Text = "Record already exist.";
// this is a different field ...
DisplayMessage.Visible = true;
我相信你想做的是:
DisplayMessage0.Text = "Record already exist.";
DisplayMessage0.Visible = true;
注意:我从您的问题中了解到,代码进入catch
试试这个
catch
{
DisplayMessage0.Text = "Record already exist.";
DisplayMessage.Visible = true;
}
至
catch(Exception ex)
{
DisplayMessage0.Text = "Record already exist.";
DisplayMessage0.Visible = true;
}