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>
|