Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


How do I Call a SQL StoredProcedure from JavaScript

How do I Call a SQL StoredProcedure from JavaScript

How do I Call a SQL StoredProcedure from JavaScript

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:


txtUserName.Attributes.Add("onBlur", "validateLogon(this)"); 

I also have the following method in the cs page:


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();

            int ReturnCode = Convert.ToInt32(cmd4.ExecuteScalar());
            if (ReturnCode == 1)
                // This user code is already in use, so force them to enter another value
                // This user code is available.  Let them continue
        catch (Exception ex)
            ex = Server.GetLastError();

In the <head> of CreateUser.asxp I have the following, which confirms that it has the value that the user entered:


<script type="text/javascript">
    function validateLogon(elementRef) 
        var logonValue = document.getElementById('<%=txtUserName.ClientID %>').value;
        alert('Entered Values: ' + logonValue);
        return true;

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?


RE: How do I Call a SQL StoredProcedure from JavaScript

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.

RE: How do I Call a SQL StoredProcedure from JavaScript

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:

RE: How do I Call a SQL StoredProcedure from JavaScript


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,

RE: How do I Call a SQL StoredProcedure from JavaScript

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.

RE: How do I Call a SQL StoredProcedure from JavaScript

Thanks... that will help (I hope!)

RE: How do I Call a SQL StoredProcedure from JavaScript

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close