Click here to Skip to main content
15,914,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i am inserting the data the duplicates not allowed and show error message like already existed,if not duplicate "insertion successfully completed".please tell me how can i do this

What I have tried:

JavaScript
$.ajax({
           type: "POST",
           url: "WebForm1.aspx/AddRecord",
           data: '{"A": "' + a + '"}',
           contentType: "application/json; charset=utf-8",
           dataType: "json"
)}

webmethod
VB
Public Shared Sub AddRecord(A As String)
       Dim con As New SqlConnection(ConfigurationManager.AppSettings("test").ToString())
       Try
           Dim sqlCmd = New SqlCommand("select Top 1 A from MDT_MOBILE_FEATURES_SETTINGS WHERE A = @a")

           sqlCmd.Parameters.Add(New SqlParameter("@a", a))

           sqlCmd.CommandType = CommandType.Text
           sqlCmd.Connection = con
           con.Open()
           Dim result = sqlCmd.ExecuteScalar()

           If result IsNot Nothing Then
               Throw New Exception(String.Format("A {0} already existed.", FName))

           End If

           Dim sqlInsertCommand = New SqlCommand("INSERT MDT_MOBILE_FEATURES_SETTINGS(A) VALUES(@a)")

           sqlInsertCommand.Parameters.AddWithValue("@a", a)
           sqlInsertCommand.Connection = con


           sqlInsertCommand.ExecuteNonQuery()

           con.Close()
       Catch ex As Exception
           Console.WriteLine("Exception caught: {0}", ex.Message)
       End Try
   End Sub
Posted
Updated 16-Jan-17 18:59pm
v2
Comments
Suvendu Shekhar Giri 16-Jan-17 23:25pm    
So what is the issue with the tried code?
Devaraneni Laxman Rao 17-Jan-17 0:40am    
i want to display the error message

Web methods OOTB handles error at client you just need to throw your error message.

Refer Sample Code for your reference below.

At Web Server side web method

C#
[WebMethod]
        public List<Models.SubOrdinates> getXXXX(string UserId)
        {
            try
            {
                    DataTable dt = bal.getSubXXXXOrdinates(UserId).Tables[0];
                    List<Models.SubXXXXs> SubXXXX = new List<Models.SubXXXXs>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        DateTime dtPeriodStart = DateTime.MinValue;
                        DateTime dtPeriodEnd = DateTime.MinValue;
                        if (DateTime.TryParse(dr["PeriodStart"].ToString(), out dtPeriodStart) && DateTime.TryParse(dr["PeriodEnd"].ToString(), out dtPeriodEnd))
                        {
                            SubOrdinates.Add(new Models.SubOrdinates()
                            {
                                PeriodStart = dtPeriodStart,
                                PeriodEnd = dtPeriodEnd
                            });
                        }
                    }
                    return SubOrdinates;
            }
            catch (Exception ex)
            {
                string ErrorCode = util.WriteLog(ex.Message);
                throw new Exception("An Error has occured with Error Code : " + ErrorCode + " Please contact support team");
            }
        }




At Client
JavaScript
$.ajax({
        type: "POST",
        url: "wsGetData.asmx/getXXXXXX",
        data: JSON.stringify({ UserId: userId }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: CallBackFunction,
        error: function (xhr, status, error) {
            var exception = JSON.parse(xhr.responseText);
            ErrorMessageModel('Error', exception.Message);
        },
        failure: function (response) {
            ErrorMessageModel('Error', response.d);
        }
    });
 
Share this answer
 
v2
Comments
Devaraneni Laxman Rao 17-Jan-17 0:39am    
in vb.net
util.writelog() is not working
Singh Vijay Kumar 17-Jan-17 6:17am    
this should work for you,
throw new Exception("An Error has occured with Error : " + ex.Message + " Please contact support team");
(Format may be different for vb.net)
Code given by me was sample code and build for our environment.
util.writeLog() is mine custom class
well, first of all...

JavaScript
$.ajax({
    type: "POST",
    url: "WebForm1.aspx/AddRecord",
    data: {A: a},
    contentType: "application/json; charset=utf-8",
    dataType: "json"
}).done(function(response){
   // do something with the  response
});
 
Share this answer
 
See if this can help, Code is in C# but it should be able to guide you.
Exception Handling in WebAPI[^]
 
Share this answer
 

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