1

我在 SQL 表后面有页面和 SQL 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 中的问题在哪里,以及可能的解决方案?

4

2 回答 2

4

好吧,您正在设置消息,但永远不会使其可见:

DisplayMessage0.Text = "Record already exist.";
// this is a different field ...
DisplayMessage.Visible = true;

我相信你想做的是:

DisplayMessage0.Text = "Record already exist.";
DisplayMessage0.Visible = true;

注意:我从您的问题中了解到,代码输入catch

于 2013-08-20T06:57:00.037 回答
1

尝试这个

catch 
    {
        DisplayMessage0.Text = "Record already exist.";
       DisplayMessage.Visible = true;
    }

catch(Exception ex)
    {
        DisplayMessage0.Text = "Record already exist.";
       DisplayMessage0.Visible = true;
    }
于 2013-08-20T07:00:34.233 回答