Wrox Programmer Forums
|
BOOK: Beginning ASP.NET Databases Also see the forum ASP Databases for more general discussions of ASP database issues not directly related to these books.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 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 July 21st, 2003, 03:37 PM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert Problem

I'm atemping to create a page that will insert new data into my database, the page runs and executes everything but the actual database is not updated, is there another line of code i need in order to do this, thanks

<%@ Page Language="c#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    OleDbConnection objConnection;
    private void Page_Load(object sender, System.EventArgs e)
    {
    objConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=AAC.mdb");
    }

    private void btnInsert_Click(object sender, System.EventArgs e)
    {
    if (Page.IsValid)
    {
    String strSQL = "INSERT INTO Hardware (Type, Location, Manufacturer, ModelNumber, SerialNumber, Other) VALUES (?, ?, ?, ?, ?, ?)";
    OleDbCommand dbComm = new OleDbCommand(strSQL, objConnection);
    dbComm.Parameters.Add("Type", OleDbType.VarChar, 32, "Type");
    dbComm.Parameters.Add("Location", OleDbType.VarChar, 32, "Location");
    dbComm.Parameters.Add("Manufacturer", OleDbType.VarChar, 32, "Manufacturer");
    dbComm.Parameters.Add("ModelNumber", OleDbType.VarChar, 32, "ModelNumber");
    dbComm.Parameters.Add("SerialNumber", OleDbType.VarChar, 32, "SerialNumber");
    dbComm.Parameters.Add("Other", OleDbType.VarChar, 32, "Other");
    dbComm.Parameters["Type"].Value = txtType.Text;
    dbComm.Parameters["Location"].Value = txtLocation.Text;
    dbComm.Parameters["Manufacturer"].Value = txtManufacturer.Text;
    dbComm.Parameters["ModelNumber"].Value = txtModelNumber.Text;
    dbComm.Parameters["SerialNumber"].Value = txtSerialNumber.Text;
    dbComm.Parameters["Other"].Value = txtOther.Text;
    try
    {
    objConnection.Open();
    dbComm.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    Response.End();
    }
    finally
    {
    if (objConnection.State == ConnectionState.Open);
    objConnection.Close();
    }
    Response.Write("A new record has been added");
    Response.End();
    }
    }

</script>
<html>
<head>
    <title>Adding a New Record</title>
</head>
<body>
    <form id="Form1" method="post" runat="server">
        <table id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellspacing="0" cellpadding="0" width="300" border="0">
            <tbody>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label1" runat="server">Type</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtType" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label2" runat="server">Location</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtLocation" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label3" runat="server">Manufacturer</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtManufacturer" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label4" runat="server">ModelNumber</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtModelNumber" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label5" runat="server">SerialNumber</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtSerialNumber" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label6" runat="server">Other</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtOther" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button id="btnInsert" onclick="btnInsert_Click" runat="server" width="298" text="INSERT"></asp:Button>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>


 
Old July 21st, 2003, 03:43 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Aac,

Of course the path doesn't work as I cannot possible know where your database resides ;) The point was, you should add a path to the database name. The right path in your case, is the exact location of your database on your local file system. C:\inetpub\wwwroot\MySite\Database\AAC.mdb was just an example, to be replaced by you.

I also noticed that you use Page.IsValid but that you are not using any validators. I am not sure about this, but is a page valid when there are no validators at all? IMO, it makes sense that it is, but you could check anyway.

Imar



---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old July 21st, 2003, 05:22 PM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've typed in my own path and i'm still getting an error, ive checked it many times over with co-workers and they can't find any problem with the path i have typed, i was using code from a book as a template for my own involving a validator but i didn't want the validator and forgot to remove the check for it, thankyou for the reminder, i'll get ride of that problem, any ideas about what i could be doing wrong with my path to my database?

"Provider=Microsoft.Jet.OLEDB.4.0; data source=C:\inetpub\wwwroot\tae\data\AAC.mdb"
i know you dont know where my database is located but do you see anything that is obviosly wrong?



 
Old July 22nd, 2003, 12:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

OK, and what error do you get? In an earlier post you said the page runs fine.... An error message would really help diagnosing the problem.

Did you make sure that the ASPNET account has sufficient rights to the database on the File System level? It needs change permission for the .mdb file and the folder it resides in (it needs to create temp files there).

If you haven't done this, open the Properties for your data folder, open the Security tab and give the ASPNET account at least change rights....

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old July 22nd, 2003, 10:58 AM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The page ran fine but didn't make the changes to the actual data base it only made changes to temp files, but when i tried to make a full physical path i'm getting this error message: "CS1009: Unrecognized escape sequence", i gave ASPNET account change rights,
thanks for your help

 
Old July 22nd, 2003, 12:27 PM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

problem solved,
thanks for your help

 
Old July 22nd, 2003, 02:04 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Would you mind sharing your solution? I am sure others will run into the same problem in the future, and nothing is as frustrating as a post with your exact problem, and no solution ;)

Cheers,

Imar


Quote:
quote:Originally posted by aac
 problem solved,
thanks for your help

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old July 22nd, 2003, 02:09 PM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well, ok, its a little embarrassing how stupid this error was but because my computer is a mess i have copies of this file in many places, it was actually being updated i was just looking in the wrong file

 
Old July 22nd, 2003, 02:16 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

;) Oh well, nothing to be embarrassed about. I am sure this happens to everyone once in a while.

Regards,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
insert problem lscjtw XSLT 2 August 3rd, 2007 10:29 AM
insert into problem yami56 Access 3 March 3rd, 2005 05:16 AM
Validation and Insert problem addos Beginning PHP 0 January 17th, 2005 06:50 PM
problem with insert iosqar JSP Basics 0 May 19th, 2004 10:07 AM
Insert problem CW Classic ASP Databases 14 September 9th, 2003 04:52 AM





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