Results 1 to 6 of 6

Thread: How to use stored procedures in ASP.net?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Location
    47-B Silliman Ave. Dgte. City
    Posts
    4

    How to use stored procedures in ASP.net?

    Hi Guys,

    I am a new member here in VB forums. How do we use stored procedures? I mean how do U link the paremeters in the stored procedures to the coding environment in VB.net, specifically ASP.net web application. Like for example a text box...

    Ex. select last_name from student where idnum = @idnum
    /*this select statement is oftenly used in the stored procedures. */

    so, how do we connect to the procedure for example we input the idnum in a textbox? so that we can query the last_name...

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    Assuming your using SQL Server.....

    VB Code:
    1. imports system.data.sqlclient
    2. 'At top of page
    3.  
    4. ....
    5. dim sqlConn as sqlConnection
    6. dim cmdSelect as sqlCommand
    7.  
    8. 'Open db connection in sqlConn
    9. ...
    10. cmdSelect = new("stored_proc_name_here",sqlConn)
    11. cmdSelect.commandType = commandType.StoredProcedure
    12. cmdSelect.parameters.add("@idnum", txtIDNum.text)
    13. ...

    Thats from memory so not sure if exact but you should get the idea.

  3. #3
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    PHP Code:
            /// <summary>
            /// Execute a nonquery stored procedure.
            /// </summary>
            /// <param name="commandText">Procedure name.</param>
            /// <param name="param">Array of SqlParameters.</param>
            /// <returns></returns>
            
    public static int ExecuteNonQuery(string commandTextSqlParameter[] param)
            {
                
    SqlConnection conn Connection;
                
    SqlCommand cmd = new SqlCommand(commandTextconn);
                
    cmd.CommandType CommandType.StoredProcedure;
                
                for(
    int i 0param.Lengthi++)
                {
                    
    cmd.Parameters.Add(param[i]);
                }

                return 
    cmd.ExecuteNonQuery();
            } 
    that is the basic idea although I haven't tested that method yet so it may have a bug I should get to test it today though and I'll repost it if I find a bug. also look in the C# forum at my threade comments on SQL Server code for a big example. The only dif betwenn OleDb and SqlClient is the one you see when you read OleDB and SqlClient at least in usage.
    Magiaus

    If I helped give me some points.

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    ugh hmm param.Lenght - 1 I'm a dumb ass sometimes
    Magiaus

    If I helped give me some points.

  5. #5
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    also Connection is a property of my class DatabaseName that return a setup connection and both it and the command should be disposed before the return for a happy framework
    Magiaus

    If I helped give me some points.

  6. #6
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    Yeah sorry that line should read:

    cmdSelect = new sqlCommand("stored_proc_name_here",sqlConn)

    When you create a stored proc you name it as such:

    create proc stored_proc_name(@param1 int) as

    So i'm not sure how you couldn't know the name of a stored procedure you were trying to use...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width