Wrox Home  
Search P2P Archive for: Go

  Return to Index  

ado_dotnet thread: Creating and using parameters


Message #1 by "Arbon Reimer" <arbon_reimer@h...> on Tue, 19 Mar 2002 17:45:08
Hello, I am still new to the .NET framework and wanted to play with 
SqlCommand objects and passing parameters.

I have written one page that works, but it seems to actually execute the 
stored procedure twice.  I'm confused why it does this!

Here is my code... I'm sure it's probably really obvious to slightly more 
seasoned ASP.NETers:

<WebForm2.aspx>
<%@ Page Language="vb" Inherits="WebForm2" src="WebForm2.aspx.vb"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>WebForm2</title>
  </head>
  <body MS_POSITIONING="GridLayout">
	<asp:Label ID="lblfork" Runat="server"></asp:Label>
  </body>
</html>
</WebForm2.aspx>
==============================
<WebForm2.aspx.vb>
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls

Public Class WebForm2
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "
...
#End Region
	Protected lblFork as System.Web.UI.WebControls.Label
	
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
		Dim oConn as New SqlConnection
(ConfigurationSettings.AppSettings("DSN"))
		oConn.Open()
		Dim oCmd as New SqlCommand()
		oCmd.Connection=oConn
		Dim oParam as New SqlParameter("@fork",3)
		oCmd.Parameters.Add(oParam)
		oCmd.CommandText="procTestParams"
		oCmd.CommandType=CommandType.StoredProcedure
		oCmd.ExecuteNonQuery()
		oConn.Close()
		lblFork.Text="check the data"
    End Sub
End Class
</WebForm2.aspx.vb>

<Stored Procedure>
ALTER  PROCEDURE procTestParams
(
	@fork int
)
AS

INSERT INTO Table1(NewNumber)
VALUES(@fork)
</Stored Procedure>

I can execute the Stored Procedure in SQL Query Analyzer and it works just 
fine.  Can anyone tell me why it is executing twice?  Thank you all for 
your help.

Arbon Reimer
Golden, Colorado
Message #2 by David.Martret@q... on Thu, 21 Mar 2002 10:50:29 +1100

Hi Arbon,

Try enclosing the code in the Page_Load with:

If Not Page.IsPostBack Then
...
End If

But this may not fix it ... I have also noticed that the Page_Load event
seems to be called twice - but I'm not sure why.

I have used a static variable to get around this (eg blnRun) so that the
code only runs once - but this is a very messy way of getting around it.

Sorry I can't really answer your question though.

David



                                                                                                                      
                    "Arbon Reimer"                                                                                    
                    <arbon_reimer@h...        To:     "ADO.NET" <ado_dotnet@p...>                               
                    tmail.com>              cc:                                                                       
                                            Subject:     [ado_dotnet] Creating and using parameters                   
                    20/03/02 04:45                                                                                    
                    Please respond                                                                                    
                    to "ADO.NET"                                                                                      
                                                                                                                      
                                                                                                                      



Hello, I am still new to the .NET framework and wanted to play with
SqlCommand objects and passing parameters.

I have written one page that works, but it seems to actually execute the
stored procedure twice.  I'm confused why it does this!

Here is my code... I'm sure it's probably really obvious to slightly more
seasoned ASP.NETers:

<WebForm2.aspx>
<%@ Page Language="vb" Inherits="WebForm2" src="WebForm2.aspx.vb"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>WebForm2</title>
  </head>
  <body MS_POSITIONING="GridLayout">
           <asp:Label ID="lblfork" Runat="server"></asp:Label>
  </body>
</html>
</WebForm2.aspx>
==============================
<WebForm2.aspx.vb>
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls

Public Class WebForm2
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "
...
#End Region
           Protected lblFork as System.Web.UI.WebControls.Label

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
                     Dim oConn as New SqlConnection
(ConfigurationSettings.AppSettings("DSN"))
                     oConn.Open()
                     Dim oCmd as New SqlCommand()
                     oCmd.Connection=oConn
                     Dim oParam as New SqlParameter("@fork",3)
                     oCmd.Parameters.Add(oParam)
                     oCmd.CommandText="procTestParams"
                     oCmd.CommandType=CommandType.StoredProcedure
                     oCmd.ExecuteNonQuery()
                     oConn.Close()
                     lblFork.Text="check the data"
    End Sub
End Class
</WebForm2.aspx.vb>

<Stored Procedure>
ALTER  PROCEDURE procTestParams
(
           @fork int
)
AS

INSERT INTO Table1(NewNumber)
VALUES(@fork)
</Stored Procedure>

I can execute the Stored Procedure in SQL Query Analyzer and it works just
fine.  Can anyone tell me why it is executing twice?  Thank you all for
your help.

Arbon Reimer
Golden, Colorado





  Return to Index