Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: method .save call from <asp:button onclick event.. out of scope?


Message #1 by "John Anderson" <john@a...> on Wed, 12 Feb 2003 07:37:18
Hello I am new to Oop and have read my Asp.net book and then been trying 
to figure out how to do this call..
I created a class, it is an object that loads a "inventory" wich is a 
category in a website. well it can load an existing, or create a new one. 
it has a .save method.. I am wanting to set some properties and call 
that .save method triggered from an onclick event. I get an Error,
I am using Onclick=Save() and Save() has the code to add the data to the 
properties and run the .update method.

here is the error and the Code.

Description: An error occurred during the compilation of a resource 
required to service this request. Please review the following specific 
error details and modify your source code appropriately. 

Compiler Error Message: BC30451: Name 'Inv' is not declared.

Source Error:

 

Line 25: Sub Save(sender as Object, e as EventArgs) ' fill Object with new 
values and write to database
Line 26: 
Line 27: 	Inv.Name = frminv_Name.text
Line 28: 	Inv.Description = frminv_description.text
Line 29: 	inv.save()
 

Here is the Code: 
this is inv_edit.aspx

<%@ Page Language="vb" Debug="true" %>
<!-- #include file="Class.aspx" -->

<script language="VB" runat="server">
dim inv_id

Sub Page_Load(Source as Object, E as EventArgs)
	if request("inv_id") <> "" then
		inv_id = request("inv_id")
	end if
	
	Dim Inv as New Inventory(inv_id)
	
	if not Page.IsPostBack then	
		PopulateForm(inv)
	end if
End Sub

Sub PopulateForm(inv as object)
	
	frminv_Name.text = Inv.Name
	frminv_description.text = Inv.Description
	
End Sub
Sub Save(sender as Object, e as EventArgs) ' fill Object with new values 
and write to database

	Inv.Name = frminv_Name.text
	Inv.Description = frminv_description.text
	inv.save()
	

End Sub
Sub btnSave()
Save(inv)
End Sub
</Script>
<form Name="form1" runat="server">
<table border=1 cellspacing=0>
<tr><td align='right'>Inventory Name:</td><td><asp:textbox 
id="frminv_Name" size="100" Text="" runat="server" /></td></tr>
<tr><td align='right'>Description:</td><td><asp:textbox 
id="frminv_description" runat="server" rows=5 cols="100" 
textmode="multiline"/></td></tr>
<tr><td><asp:button id="button1" Text="Update" onclick="btnSave()" 
runat="server" /></td></tr>

</table>
</form> 



and here is the Class I created..

Class.aspx

<%@ Import Namespace="System.Data" %>
<%@ Import NameSpace="System.Data.SqlClient" %>
<!-- #include file="include/datastore.aspx" -->
<script language="VB" runat="server">


Public Class Inventory
	Dim invID, invName, invParentID, invDescription
	Dim invIsNew

Public Sub New(LoadID as long)
	if LoadID < "1" then
		invIsNew = true
	else
	invID = LoadID
	invIsNew = False
	LoadBYID()
	end if
End Sub
Public Sub New()
	invIsNew = true
End Sub

Public Sub LoadByID()

	Dim myConnection As SqlConnection
	Dim myCommand As SqlCommand
	Dim myDataReader As SqlDataReader

	myConnection = New SqlConnection(strconn)
	myConnection.Open()
	myCommand = New SqlCommand( "Select * from tblInventories where 
inv_id = " & invID , myConnection )
	myDataReader = myCommand.ExecuteReader()


	While myDataReader.Read()
		
		invName = myDataReader.Item( "inv_name" )
		invdescription = myDataReader.Item( "inv_description" )
		invParentID = myDataReader.Item( "inv_parent_id" )
		
	End While

myDataReader.Close()
myConnection.Close()


End Sub

Public Sub Save()
	Dim MySQL
	if invIsNew = true then 
		MySQL = "insert into tblInventories (inv_name, 
inv_parent_id, inv_description) values (@Nname, @inv_parent_id, @Desc)"
	else
		MySQL = "Update tblInventories Set inv_name=@Nname, 
inv_description=@Desc Where inv_id=@ID"
	end if

	
	
	Dim Conn as New SQLConnection(strConn)
	Dim Cmd as New SQLCommand(MySQL, Conn)
		cmd.Parameters.Add(New SQLParameter("@ID", invID))
		cmd.Parameters.Add(New SQLParameter("@Nname", invName))
		cmd.Parameters.Add(New SQLParameter("@Desc", 
invDescription))
		cmd.Parameters.Add(New SQLParameter("@inv_parent_id", 
invParentID))
	Conn.Open()
	cmd.ExecuteNonQuery()


End Sub

Public Property ID As Long
	Get 
		Return invID
	End Get
	Set
		invID = value
	End Set
End Property

Public Property Name As String
	Get 
		Return invName
	End Get
	Set
		invName = value
	End Set
End Property

Public Property ParentID As Long
	Get 
		Return invParentID
	End Get
	Set
		invParentID = value
	End Set
End Property

Public Property Description As String
	Get 
		Return invDescription
	End Get
	Set
		invDescription = value
	End Set
End Property




End Class
</script>
Message #2 by "Van Knowles" <vknowles@s...> on Wed, 19 Feb 2003 20:39:52
If I am not mistaken, the problem is that your Inv variable is declared 
local to the Page_Load procedure.  It needs to be declared outside any Sub 
or Function statements (e.g., move the Dim statement just before the Sub 
Page_Load statement).

-Van

> Hello I am new to Oop and have read my Asp.net book and then been trying 
t> o figure out how to do this call..
I>  created a class, it is an object that loads a "inventory" wich is a 
c> ategory in a website. well it can load an existing, or create a new 
one. 
i> t has a .save method.. I am wanting to set some properties and call 
t> hat .save method triggered from an onclick event. I get an Error,
I>  am using Onclick=Save() and Save() has the code to add the data to the 
p> roperties and run the .update method.

> here is the error and the Code.

> Description: An error occurred during the compilation of a resource 
r> equired to service this request. Please review the following specific 
e> rror details and modify your source code appropriately. 

> Compiler Error Message: BC30451: Name 'Inv' is not declared.

> Source Error:

>  

> Line 25: Sub Save(sender as Object, e as EventArgs) ' fill Object with 
new 
v> alues and write to database
L> ine 26: 
L> ine 27: 	Inv.Name = frminv_Name.text
L> ine 28: 	Inv.Description = frminv_description.text
L> ine 29: 	inv.save()
 > 

> Here is the Code: 
t> his is inv_edit.aspx

> <%@ Page Language="vb" Debug="true" %>
<> !-- #include file="Class.aspx" -->

> <script language="VB" runat="server">
d> im inv_id

> Sub Page_Load(Source as Object, E as EventArgs)
	> if request("inv_id") <> "" then
	> 	inv_id = request("inv_id")
	> end if
	> 
	> Dim Inv as New Inventory(inv_id)
	> 
	> if not Page.IsPostBack then	
	> 	PopulateForm(inv)
	> end if
E> nd Sub

> Sub PopulateForm(inv as object)
	> 
	> frminv_Name.text = Inv.Name
	> frminv_description.text = Inv.Description
	> 
E> nd Sub
S> ub Save(sender as Object, e as EventArgs) ' fill Object with new values 
a> nd write to database

> 	Inv.Name = frminv_Name.text
	> Inv.Description = frminv_description.text
	> inv.save()
	> 

> End Sub
S> ub btnSave()
S> ave(inv)
E> nd Sub
<> /Script>
<> form Name="form1" runat="server">
<> table border=1 cellspacing=0>
<> tr><td align='right'>Inventory Name:</td><td><asp:textbox 
i> d="frminv_Name" size="100" Text="" runat="server" /></td></tr>
<> tr><td align='right'>Description:</td><td><asp:textbox 
i> d="frminv_description" runat="server" rows=5 cols="100" 
t> extmode="multiline"/></td></tr>
<> tr><td><asp:button id="button1" Text="Update" onclick="btnSave()" 
r> unat="server" /></td></tr>

> </table>
<> /form> 

> 

> and here is the Class I created..

> Class.aspx

> <%@ Import Namespace="System.Data" %>
<> %@ Import NameSpace="System.Data.SqlClient" %>
<> !-- #include file="include/datastore.aspx" -->
<> script language="VB" runat="server">

> 
P> ublic Class Inventory
	> Dim invID, invName, invParentID, invDescription
	> Dim invIsNew

> Public Sub New(LoadID as long)
	> if LoadID < "1" then
	> 	invIsNew = true
	> else
	> invID = LoadID
	> invIsNew = False
	> LoadBYID()
	> end if
E> nd Sub
P> ublic Sub New()
	> invIsNew = true
E> nd Sub

> Public Sub LoadByID()

> 	Dim myConnection As SqlConnection
	> Dim myCommand As SqlCommand
	> Dim myDataReader As SqlDataReader

> 	myConnection = New SqlConnection(strconn)
	> myConnection.Open()
	> myCommand = New SqlCommand( "Select * from tblInventories where 
i> nv_id = " & invID , myConnection )
	> myDataReader = myCommand.ExecuteReader()

> 
	> While myDataReader.Read()
	> 	
	> 	invName = myDataReader.Item( "inv_name" )
	> 	invdescription = myDataReader.Item( "inv_description" )
	> 	invParentID = myDataReader.Item( "inv_parent_id" )
	> 	
	> End While

> myDataReader.Close()
m> yConnection.Close()

> 
E> nd Sub

> Public Sub Save()
	> Dim MySQL
	> if invIsNew = true then 
	> 	MySQL = "insert into tblInventories (inv_name, 
i> nv_parent_id, inv_description) values (@Nname, @inv_parent_id, @Desc)"
	> else
	> 	MySQL = "Update tblInventories Set inv_name=@Nname, 
i> nv_description=@Desc Where inv_id=@ID"
	> end if

> 	
	> 
	> Dim Conn as New SQLConnection(strConn)
	> Dim Cmd as New SQLCommand(MySQL, Conn)
	> 	cmd.Parameters.Add(New SQLParameter("@ID", invID))
	> 	cmd.Parameters.Add(New SQLParameter("@Nname", invName))
	> 	cmd.Parameters.Add(New SQLParameter("@Desc", 
i> nvDescription))
	> 	cmd.Parameters.Add(New SQLParameter("@inv_parent_id", 
i> nvParentID))
	> Conn.Open()
	> cmd.ExecuteNonQuery()

> 
E> nd Sub

> Public Property ID As Long
	> Get 
	> 	Return invID
	> End Get
	> Set
	> 	invID = value
	> End Set
E> nd Property

> Public Property Name As String
	> Get 
	> 	Return invName
	> End Get
	> Set
	> 	invName = value
	> End Set
E> nd Property

> Public Property ParentID As Long
	> Get 
	> 	Return invParentID
	> End Get
	> Set
	> 	invParentID = value
	> End Set
E> nd Property

> Public Property Description As String
	> Get 
	> 	Return invDescription
	> End Get
	> Set
	> 	invDescription = value
	> End Set
E> nd Property

> 

> 
E> nd Class
<> /script>

  Return to Index