Connect Form to Database Insert Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having trouble figuring out to call a database INSERT procedure from a
simple submit form.

It appears I should use the onclick event to trigger the procedure called
[Sub btnInsert_Click] BUT when I do this I receive this error:

'btnInsert_Click' is not a member of 'ASP.Insert_aspx'.

I realise the procedure is houwsed in a [Private] sub which may be what is
causing the problem, but I am stumped on how to connect the form to the .NET
code...

Could someone help me here?


<% @ Import namespace="System.Data.SqlClient" %>
<script runat="server">

'Imports System.Data.SqlClient
Public Class index
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents btnInsert As System.Web.UI.WebControls.Button
Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx;
Password=xxxxxx; Persist Security Info=True;packet size=4096"

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadData()
End Sub

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnInsert.Click
Dim strConn As String = "data source=test.maximumasp.com; User
ID=V03testUW; Password=test Persist Security Info=True;packet size=4096"
Dim cmd As New SqlCommand("INSERT INTO ChangeStatus (FirstName,
LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')",
New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End Sub

Sub LoadData()
Dim strConn As String = "data source=test.maximumasp.com; User ID=test;
Password=testJ; Persist Security Info=True;packet size=4096"
Dim strSQL As String = "Select * From Customers"
Dim cmd As New SqlCommand(strSQL, New SqlConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub

End Class' Insert page code here
'



</script>

<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="txtFirstName" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:TextBox id="txtLastName" runat="server"></asp:TextBox>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
</p>
</form>
</body>
</html>
 
Jason:
you are trying to hook up the event two ways...

once in the aspx via onClick="Button1_Click"
second via Handles btnInsert.Click in the function name...

you only need one or the other....just remove the onClick="Button1_Click"
from the aspx control and the Handles btnInsert.Click will hook the event..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
I am having trouble figuring out to call a database INSERT procedure from a
simple submit form.

It appears I should use the onclick event to trigger the procedure called
[Sub btnInsert_Click] BUT when I do this I receive this error:

'btnInsert_Click' is not a member of 'ASP.Insert_aspx'.

I realise the procedure is houwsed in a [Private] sub which may be what is
causing the problem, but I am stumped on how to connect the form to the ..NET
code...

Could someone help me here?


<% @ Import namespace="System.Data.SqlClient" %>
<script runat="server">

'Imports System.Data.SqlClient
Public Class index
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents btnInsert As System.Web.UI.WebControls.Button
Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx;
Password=xxxxxx; Persist Security Info=True;packet size=4096"

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadData()
End Sub

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnInsert.Click
Dim strConn As String = "data source=test.maximumasp.com; User
ID=V03testUW; Password=test Persist Security Info=True;packet size=4096"
Dim cmd As New SqlCommand("INSERT INTO ChangeStatus (FirstName,
LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')",
New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End Sub

Sub LoadData()
Dim strConn As String = "data source=test.maximumasp.com; User ID=test;
Password=testJ; Persist Security Info=True;packet size=4096"
Dim strSQL As String = "Select * From Customers"
Dim cmd As New SqlCommand(strSQL, New SqlConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub

End Class' Insert page code here
'



</script>

<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="txtFirstName" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:TextBox id="txtLastName" runat="server"></asp:TextBox>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
</p>
</form>
</body>
</html>
 
Hi Karl, No go!

I did as you said and made only one hook up event. That removes the error
and deploys the form. But on submitting the form, nothing happens. The page
does seem to load but data in the text boxes remains after submitting as if
maintaining state and nothing is deposited in the database...........it does
not appear to be calling the sub below. IF it is something else is going
wrong, but no errror is thrown.

- Jason

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnInsert.Click
Dim strConn As String = "data source=test; User ID=V032U10DUW;
Password=ctestJ; Persist Security Info=True;packet size=4096"
Dim cmd As New SqlCommand("INSERT INTO ChangeStatus (ListingBroker,
SellingBroker)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text &
"')", New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End Sub

Sub LoadData()
Dim strConn As String = "data source=test.maxtestasp.com; User ID=test;
Password=ctestJ; Persist Security Info=True;packet size=4096"
Dim strSQL As String = "Select * From ChangeStatus"
Dim cmd As New SqlCommand(strSQL, New SqlConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub

End Class' Insert page code here
'

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="txtFirstName" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:TextBox id="txtLastName" runat="server"></asp:TextBox>
<asp:Button id="btnInsert" onclidk="btnInsert_Click"
runat="server" Text="Button"></asp:Button>
</p>
</form>
</body>
</html>







Karl Seguin said:
Jason:
you are trying to hook up the event two ways...

once in the aspx via onClick="Button1_Click"
second via Handles btnInsert.Click in the function name...

you only need one or the other....just remove the onClick="Button1_Click"
from the aspx control and the Handles btnInsert.Click will hook the event..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
I am having trouble figuring out to call a database INSERT procedure
from
a
simple submit form.

It appears I should use the onclick event to trigger the procedure called
[Sub btnInsert_Click] BUT when I do this I receive this error:

'btnInsert_Click' is not a member of 'ASP.Insert_aspx'.

I realise the procedure is houwsed in a [Private] sub which may be what is
causing the problem, but I am stumped on how to connect the form to the .NET
code...

Could someone help me here?


<% @ Import namespace="System.Data.SqlClient" %>
<script runat="server">

'Imports System.Data.SqlClient
Public Class index
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents btnInsert As System.Web.UI.WebControls.Button
Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx;
Password=xxxxxx; Persist Security Info=True;packet size=4096"

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LoadData()
End Sub

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnInsert.Click
Dim strConn As String = "data source=test.maximumasp.com; User
ID=V03testUW; Password=test Persist Security Info=True;packet size=4096"
Dim cmd As New SqlCommand("INSERT INTO ChangeStatus (FirstName,
LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')",
New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End Sub

Sub LoadData()
Dim strConn As String = "data source=test.maximumasp.com; User ID=test;
Password=testJ; Persist Security Info=True;packet size=4096"
Dim strSQL As String = "Select * From Customers"
Dim cmd As New SqlCommand(strSQL, New SqlConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub

End Class' Insert page code here
'



</script>

<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="txtFirstName" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:TextBox id="txtLastName" runat="server"></asp:TextBox>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
</p>
</form>
</body>
</html>
 
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
if Not Page.IsPOstBack Then
LoadData()
end if
End Sub

you are reloading the original data on postback thus overwritting whateve
was entered...

Kar

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


Hi Karl, No go!

I did as you said and made only one hook up event. That removes the error
and deploys the form. But on submitting the form, nothing happens. The page
does seem to load but data in the text boxes remains after submitting as if
maintaining state and nothing is deposited in the database...........it does
not appear to be calling the sub below. IF it is something else is going
wrong, but no errror is thrown.

- Jason

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnInsert.Click
Dim strConn As String = "data source=test; User ID=V032U10DUW;
Password=ctestJ; Persist Security Info=True;packet size=4096"
Dim cmd As New SqlCommand("INSERT INTO ChangeStatus (ListingBroker,
SellingBroker)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text &
"')", New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End Sub

Sub LoadData()
Dim strConn As String = "data source=test.maxtestasp.com; User ID=test;
Password=ctestJ; Persist Security Info=True;packet size=4096"
Dim strSQL As String = "Select * From ChangeStatus"
Dim cmd As New SqlCommand(strSQL, New SqlConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub

End Class' Insert page code here
'

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="txtFirstName" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:TextBox id="txtLastName" runat="server"></asp:TextBox>
<asp:Button id="btnInsert" onclidk="btnInsert_Click"
runat="server" Text="Button"></asp:Button>
</p>
</form>
</body>
</html>







Karl Seguin said:
Jason:
you are trying to hook up the event two ways...

once in the aspx via onClick="Button1_Click"
second via Handles btnInsert.Click in the function name...

you only need one or the other....just remove the onClick="Button1_Click"
from the aspx control and the Handles btnInsert.Click will hook the event..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
I am having trouble figuring out to call a database INSERT procedure
from
a
simple submit form.

It appears I should use the onclick event to trigger the procedure called
[Sub btnInsert_Click] BUT when I do this I receive this error:

'btnInsert_Click' is not a member of 'ASP.Insert_aspx'.

I realise the procedure is houwsed in a [Private] sub which may be
what
is the
.NET Web
Form
 
Thanks, Karl, but this has seemed to have no impact on the script after I
adjusted the script....the values are not going into the databasse No error
is thrown.

- Jason
 
Great Idea - but I am a bit stumped on how to do this, do you mean:

1. Use the Web Matrix debugger...not sure how to activate this....plus,
surely I need an error message prior to using a debugger?

2. Manually debug myself via the code....but not sure where to start as I am
not getting any errors.....?

I did not notice that perhaps the code I am using is redundant...I got this
from a tutorial site...I notice that the connection to the database seems to
be repeated in various sections of the code which I find confusing...surely
it only needs to be connected once in the string and then referred to, yet
the various subs seem to duplicate this.......

- Jason
 
Ah...WebMatrix doesn't support debugging...sorry, didn't know you were using
this.

As for the connection code, you typically want to recreate a connection and
open it by chunks and don't want to reuse/reopen old connections. If your
coming from classic ASP this might not seem intuitive. In classic ASP you'd
often open a connection early and close it late, in ASP.Net you open late
and close early...this is because of the underlying connection pooling...

Sorry I can't be any more help about the initial problem ....if you simply
the code to it's core (although it's pretty simple as-is) it might help.
You could also turn on page training via Trace="True" in the @Page directive
and adding some Trace.Write("Click_Event") this will atleast tell you if
your hitting the button click event.. By any chance, your button/textboxes
aren't in the repeater, are they?

Karl
 
Back
Top