User already Exists

 <script type="text/javascript">
        $(document).ready(function(){$('#<%=txtimgtitle .ClientID%>').keyup(function(){var userName=$(this).val();if(userName.length>=1){$.ajax({url:'../webservices/replica.asmx/UserNameExists',method:'post',data:{userName:userName},dataType:'json',success:function(data){var divElement=$('#<%=errormsg .ClientID%>');if(data.UserNameInUse)
            {divElement.text(userName+' already exists');divElement.css('color','orange')}
            else{divElement.text(userName);divElement.css('color','green')}},error:function(err){alert(err)}})}})});
    </script>
        <script type="text/javascript">
        var employee = {};
        var alertmsg = $("#alerttext").val();
        $(document).ready(function () {

            $('#<%=btninsertq.ClientID%>').click(function () {
                var userName = $('#<%=txtimgtitle .ClientID%>').val();
                if (($('#<%=txtimgtitle.ClientID%>').val().length == 0)||($('#<%=txtdescription .ClientID%>').val().length==0)) {
                    $("#errmsgtitle").html("Required");
                    if (($('#<%=txtimgtitle.ClientID%>').val().length > 0)) {
                        $("#errmsgtitle").html(" ");
                    }
                    $("#errmsgdesc").html("Required");
                    if (($('#<%=txtdescription.ClientID%>').val().length > 0)) {
                        $("#errmsgdesc").html(" ");
                    }
                }
              else if (userName.length >= 1) {
                    $.ajax({
                        url: '../webservices/replica.asmx/UserNameExists',
                        method: 'post',
                        data: { userName: userName },
                        dataType: 'json',
                        success: function (data) {
                            var divElement = $('#<%=errormsg .ClientID%>');
                            if (data.UserNameInUse)
                            {
                                divElement.text(/*data.UserName*/userName + ' already exists');
                                divElement.css('color', 'orange');
                            }
                            else
                            {
                                employee.CATEGORY_NAME = $('#<%=txtimgtitle .ClientID%>').val();
                                employee.CATEGORY_DESCRIPTION = $('#<%=txtdescription.ClientID%>').val();
                                $.ajax({
                                    url: '../webservices/replica.asmx/Addcategory',
                                    method: 'post',
                                    data: '{emp: ' + JSON.stringify(employee) + '}',
                                    contentType: "application/json; charset=utf-8",
                                    success: function () {

                                        $("#alertjquery").show();
                                        $("#alerttextjquery").html("Added Successfully");
                                        $('#<%=txtimgtitle .ClientID%>').val('');
                                        $('#<%=txtdescription.ClientID%>').val('');
                                     
                                        window.setTimeout(function () {
                                            $("#alertjquery").fadeTo(500, 0).slideUp(500, function () {
                                                $(this).remove();
                                                window.location.href = "category-list.aspx";

                                            });

                                        }, 4000);
                                     

                                    },
                                    error: function (err) {
                                        alert(err);
                                    }
                                });
                            }
                        },
                        error: function (err) {
                            alert(err);
                        }
                    });
                }
            });
        });
    </script>









 [WebMethod]
    public void UserNameExists(string userName)
    {
        bool userNameInUse = false;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("ASP_CHECK_CATEGORY_EXISTS", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName = "@CATEGORY_NAME",
                Value = userName
            });
            con.Open();
            userNameInUse = Convert.ToBoolean(cmd.ExecuteScalar());
        }

        ImageEntities entities = new ImageEntities();
        entities.CATEGORY_NAME = userName;
        entities.UserNameInUse = userNameInUse;

        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.Write(js.Serialize(entities));
    }


    [WebMethod]
    public void Addcategory(ImageEntities emp)
    {
        bool userNameInUse = false;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("ASP_INSERT_CATEGORY", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName = "@CATEGORY_NAME",
                Value = emp.CATEGORY_NAME
            });
            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName = "@CATEGORY_DESCRIPTION",
                Value = emp.CATEGORY_DESCRIPTION
            });

            con.Open();
            userNameInUse = Convert.ToBoolean(cmd.ExecuteScalar());
            cmd.ExecuteNonQuery();
        }
    }

Comments

Popular posts from this blog

Automatically send Birthday email using C#.Net

Drag and Drop multiple File upload using jQuery AJAX in ASP.Net using C# and VB.Net

Difference between each and map in jquery