Help inserting fields from form into a Database

Discussion in 'C#' started by mikebyrne, Dec 11, 2007.

  1. #1
    I want the user to select an option from a dropdown list. Im currently working on the add option. When this is selected i want fields to be shown so the user can fill out the info, press sumbit and the records enter the table.

    At the moment nothing is getting posted and the form just returns the asp page

    Heres my code:

    HTML
    
    <html>
    <head>
    <title>Ass3</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
    <body bgcolor="#FFFF99">
    <form name="myform" action="Ass3.asp" method="POST">
    <div align="center">
      <p>&nbsp;</p>
      <p><font size="+7">Database Maintenance </font></p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>
        <select name="mydropdown" size="1">
          <option value="Select">Please Select Option</option>
          <option value="Add">Add a Project</option>
          <option value="Change">Change a Project</option>
          <option value="Delete">Delete a project</option>
          <option value="List">List all project details</option>
        </select>
              </p>
              <input type="submit" value="Go!">
    </div>
    </form>
    </body>
    </html>
    
    Code (markup):
    ASP
    
    <%
    ' Dim variables
    Dim strFormAction, strPNUMBER, strPNAME, strPLOCATION, strDNUM
    
    ' Request values from form
    strFormAction = Request.Form("formAction")
    strPNUMBER = Request.form("PNUMBER") ' Request data from the form
    strPNAME = Request.Form("PNAME") ' Request data from the form
    strPLOCATION = Request.Form("PLOCATION") ' Request data from the form
    strDNUM = Request.Form("DNUM") 'Request data from the form
    
    
    If strFormAction = "SUBMIT" Then
    	'Dimension variables
    	Dim Conn 'Holds the Database Connection Object
    	Dim strSQL 'Holds the SQL query to query the database
    	Dim errorList 'Holds form errors
    
    
    	'Create an ADO connection object
    	Set Conn = Server.CreateObject("ADODB.Connection")
    
    
    	'Set an active connection to the Connection object using a DSN-less connection
    	strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/Byrne000/db/comp2000.mdb") & ";"
    	Conn.Open(strConnection)
    
    
    	'Check for missing data
    	If strPNUMBER = "" OR strPNAME = "" OR strPLOCATION = "" OR strDNUM = "" Then
    		errorList = "<font color=""red""><b>You must submit all required information.</b></font>"
    	End If
    
    
    	'Proceed if no errors
    	If errorList = "" Then
    		strSQL = "INSERT INTO Project (pnumber, pname, plocation, dnum) VALUES ('" & strPNUMBER & "', '" & strPNAME & "', '" & strPLOCATION & "', '" & strDNUM & "')"
    		Conn.Execute(strSQL)
    	End If
    
    
    	'Close connection objects
    	Conn.Close
    	Set Conn = Nothing
    End If
    %>
    <html>
    <head>
    <title>Adding Records To A Database</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
    <body bgcolor="#FFFF00">
    <p>Please enter details in table below:</p>
    <p><%= errorList %></p>
    <p>
      <form method="post" action="ass3.asp">
      <table width="500" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="200"><b>Project Number</b></td>
          <td width="300"><input type="text" name="PNUMBER" value="<%= strPNUMBER %>"></td>
        </tr>
        <tr>
          <td><b>Project Name</b></td>
          <td><input type="text" name="PNAME" value="<%= strPNAME %>"></td>
        </tr>
        <tr>
          <td><b>Project Location</b></td>
          <td><input type="text" name="PLOCATION" value="<%= strPLOCATION %>"></td>
        </tr>
        <tr>
          <td><b>Department Number</b></td>
          <td><input type="text" name="DNUM" value="<%= strDNUM %>"></td>
        </tr>
        <tr>
          <td colspan="2" height="20"></td>
        </tr>
        <tr>
          <td colspan="2"><input type="Submit" value="Add Project"></td>
        </tr>
      </table>
      <input type="hidden" name="formAction" value="SUBMIT">
      </form>
    </p>
    </body>
    </html>
    
    Code (markup):
    Any help would be great!!!!
     
    mikebyrne, Dec 11, 2007 IP
  2. mikebyrne

    mikebyrne Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <%
    ' Dim variables
    Dim strFormAction, strPNUMBER, strPNAME, strPLOCATION, strDNUM
    
    ' Request values from form
    strFormAction = Request.Form("formAction")
    strPNUMBER = Request.form("PNUMBER") ' Request data from the form
    strPNAME = Request.Form("PNAME") ' Request data from the form
    strPLOCATION = Request.Form("PLOCATION") ' Request data from the form
    strDNUM = Request.Form("DNUM") 'Request data from the form
    
    
    If strFormAction = "SUBMIT" Then
    	'Dimension variables
    	Dim Conn 'Holds the Database Connection Object
    	Dim strSQL 'Holds the SQL query to query the database
    	Dim errorList 'Holds form errors
    
    
    	'Create an ADO connection object
    	Set Conn = Server.CreateObject("ADODB.Connection")
    
    
    	'Set an active connection to the Connection object using a DSN-less connection
    	strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/Byrne000/db/comp2000.mdb") & ";"
    	Conn.Open(strConnection)
    
    
    	'Check for missing data
    	If strPNUMBER = "" OR strPNAME = "" OR strPLOCATION = "" OR strDNUM = "" Then
    		errorList = "<font color=""red""><b>You must submit all required information.</b></font>"
    	End If
    
    
    	'Proceed if no errors
    	If errorList = "" Then
    		strSQL = "INSERT INTO Project (pnumber, pname, plocation, dnum) VALUES ('" & strPNUMBER & "', '" & strPNAME & "', '" & strPLOCATION & "', '" & strDNUM & "')"
    		Conn.Execute(strSQL)
    		response.write strSQL & "<p>"
    		strSQL  = "Select * From Project WHERE pname = '" & strPNAME & "' "
    		'strSQL = "SELECT fname, lname, ESSN,  sum(Hours) as totsum FROM Works_On  left join employee on employee.ssn = Works_On.ESSN where = '" & ssnum & "' "
    		response.write strSQL & "<p>"
    		Conn.Execute(strSQL)
    		%><table border=3> <tr><td><%=pname%>
    		</td></tr></table> <%
    	End If
    
    
    	'Close connection objects
    	Conn.Close
    	Set Conn = Nothing
    End If
    %>
    <html>
    <head>
    <title>Adding Records To A Database</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
    <body bgcolor="#FFFF00">
    <p>Please enter details in table below:</p>
    <p><%= errorList %></p>
    <p>
      <form method="post" action="ass3.asp">
      <table width="500" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="200"><b>Project Number</b></td>
          <td width="300"><input type="text" name="PNUMBER" value="<%= strPNUMBER %>"></td>
        </tr>
        <tr>
          <td><b>Project Name</b></td>
          <td><input type="text" name="PNAME" value="<%= strPNAME %>"></td>
        </tr>
        <tr>
          <td><b>Project Location</b></td>
          <td><input type="text" name="PLOCATION" value="<%= strPLOCATION %>"></td>
        </tr>
        <tr>
          <td><b>Department Number</b></td>
          <td><input type="text" name="DNUM" value="<%= strDNUM %>"></td>
        </tr>
        <tr>
          <td colspan="2" height="20"></td>
        </tr>
        <tr>
          <td colspan="2"><input type="Submit" value="Add Project"></td>
        </tr>
      </table>
      <input type="hidden" name="formAction" value="SUBMIT">
      </form>
    </p>
    </body>
    </html>
    
    Code (markup):
    Im trying to get the contents of table "projects" displayed in a table. Any idea how i can do it???
     
    mikebyrne, Dec 12, 2007 IP
  3. Link.ezer.com

    Link.ezer.com Peon

    Messages:
    647
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #3
    have you tried [ form method="get" ] instead of [ form method="POST" ]
     
    Link.ezer.com, Dec 12, 2007 IP