Inserting Data in Page_load problem!!!!!

  • Thread starter Patrick Olurotimi Ige
  • Start date
P

Patrick Olurotimi Ige

I'm trying to insert data in Page_load below but its inserting 2 records
at the sametime!!!
What am i doing wrong!!!!!

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

if Not Page.IsPostBack then

Dim strLogonUsr As String

strLogonUsr = Request.ServerVariables("LOGON_USER")

Dim myConnection = New
SqlConnection("server=(local);database=Wintergreen;integrated
security=true;")


Dim insertCmd As String = _
"insert into survey values (@username, @DateCreated);"
' Initialize the SqlCommand with the new SQL string.

Dim myCommand = New SqlCommand(insertCmd, myConnection)
' Created new parameters for the SqlCommand object and
' initialize them to the field values.
' Create the parameter for the logon user

myCommand.Parameters.Add(New SqlParameter("@username", _
SqlDbType.VarChar, 50))

' Assigned the value
myCommand.Parameters("@username").Value = strLogonUsr

' Created the parameter for the time
myCommand.Parameters.Add(New SqlParameter("@DateCreated", _
SqlDbType.DateTime, 8))

' Assign the value as Now()

myCommand.Parameters("@DateCreated").Value = Now()
myCommand.Connection.Open()

'myCommand.ExecuteNonQuery()

Try
' Execute the command
'myCommand.ExecuteNonQuery()

myCommand.ExecuteNonQuery()
' Report success
Label1.Text = "Thanks for filling Crazy John's Survey!"

Catch exc As SqlException

' Report error if u have one
Label1.Text = exc.Message

Finally
' Close the connection no matter what
myCommand.Connection.Close()
End Try

End If

End Sub
 
K

Kumar Reddi

Make sure you have AutoEventWireUp set to false in the page directive of the
aspx page
 
P

Patrick Olurotimi Ige

thx Kumar Reddi it worked like a charm...
But why must AutoEventWireUp set to false?
Is it becos 'm inserting the data in page_load..
If u can hint me that would be nice
 

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

Top