How to call a stored procedure in Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • user1980
    New Member
    • Dec 2009
    • 112

    How to call a stored procedure in Javascript

    Hello there

    I would like to know if I can call a stored procedure from java script function.. I have a hyperlink, which should allow user to insert or delete or edit a column in the database. can this be done?? i would like to put the functionality in the javascript function
    my page is an asp page.......

    thank you
  • chathura86
    New Member
    • May 2007
    • 227

    #2
    i dont think so, cuz as far as i know js cannot connect to a database
    on the other hand even if you can since you have the connection string on the
    js, anyone can see your usernames and passwords so i guess you should use
    asp to do that stuff

    Regards

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Although this is not possible with JavaScript, you can make an ASP page which calls the stored procedure and whatever result ensues, and then make a request to that page via Ajax.

      Comment

      • walidtunis
        New Member
        • Oct 2011
        • 1

        #4
        Code:
        public string[] CCNumLookup(string prefixText) 
                {
        
                    SqlConnection conn = new SqlConnection("connectionstring goes here");
                    
                    SqlCommand command = null;
                    command = new SqlCommand("You stored Procedure name here", conn);
        
                    DataTable dt = new DataTable();
                    SqlDataAdapter adapter = null;
                    
                    try
                    {
                        command.Parameters.Add("@prefixText", SqlDbType.NVarChar, 50).Value = prefixText + "%";
                        adapter = new SqlDataAdapter(command);
                        adapter.Fill(dt);
                    }
                    catch (Exception e)
                    {
                        //handle exception
                    }
                    finally
                    {
                        if (adapter != null)
                        {
                            adapter.Dispose();
                        }
        
                        if (command != null)
                        {
                            command.Dispose();
                        }
        
                        if (conn != null)
                        {
                            if (conn.State == ConnectionState.Open)
                            {
                                conn.Close();
                            }
                            conn = null;
                        }
        
                    }
                    string[] items = new string[dt.Rows.Count]; 
                    int i = 0;
        
                    foreach (DataRow dr in dt.Rows) 
                    {
        
                    items.SetValue(dr["cost_center"].ToString(), i);
        
                    i++; 
                    }
        
                    return items; 
                }
        Last edited by Niheel; Oct 26 '11, 05:06 PM.

        Comment

        Working...