Execute a SQL Stored Procedure through VWD Express

Last post 05-20-2008 6:24 AM by mcleana. 3 replies.

Sort Posts:

  • Execute a SQL Stored Procedure through VWD Express

    05-17-2008, 1:12 AM
    • Member
      1 point Member
    • mcleana
    • Member since 05-17-2008, 12:51 AM
    • Posts 2

    I would like to be able to execute a stored procedure by clicking a button on my webpage.  The stored procedure would be used to manipulate data in the underlying database, rather than returing data.  I am using C# programming through VWD Express.  Today is my first day of trying to use this software.

    After reading a few articles on the web I thought that I would be able to create a void and use the sqlcommand function to do this (eg below).  However VWD does not have this listed as an acceptable comment to use.

     

    protection void Button1_Click ()

    {

    conn = new SqlConnection("Server=Server;DataBase=Database");

    conn.open() 

    sqlcommand cmd = new sqlcommand ("EXEC ProcedureName", conn);

    cmd.ExecuteReader;

    }

     

    I there any way I can execute a stored procedure through VWD express?  Thank you.

     

     

  • Re: Execute a SQL Stored Procedure through VWD Express

    05-17-2008, 5:56 AM
    Answer
    • Star
      14,584 point Star
    • david wendelken
    • Member since 07-27-2005, 11:47 PM
    • Fayetteville, NC, USA
    • Posts 2,076
    • Moderator

    Try cmd.ExecuteNonQuery.  ExecuteReader is for queries, and your procedure (probably) isn't returning data from a query.  I'm also assuming that conn is defined at the object level and that your procedure has no parameters.

    You are brand new at this, so there is no shame in doing things wrong.  Might as well learn to do it right on day one! Smile

    1. Do not put your connection string information hard-wired into your call to new SqlConnection.  It should be stored somewhere and referenced.  As your website grows, you don't want to have to search for every single time you make a connection to change the connection details.  The traditional place to put this is in the web.config file.  There are online and book examples aplenty of how to do it.
    2. When you open a connection you MUST, MUST, MUST make absolutely positively sure you close it.  It is not sufficient to include a conn.close() statement after you execute your command because an error might be raised by that command, and therefore the close would not be executed.  You can use a try/finally construct, with the open in the try and the close in the finally.  You can also use a using construct.  Again, lots of examples out there.  Failing to close connections can cause a site to crash, which is a very embarassing thing to explain to the president of your company (before you get escorted off the premises to find a new job).
    3. When you start to need parameters, make sure you define them as the cmd.parameters.  Do NOT go around concatenating user-supplied values stored in strings into a sql statement!  Read up on sql injection attacks and parameterized queries (which protect you from same).

    Good luck!  The .Net tools are first rate, you'll enjoy them as you learn them.

     

    If this answered your question, be sure to mark it as the answer. That way, everybody after you will know it's the answer also!
  • Re: Execute a SQL Stored Procedure through VWD Express

    05-19-2008, 4:30 PM
    • Member
      231 point Member
    • tracer
    • Member since 02-08-2008, 12:39 PM
    • Cochin , India
    • Posts 66
    // freeclass is the procedure i am executed from SQL server 2005 from VWD
    //input : @slot and @classno
    // output : @count
    
     protected Int16 SQL_freeclass(Int16 slot, Int16 classno)
            {
                SqlConnection con = new SqlConnection(this.Connectstring); //this.Connectstring="Server=Server;DataBase=Database")
                SqlCommand command = new SqlCommand("freeclass",con);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@slot", SqlDbType.Int);
                command.Parameters["@slot"].Value = slot;
                command.Parameters.Add("@classno", SqlDbType.Int);
                command.Parameters["@classno"].Value = classno;
                command.Parameters.Add("@count", SqlDbType.Int);
                command.Parameters["@count"].Direction = ParameterDirection.Output;
                con.Open();
                Int16 result;
                command.ExecuteNonQuery();
                result = (Int16)command.Parameters["@count"].Value;
                con.Close();
                return result;
            }
    

    i am pasting a function which i recently defined to access a stored procedure in sql server 2005 from VWD. Hope it helps u. U may contact me if u further need any help on this

    Plz do mark this as answer if it helped u



    tracer

  • Re: Execute a SQL Stored Procedure through VWD Express

    05-20-2008, 6:24 AM
    • Member
      1 point Member
    • mcleana
    • Member since 05-17-2008, 12:51 AM
    • Posts 2

    Thank you for your response David.  I have managed to get this to work.

    The main reason I was having problems was that I didn't have Using.System.SQLClient at the top of my code, so couldn't find commands such as SQL connection to use.

     Now that I have worked this one out I have managed to get my stored procedure to run.  I also found http://forums.asp.net/p/1104457/1690796.aspx#1690796 helpful in working out how to put the connection string into the web.config file.

     Thanks for your help.  I am up and running.

Page 1 of 1 (4 items)