Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
On the Button Save event, i want to check the name exist in the database or not. if exist then i want to show the confirmation message like "Same Name already exist! Do you want to continue?". if the user press "Yes" then Save all details to database otherwise No.

What I have tried:

I tried in google, but all shows the client side validation.
Posted
Updated 12-Oct-18 7:59am
Comments
summiya1 12-Oct-18 13:38pm    
Can you show us your piece of code ? you can try remote validation its server side validation using data Annotation if you are using MVC
Robymon 12-Oct-18 13:38pm    
i am not using MVC
summiya1 12-Oct-18 13:44pm    
then you can do it using change event on textbox onchange="UserAvailability()"
Robymon 12-Oct-18 13:54pm    
sorry, this is just an example mentioned in the question. i need to validate from database with different types of database validations and save only when the user clicks "Yes" button.
summiya1 12-Oct-18 14:00pm    
Check My Solution below

Check this code 
<pre><asp:TextBox ID="Name" runat="server" onchange="checkName()" />  
<pre><div id="checkName" runat="server">                             
<asp:Label ID="lblStatus" runat="server"></asp:Label></div>

<script src="Scripts/jquery-1.7.1.min.js"></script>  
 <script type="text/javascript">  
  
        function checkName() { //This function call on text change.             
            $.ajax({  
                type: "POST",  
                url: "URL Here", // this for calling the web method in cs code.  
                data: '{Name: "' + $("#<%=txtName.ClientID%>")[0].value + '" }', 
                contentType: "application/json; charset=utf-8",  
                dataType: "json",  
                success: OnSuccess,  
                failure: function (response) {  
                    alert(response);  
                }  
            });  
        }  
  
        // function OnSuccess  
        function OnSuccess(response) {  
            /// Show your message here bind with error label 
            }  
        }  
  
    </script>

Code Behind
[System.Web.Services.WebMethod]  
public static string CheckName(string Name)  
{  
    string retval = "";  
    SqlConnection con = new SqlConnection("data source");  
    con.Open();  
    SqlCommand cmd = new SqlCommand("select Name from tableName where Name=@Name",con);  
    cmd.Parameters.AddWithValue("@Name", Name);  
    SqlDataReader dr = cmd.ExecuteReader();  
    if (dr.HasRows)  
    {  
        retval = "true";            
    }  
    else  
    {  
        retval = "false";            
    }  
  
  return retval;  
}  
 
Share this answer
 
v2
Use Javascript code. put this code on button client click event.

JavaScript
confirm("Same Name already exist! Do you want to continue?");
 
Share this answer
 
Comments
Robymon 12-Oct-18 14:02pm    
i tried this, button when we click "Yes" it won't proceed to next code. it stops and go to page load event.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900