1

I have page and SQL DB behind SQL table do not allowed duplicate names for addresses. I connect it to db and trying to implement try catch stamen it is working but fore some reason doesn't want to show error message

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> 

and .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;
        }
    }
}

Can you please point me where is the problem in try...catch, and maybe possible solution for it?

4
  • Does it enter the try catch? have you tried setting breakpooints? (you set DisplayMessage0 text but DisplayMessage to visible) Commented Aug 20, 2013 at 6:57
  • you dont want to show error message which is you assigning to your display message variable? Commented Aug 20, 2013 at 6:58
  • 1
    As a general rule you should never use a exception for expected program flow - they're slow and costly. Instead you should check for duplicates before inserting the record. Alternatively use a SQL MERGE statement to INSERT or UPDATE as required. Commented Aug 20, 2013 at 7:00
  • 1
    Dimitar Dimitrov answer the question i messed up with my DisplayMessage lables. Commented Aug 20, 2013 at 7:00

2 Answers 2

4

Well you're setting the message but you're never making it visible:

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

What I believe you want to do is:

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

Note: I understood from your question that the code enters the catch

Sign up to request clarification or add additional context in comments.

2 Comments

Thanx! I dint notice that i put different lable:)
@AndreyIvanov :) It happens :) Glad I could help. Cheers
1

Try this

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

to

catch(Exception ex)
    {
        DisplayMessage0.Text = "Record already exist.";
       DisplayMessage0.Visible = true;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.