Form submit to access database. Correct method?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • munkee
    Contributor
    • Feb 2010
    • 374

    Form submit to access database. Correct method?

    Hi all,

    I have taken the plunge to move away from regular html forms with and all client side coding in to asp.net. Server side scripting is totally alien to me but atleast you can write in VB (I am used to VBA but it is close enough to be able to get by).

    I have a form which currently submits information to an access database using javascript however I am looking to move this to asp.net. I have looked at numerous tutorials and found a half decent example which I got working in visual web developer.

    The code is as follows:

    Code:
    <%@ Import Namespace="System.Data.OleDb" %>
    <script runat="server">
    Sub submit(sender As Object, e As EventArgs)
    
        Dim conCoaxis As OleDbConnection
        Dim strInsert As String
        Dim cmdInsert As OleDbCommand
        Dim dtmDate As DateTime
       
    dtmDate = DateTime.Now()
     
    conCoaxis=New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath(“email.mdb"))
    strInsert = “INSERT INTO email (Email, Fname, CompanyName, [Date]) Values (@Email, @Fname, @CompanyName, @Date)"
     
    cmdInsert = New OleDbCommand( strInsert, conCoaxis )
     
    cmdInsert.Parameters.Add( “@Email", txtEmail.Text )
    cmdInsert.Parameters.Add( “@Fname", txtName.Text )
     cmdInsert.Parameters.Add( “@CompanyName", txtCompany.Text )
     cmdInsert.Parameters.Add( “@Date", OleDbType.Date).Value = dtmDate.ToString(“g")  
      Try
       conCoaxis.Open()
       cmdInsert.ExecuteNonQuery()
       conCoaxis.Close()
       Response.Write(“Updated Successfully!<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>")
       Catch
       conCoaxis.Close()
      End Try
     End Sub
    </script>
    
    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN" “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
    <html xmlns="http://www.w3.org/1999/xhtml“>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <form  runat="server" id="form1" name="form1" method="post" action="">
      Name:     <asp:TextBox id="txtName" runat=server /> 
    
    Email:    <asp:TextBox id="txtEmail" runat=server />
    Company:    <asp:TextBox id="txtCompany" runat=server />
     <asp:Button id="id" text="Submit" OnClick="submit" runat=server />
    </form> 
    </body>
    </html>
    My question is whether this is the typical way to do this? I have seen a couple of examples of regular asp files being used within a form post to which then execute the new record insert. But I have since read in aspx you do not need this separate file at all.

    Just looking for some reassurance and advice here.

    Thanks,

    Chris
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    This looks fine to me.
    Are you having any problems with it?

    Comment

    Working...