Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I Call a SQL StoredProcedure from JavaScript

Status
Not open for further replies.

SteveL714

Programmer
Sep 19, 2013
21
US
I have added an onBlur method to an ASP Textbox used for creating a new user logon account. As the user exits the text box I want to take the entered value call a stored procedure to see if that logon value is already used or not.

In the Page_Init of CreateUser.aspx.cs I have the following:
Code:
txtUserName.Attributes.Add("onBlur", "validateLogon(this)");

I also have the following method in the cs page:
Code:
    private void ValidateUserLogon(string cUserLogon)
    {
        string myConnectionString;
        myConnectionString = Utils.GetConnectionString();
        SqlConnection conn4 = null;
        conn4 = new SqlConnection(myConnectionString);
        SqlCommand cmd4 = new SqlCommand("IsLogonUsed", conn4);
        cmd4.CommandType = CommandType.StoredProcedure;
        cmd4.Parameters.AddWithValue("@LogonValue", SqlDbType.VarChar).Value = txtUserName.Text.Trim();

        try
        {
            conn4.Open();
            int ReturnCode = Convert.ToInt32(cmd4.ExecuteScalar());
            if (ReturnCode == 1)
            {
                // This user code is already in use, so force them to enter another value
            }
            else
            {
                // This user code is available.  Let them continue
            }
        }
        catch (Exception ex)
        {
            ex = Server.GetLastError();
            Server.ClearError();
            LogErrorsToSql.Log(ex);
            Server.Transfer("~/Errors.aspx");
        }
    }


In the <head> of CreateUser.asxp I have the following, which confirms that it has the value that the user entered:
Code:
<script type="text/javascript">
    function validateLogon(elementRef) 
    {
        var logonValue = document.getElementById('<%=txtUserName.ClientID %>').value;
        alert('Entered Values: ' + logonValue);
        return true;
    }
</script>

What do I need to do to get the script to call the ValidateUserLogon method and take the appropriate action once the result gets back to the script?

Steve

 
Not like this. At all.
You can't connect to your db via Javascript. JS is executed in the browser, i.e. client-side.
You need to validate the logon server-side, in a post-back.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
You don't need to do a post-back to do what you are trying to do.
What you need to do, is to use an AJAX call via javascript.

This is how it works:
You assign an onclick event to you button that would normally call server side code. That onlclick event would in turn make an AJAX call to a handler (a .ashx file). The ashx file is your server side code that will execute and then send the response back to your AJAX function. In there you can do an alert or what ever you want.

How do you make an AJAX call you ask? Very easily using the JQuery framework. You can just pull in the CDN files in your master page for example. You make the call and you can return an XML object or JSON object for example. I suggest using a JSON object because they are easier to deal with an parse.

I know is sounds complicated, but once you do it, you will see it is not as complicated as it sounds.

Check the JQuery website to see all documentation and examples:
 
jbenson001,

Thanks for the information. I followed the link you provided, and to a Java novice like me it looks anything but easy. Also, I'd planned on working off of the onBlur event of the text box rather than a button, as I want immediate feedback as soon as the user entered their desired username value. Can you narrow down for me where I should be looking in that long list of functions?

Thanks again,
Steve
 
Just assign an onblur event to the textbox. You can look on that page. There are links to the api and all the functions.
If you don't want to navigate through it, simply use google, i.e "JQUery assign event to element"
You will see the documentation link pop up usually as the first result.
 
You'll get it. It's not as bad as it seems. Post in the javascript forum if you have any questions, or here if you have questions on how to call the code from an asp.net page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top