Imports errror while attempting Insert

G

Guest

I'm picking up an 'IMPORTS' error for a simple database insert based on two
input entry boxes in my form?

It says an 'Imports' statement must preceede any declarations....... is this
perahps the '@' symbol??

Any help appreciated!

Thanks
Jason




---------------------------------------------
ERROR ----------------------------------

Server Error in '/' Application.


Compilation Error
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: BC30465: 'Imports' statements must precede any
declarations.

Source Error:


Line 2: <script runat="server">
Line 3:
Line 4: Imports System.Data.SqlClient
Line 5: Public Class index
Line 6: Inherits System.Web.UI.Page


Source File: C:\source\Insert.aspx Line: 4

-------------------------------------------
HTML -----------------------------
<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>


-----------------------------------
CODE ----------------------------------------------------

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=xx.xx.xxx.xxx; User ID=xxxxxx;
Password=xxxxxx; Persist Security Info=True;packet size=4096"
Dim cmd As New SqlCommand("INSERT INTO Customers (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=xx.xx.xxx.xxx; User ID=xxxxxx;
Password=xxxxxx; 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


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=MTEST.maximumasp.com; User ID=TEST;
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=MTEST.maximumasp.com; User ID=TEST;
Password=TEST; 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
'

Sub Button1_Click(sender As Object, e As EventArgs)

End Sub
 
K

Karl Seguin

to import a namespace into an aspx page, you need to use the @Import
directive

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

Karl
 
G

Guest

Hi Karl,

But, do I add this to the 'html' or the 'code' page.

I tried adding it to the top of the html portion and still get the same
error:

This is confusing...I mean my code behind view already has the imports
statement...why would I need to add to the html form section anyway?
 
G

Guest

Ok, I fixed the error! I did this by going to the 'ALL' view in Web Matrix
which allowed me to see the vicinity of the 'runat server' in relation to
the 'imports' statement.

But, now I am trying to see how to 'connect' my database insert statement to
the form submit button.......I know I am close but need some help:

I think I need to load the sub 'load_data' from the submit button, but not
sure how to do it....Perhaps I need to rename my button to 'btnInsert_Click'
to activate the insert sub?


<% @ 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
'

Sub Button1_Click(sender As Object, e As EventArgs)

End Sub

</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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top