locked
Message Box to show duplicated Records RRS feed

  • Question

  • User-1120028727 posted

    Hello,

    I have a  C# code with an INSERT SQL statement. the table at the backend has a primary key. i would like the program to display a message box that indicates the record exists once the user inserts something similar.

    At the moment it keeps throwing an exception.

    private void btnsave_Click(object sender, RoutedEventArgs e)
    {
    SqlConnection con = new SqlConnection(cs);
    con.Open();
    SqlCommand cmd = new SqlCommand("Insert into Passengers values (@ID_Number, @Name, @From, @To, @Phone, @Time_Out, @Date, @Price)", con);
    cmd.Parameters.AddWithValue("ID_Number", txtid.Text);
    cmd.Parameters.AddWithValue("Name", txtname.Text);
    cmd.Parameters.AddWithValue("From", txtfrom.Text);
    cmd.Parameters.AddWithValue("To", txt_to.Text);
    cmd.Parameters.AddWithValue("Phone", txtphone.Text);
    cmd.Parameters.AddWithValue("Time_Out", txt_time.Text);
    cmd.Parameters.AddWithValue("Date", txtdate.Text);
    cmd.Parameters.AddWithValue("Price", txtprice.Text);
    cmd.ExecuteNonQuery();

    txtid.Text = "";
    txtname.Text = "";
    txtfrom.Text = "";
    txt_to.Text = "";
    txtphone.Text = "";
    txt_time.Text = "";
    txtdate.Text = "";
    txtprice.Text = "";
    }
    }
    }

    Saturday, April 21, 2018 1:38 PM

Answers

  • User-369506445 posted

    hi

    you can use try catch block for get your error and handle it and generate your custom message .

    please try below  code :

    protected void btnsave_Click_Click(object sender, EventArgs e)
            {
                SqlConnection con=null;
                try
                {
                    con = new SqlConnection(cs);
    
                    con.Open();
                    SqlCommand cmd =
                        new SqlCommand(
                            "Insert into Passengers values (@ID_Number, @Name, @From, @To, @Phone, @Time_Out, @Date, @Price)",
                            con);
                    cmd.Parameters.AddWithValue("ID_Number", txtid.Text);
                    cmd.Parameters.AddWithValue("Name", txtname.Text);
                    cmd.Parameters.AddWithValue("From", txtfrom.Text);
                    cmd.Parameters.AddWithValue("To", txt_to.Text);
                    cmd.Parameters.AddWithValue("Phone", txtphone.Text);
                    cmd.Parameters.AddWithValue("Time_Out", txt_time.Text);
                    cmd.Parameters.AddWithValue("Date", txtdate.Text);
                    cmd.Parameters.AddWithValue("Price", txtprice.Text);
                    cmd.ExecuteNonQuery();
    
                    txtid.Text = "";
                    txtname.Text = "";
                    txtfrom.Text = "";
                    txt_to.Text = "";
                    txtphone.Text = "";
                    txt_time.Text = "";
                    txtdate.Text = "";
                    txtprice.Text = "";
                }
                catch (Exception ex)
                {
                    //  ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "window.alert('')", true);
                    ClientScript.RegisterStartupScript(this.GetType(), "ViewSecond",
    
                        "<script language=\"javascript\">alert(\"this number is duplicate\");</script>");
    
                }
                finally
                {
                   con.Close(); 
                }
                
            }

    I hope it can be helpful

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 21, 2018 2:48 PM

All replies

  • User-369506445 posted

    hi

    you can use try catch block for get your error and handle it and generate your custom message .

    please try below  code :

    protected void btnsave_Click_Click(object sender, EventArgs e)
            {
                SqlConnection con=null;
                try
                {
                    con = new SqlConnection(cs);
    
                    con.Open();
                    SqlCommand cmd =
                        new SqlCommand(
                            "Insert into Passengers values (@ID_Number, @Name, @From, @To, @Phone, @Time_Out, @Date, @Price)",
                            con);
                    cmd.Parameters.AddWithValue("ID_Number", txtid.Text);
                    cmd.Parameters.AddWithValue("Name", txtname.Text);
                    cmd.Parameters.AddWithValue("From", txtfrom.Text);
                    cmd.Parameters.AddWithValue("To", txt_to.Text);
                    cmd.Parameters.AddWithValue("Phone", txtphone.Text);
                    cmd.Parameters.AddWithValue("Time_Out", txt_time.Text);
                    cmd.Parameters.AddWithValue("Date", txtdate.Text);
                    cmd.Parameters.AddWithValue("Price", txtprice.Text);
                    cmd.ExecuteNonQuery();
    
                    txtid.Text = "";
                    txtname.Text = "";
                    txtfrom.Text = "";
                    txt_to.Text = "";
                    txtphone.Text = "";
                    txt_time.Text = "";
                    txtdate.Text = "";
                    txtprice.Text = "";
                }
                catch (Exception ex)
                {
                    //  ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "window.alert('')", true);
                    ClientScript.RegisterStartupScript(this.GetType(), "ViewSecond",
    
                        "<script language=\"javascript\">alert(\"this number is duplicate\");</script>");
    
                }
                finally
                {
                   con.Close(); 
                }
                
            }

    I hope it can be helpful

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 21, 2018 2:48 PM
  • User-1716253493 posted

    you can check by

    "select * from Passengers where ID_Number=@ID_Number" or ""select count(*) from Passengers where ID_Number=@ID_Number" 

    based the result then insert or show message

    Saturday, April 21, 2018 3:44 PM