Form for updating sql, how to pass the values to the form?

Discussion in 'C#' started by vidus, Aug 18, 2010.

  1. #1
    Hey guys,
    I am having some trouble finding help for this problem. I have some asp pages to manage our sql database. I have an "edit" button next to every record displayed. When clicked, the user gets a form to edit that record.

    The problem is, I need the form to show the records data! Or else the user is editing blind...

    How can I make this happen! Here is some of my code, hopefully someone here can show me how this is done? :)

    
    <%
    'declare the variables 
    Dim Connection
    Dim ConnString
    Dim Recordset
    Dim SQL
    
    
    'define the connection string, specify database driver
    ConnString="DRIVER={SQL Server};SERVER=LOCALHOST\SQLEXPRESS;UID=xxxx;PWD=x  xxx;DATABASE=xxxx"
    
    
    'create an instance of the ADO connection and recordset objects
    Set Connection = Server.CreateObject("ADODB.Connection")
    
    'Open the connection to the database
    Connection.Open ConnString
    
    cid=Request.Form("ID")
    
    if Request.form("ID")="" then
      set Recordset=Server.CreateObject("ADODB.Recordset")
      Recordset.open "SELECT ID, Customer, JobName, QuoteStatus, QuoteDate, Job, Status, DueDate, Value FROM Jobs WHERE ID='" & cid & "'",Connection
      %>
     
     
     
    <form method="post" action="update.asp">
    <table cellpadding="10px" cellspacing="0px">
    
    <tr bgcolor="#DFDFDF">
    <td>ID:</td>
    <td><input name="ID" size="50"></td>
    </tr>
    
    <tr bgcolor="#DFDFDF">
    <td>Customer:</td>
    <td><input name="Customer" size="50"></td>
    </tr>
    
    <tr bgcolor="#DFDFDF">
    <td>Job Name:</td>
    <td><input name="JobName" size="50"></td>
    </tr>
    
    <tr bgcolor="#DFDFDF">
    <td>QuoteStatus:</td>
    <td><input name="QuoteStatus" size="50"></td>
    </tr>
    
    <tr bgcolor="#DFDFDF">
    <td>QuoteDate:</td>
    <td><input name="QuoteDate" size="50"></td>
    </tr>
    
    <tr bgcolor="#DFDFDF">
    <td>Job:</td>
    <td><input name="Job" size="50"></td>
    </tr>
    
    <tr bgcolor="#DFDFDF">
    <td>Status:</td>
    <td><input name="Status" size="50"></td>
    </tr>
    
    <tr bgcolor="#DFDFDF">
    <td>DueDate:</td>
    <td><input name="DueDate" size="50"></td>
    </tr>
    
    <tr bgcolor="#DFDFDF">
    <td>Value:</td>
    <td><input name="Value" size="50"></td>
    </tr>
    
    </table>
    <br /><br />
    <input type="submit" value="Save">
    <INPUT TYPE="BUTTON" VALUE="Cancel" ONCLICK="window.location.href='default.asp'"> 
    </form>
     
    
    <%
    else
      sql="UPDATE Jobs SET "
      sql=sql & "Quote='" & Request.Form("Quote") & "',"
      sql=sql & "Customer='" & Request.Form("Customer") & "',"
      sql=sql & "JobName='" & Request.Form("JobName") & "',"
      sql=sql & "QuoteStatus='" & Request.Form("QuoteStatus") & "',"
      sql=sql & "QuoteDate='" & Request.Form("QuoteDate") & "',"
      sql=sql & "Job='" & Request.Form("Job") & "',"
      sql=sql & "Status='" & Request.Form("Status") & "',"
      sql=sql & "DueDate='" & Request.Form("DueDate") & "',"
      sql=sql & "Value='" & Request.Form("Value") & "'"
      sql=sql & " WHERE ID=" & Request.Form("ID")
      on error resume next
      Connection.Execute sql
      if err<>0 then
        response.write("No update permissions!")
        Response.Write err.description
      else
      Response.Redirect ("default.asp")
      end if
    end if
    Connection.close
    %>
    
    Code (markup):
     
    vidus, Aug 18, 2010 IP
  2. Lucas Rodrigues

    Lucas Rodrigues Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    on the form item set the value with a response.write: <input value="<%=nameoftherecordset(nameofthefieldfromthesqlquery)%>" name="itsname">
     
    Lucas Rodrigues, Aug 23, 2010 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    this code here is wrong:
    
    cid=Request.Form("ID")
    [B]
    if Request.form("ID")="" then[/B]
      set Recordset=Server.CreateObject("ADODB.Recordset")
      Recordset.open "SELECT ID, Customer, JobName, QuoteStatus, QuoteDate, Job, Status, DueDate, Value FROM Jobs WHERE ID='" & cid & "'",Connection
    
    Code (markup):
    I think you mean:
    
    if Request.form("ID")[B]<>[/B]"" then
    
    Code (markup):
    But I would use a querystring so you can easily link to the edit page like: edit.asp?id=12

    As for loading the values it is easy:
    
    <input name="Customer" size="50" value="<%=Recordset("Customer").value %>">
    
    Code (markup):
    and the same for the rest of the form....
     
    camjohnson95, Aug 23, 2010 IP