Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2004, 07:20 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert method doesnot work

I am trying to Insert authors record in database sql server
and in asp page. I have been trying for a while. I think the code does not work at function handle save(). In this method I am trying to send values to the data base and sql server

document.frmMain.txtID.value;
document.frmMain.txtLname.value;
It doesnot give me error. It doesnot do any thing in the database. Please help me with this situation.


<%@ Language=VBScript %>
 <%
'ASP Header
Option Explicit
Response.Buffer=true
Response.Expires=-1

Dim oConnection 'objConnection to connect to the database
Dim oRecordset 'objRecordset
Dim oCommand 'objCommand used for store procedure and sql

' Database connection string
Const sDatabaseConnectionString = "DATABASE=;DSN=;UID=;Password="


Dim mode

mode=Request.Form("hdnMode")
If mode= "" Then ' default value is blank
 mode = "Select"
End if

Select Case mode
 Case "Select"
       Call FetchAuthors()
Case "Delete"
     Call FetchAuthors()
     Call DeleteAuthor()
Case "Insert"
   Call FetchAuthors()
   Call InsertAuthor()
Case "Update"
  Call FetchAuthors()
  Call UpdateAuthor()
End Select

Sub FetchAuthors()
'Database Connection String
On Error Resume Next
Set oConnection=Server.CreateObject("ADODB.Connection" )
oConnection.ConnectionString=sDataBaseConnectionSt ring
oConnection.Open 'Connect to the database
   If Err.number <> 0 Then
        Response.Write("Problem establishing Database connection")
        Response.End
    End If

Set oCommand = Server.CreateObject("ADODB.Command")
oCommand.ActiveConnection=oConnection
oCommand.CommandType=1
oCommand.CommandText="Select * from authors"
Set oRecordset= oCommand.Execute
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''
 'INSERT Authors
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''
Sub InsertAuthor()
Dim id
Dim fname
Dim lname
Dim phone
Dim address
Dim city
Dim state
Dim zip
Dim contract
 id = Request.Form("txtID")
 lname =Request.Form("txtLName")
 fname =Request.Form("txtFName")
 phone =Request.Form("txtPhone")
 address =Request.Form("txtAddress")
 city=Request.Form("txtCity")
 state=Request.Form("txtState")
 zip=Request.Form("txtZip")
 contract=CByte(Request.Form("txtContract"))
 oRecordset.Fields("au_id")=id
 oRecordset.Fields("au_lname")=lname
 oRecordset.Fields("au_fname")=fname
 oRecordset.Fields("phone")=phone
 oRecordset.Fields("address")=address
 oRecordset.Fields("city")=city
 oRecordset.Fields("state")=state
 oRecordset.Fields("zip")=zip
 oRecordset.Fields("contract")=contract


 On Error Resume Next
Set oConnection=Server.CreateObject("ADODB.Connection" )
oConnection.ConnectionString=sDataBaseConnectionSt ring
oConnection.Open 'Connect to the database
   If Err.number <> 0 Then
        Response.Write("Problem establishing Database connection")
        Response.End
    End If

Set oCommand = Server.CreateObject("ADODB.Command")
oCommand.ActiveConnection=oConnection
oCommand.CommandType=1

oCommand.CommandText=BuildInsertSQL(id,lname,fname ,phone,address,city,state,zip,contract)
Response.Write(oCommand.CommandText)
oCommand.Execute
  If Err.number <> 0 Then
        Response.Write(Err.Description)

    End If
oConnection.Close ' Close the database connection and release memory
Set oConnection = Nothing
Set oCommand = Nothing
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''
' BuildInsertSQL - Creates the Insert SQL statement
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''
Function BuildInsertSQL(id,lname,fname,phone,address,city,s tate,zip,contract)
Dim sql
sql= "Insert into Authors Values('" &id& "','" &lname& "','"&fname&" ','" &phone&" ',' "&address&_
                                 "',' " &city& "','" &state& "','" &zip& "'," &contract& ")"
BuildInsertSQL =sql
End Function


%>

<html>
<Head>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<Script language="JavaScript">
/* *************************************
 HandleNew Handles the new Button Click
 ****************************** */
 function HandleNew(){
var entryFieldsHTML= "<Table align='center' width='250px'>" +
 "<TR><TD><B>Id</B></TD><TD><Input type='text' name='txtID' ></TD></TR>" +

 "<TR><TD><B>LastName</B></TD><TD><Input type='text' name='txtLName' ></TD></TR>" +
 "<TR><TD><B>FirstName</B></TD><TD><Input type='text' name='txtFName' ></TD></TR>"+
 "<TR><TD><B>Phone</B></TD><TD><Input type='text' name='txtPhone'></TD></TR>"+
 "<TR><TD><B>address</B></TD><TD><Input type='text' name='txtAddress'></TD></TR>"+

 "<TR><TD><B>City</B></TD><TD><Input type='text' name='txtCity' ></TD></TR>" +
 "<TR><TD><B>State</B></TD><TD><Input type='text' name='txtState' ></TD></TR>" +
 "<TR><TD><B>Zip</B></TD><TD><Input type='text' name='txtZip' ></TD></TR>" +
 "<TR><TD><B>Contract</B></TD><TD><Select id='Contract' name='txtContract'>" +
   "<Option value='1'>True</Option><Option value='0'>False</Option></Select></TD></TR>" +
 "<TR><TD align='center' colspan='2'><Input type='button' value='Save' id='btnSave' onclick='handleSave();'></TD></TR>"
 "</TABLE>"
 entryFields.innerHTML = entryFieldsHTML;

 }


 /* **************************************
handleSave Handles the New Button Click
***********************************/

function handleSave(){
    document.frmMain.txtID.value;
    document.frmMain.txtLName.value;
    document.frmMain.txtFName.value;
    document.frmMain.txtPhone.value;
    document.frmMain.txtAddress.value;
    document.frmMain.txtCity.value;
    document.frmMain.txtState.value;
    document.frmMain.txtZip.value;
    document.frmMain.txtContract.value;
    document.frmMain.submit();
}

</Script>
</Head>
<BODY >
<Table width="800px" border="1" align="center" >


        <TD align="center"><B>ID</B></TD>
        <TD align="center"><B>Last Name</B></TD>
        <TD><B>First Name</B></TD>
        <TD><B>Phone</B></TD>
        <TD><B>Address</B></TD>
        <TD><B>City</B></TD>
        <TD><B>State</B></TD>
        <TD ><B>Zip</B></TD>
        <TD ><B>Contract</B></TD>

<%Do until oRecordset.EOF%>
   <TR>
    <TD><%=oRecordset.Fields("au_id").Value%></TD>
    <TD><%=oRecordset.Fields("au_lname").Value%></TD>
    <TD><%=oRecordset.Fields("au_fname").Value%></TD>
    <TD><%=oRecordset.Fields("phone").Value%></TD>
    <TD><%=oRecordset.Fields("address").Value%></TD>
    <TD><%=oRecordset.Fields("city").Value%></TD>
    <TD><%=oRecordset.Fields("state").Value%></TD>
    <TD><%=oRecordset.Fields("zip").Value%></TD>
    <TD><%=oRecordset.Fields("contract").Value%></TD>
    </TR>
    <%oRecordset.MoveNext
Loop

%>

</Table>

<Form name="frmMain" method="post" action="Test Page 3.asp">
    <Input type="hidden" name="hdnMode" value="">
    <DIV id="entryFields"></DIV>
<Center>
    <Input type= "button" name="btnNew" value="New" onClick="HandleNew();">
</Center>
 </Body>







 
Old November 16th, 2004, 09:09 PM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 363
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

  Its better to post a little code. If its too long, we cannot go thru all the code. Anyhow, do response.write in the functions to check whether the control is reaching there or not. And also response.write the BuildInsertSQL to check how he sql statement formed.


------------
Rajani

 
Old November 17th, 2004, 05:05 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 function handleSave(){
 if (document.frmMain.txtID !="")
   document.frmMain.txtLName.value;
   document.frmMain.txtFName.value;
   document.frmMain.txtPhone.value;
   document.frmMain.txtAddress.value;
   document.frmMain.txtCity.value;
   document.frmMain.txtState.value;
   document.frmMain.txtZip.value;
   document.frmMain.txtContract.value;
 document.frmMain.submit();
 }

  The user enter the values in the txt boxes,Then I want to save the
values in the database and asp page by writing an ASP code. The code above does not enter. I also tried to check the values whether it accepts values. It seems that it doesnot accept values, but at the same time it doesnot give an error. Please guide me what is going on here. I am frustrated with this code.
Thanks



 
Old November 22nd, 2004, 07:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

If I understood that right, one should have valueX stored into something.
Quote:
quote:function handleSave(){
 if (document.frmMain.txtID !="")
   document.frmMain.txtLName.value;
   document.frmMain.txtFName.value;
   document.frmMain.txtPhone.value;
   document.frmMain.txtAddress.value;
   document.frmMain.txtCity.value;
   document.frmMain.txtState.value;
   document.frmMain.txtZip.value;
   document.frmMain.txtContract.value;
 document.frmMain.submit();
 }
 As per this code, what is that you are doing within the function? Are you trying to store the form values somewhere? If so where? I see NO EQUAL symbol there and it is not assigned to anything there. Can you explain us on what this code does?

Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
Would this php cookies embedding method work? Puripong PHP How-To 0 November 8th, 2007 10:34 AM
Replace method don't work with [ ] / skyler Javascript How-To 1 December 30th, 2006 10:04 AM
CompositeDataBoundControl's Render method not work bmains ASP.NET 2.0 Professional 0 January 12th, 2006 09:22 AM
Why normal insert querry doesnot work simsimlhr C# 2 January 9th, 2006 03:26 AM
POST method not working in LINUX Server , but work cnilashis Beginning PHP 2 August 19th, 2003 08:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.