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 July 15th, 2003, 10:32 AM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object reference not set to an instance of an obje

I have created a page to insert new data into my database but when i try to insert it i get this error message: "Object reference not set to an instance of an object.", does any one have any ideas?
Thanks for you time,

 
Old July 16th, 2003, 04:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Show us the code.
 
Old July 16th, 2003, 10:19 AM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

here's the code

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

    OleDbConnection objConnection;

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

    private void btnInsert_Click(object sender, System.EventArgs e)
    {
    if (Page.IsValid)
    {
    String strSQL = "INSERT INTO Computers (Location, User, TypeOfComputer, Manufacturer, ModelNumber, SerialNumber, DatePurchased, DateGarunteed, DateMaintenence, CPU, Memory, HardDrive, CDROM, SoundCard, VideoCard, NetworkID, PrinterType, OtherEquipment, OS, ApplicationSoftware, NetworkCard, NetworkConnection, Update) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    OleDbCommand dbComm = new OleDbCommand(strSQL, objConnection);
    dbComm.Parameters.Add("Location", OleDbType.VarChar, 32, "Location");
    dbComm.Parameters.Add("User", OleDbType.VarChar, 32, "User");
    dbComm.Parameters.Add("TypeOfComputer", OleDbType.VarChar, 32, "TypeOfComputer");
    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("DatePurchased", OleDbType.VarChar, 32, "DatePurchased");
    dbComm.Parameters.Add("DateGarunteed", OleDbType.VarChar, 32, "DateGarunteed");
    dbComm.Parameters.Add("DateMaintenence", OleDbType.VarChar, 32, "DateMaintenence");
    dbComm.Parameters.Add("CPU", OleDbType.VarChar, 32, "CPU");
    dbComm.Parameters.Add("Memory", OleDbType.VarChar, 32, "Memory");
    dbComm.Parameters.Add("HardDrive", OleDbType.VarChar, 32, "HardDrive");
    dbComm.Parameters.Add("CDROM", OleDbType.VarChar, 32, "CDROM");
    dbComm.Parameters.Add("SoundCard", OleDbType.VarChar, 32, "SoundCard");
    dbComm.Parameters.Add("VideoCard", OleDbType.VarChar, 32, "VideoCard");
    dbComm.Parameters.Add("NetworkID", OleDbType.VarChar, 32, "NetworkID");
    dbComm.Parameters.Add("PrinterType", OleDbType.VarChar, 32, "PrinterType");
    dbComm.Parameters.Add("OtherEquipment", OleDbType.VarChar, 32, "OtherEquipment");
    dbComm.Parameters.Add("OS", OleDbType.VarChar, 32, "OS");
    dbComm.Parameters.Add("ApplicationSoftware", OleDbType.VarChar, 32, "ApplicationSoftware");
    dbComm.Parameters.Add("NetworkCard", OleDbType.VarChar, 32, "NetworkCard");
    dbComm.Parameters.Add("NetworkConnection", OleDbType.VarChar, 32, "NetworkConnection");
    dbComm.Parameters.Add("Update", OleDbType.VarChar, 32, "Update");
    dbComm.Parameters["Location"].Value = txtLocation.Text;
    dbComm.Parameters["User"].Value = txtUser.Text;
    dbComm.Parameters["TypeOfComputer"].Value = txtTypeOfComputer.Text;
    dbComm.Parameters["Manufacturer"].Value = txtManufacturer.Text;
    dbComm.Parameters["ModelNumber"].Value = txtModelNumber.Text;
    dbComm.Parameters["SerialNumber"].Value = txtSerialNumber.Text;
    dbComm.Parameters["DatePurchased"].Value = txtDatePurchased.Text;
    dbComm.Parameters["DateGarunteed"].Value = txtDateGarunteed.Text;
    dbComm.Parameters["DateMaintenence"].Value = txtDateMaintenence.Text;
    dbComm.Parameters["CPU"].Value = txtCPU.Text;
    dbComm.Parameters["Memory"].Value = txtMemory.Text;
    dbComm.Parameters["HardDrive"].Value = txtHardDrive.Text;
    dbComm.Parameters["CDROM"].Value = txtCDROM.Text;
    dbComm.Parameters["SoundCard"].Value = txtSoundCard.Text;
    dbComm.Parameters["VideoCard"].Value = txtVideoCard.Text;
    dbComm.Parameters["NetworkID"].Value = txtNetworkID.Text;
    dbComm.Parameters["PrinterType"].Value = txtPrinterType.Text;
    dbComm.Parameters["OtherEquipment"].Value = txtOtherEquipment.Text;
    dbComm.Parameters["OS"].Value = txtOS.Text;
    dbComm.Parameters["ApplicationSoftware"].Value = txtApplicationSoftware.Text;
    dbComm.Parameters["NetworkCard"].Value = txtNetworkCard.Text;
    dbComm.Parameters["NetworkConnection"].Value = txtNetworkConnection.Text;
    dbComm.Parameters["Update"].Value = txtUpdate.Text;

    int iID = 0;
    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>Validating a Feild</title>
</head>
<body>
    <form id="Form1" methode="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">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="Label2" runat="server">User</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtUser" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label3" runat="server">Type of Computer</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtTypeOfComputer" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label4" 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="Label5" runat="server">Model Number</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtModelNumber" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label6" runat="server">Serial Number</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtSerialNumber" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label7" runat="server">Date Purchased</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtDatePurchased" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label8" runat="server">Date Garuntee Expires</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtDateGarunteed" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label9" runat="server">Date Maintenence Contract Renewal</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtDateMaintenence" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label10" runat="server">CPU</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtCPU" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label11" runat="server">Memory</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtMemory" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label12" runat="server">HardDrive</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtHardDrive" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label13" runat="server">CD-ROM</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtCDROM" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label14" runat="server">SoundCard</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtSoundCard" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label15" runat="server">VideoCard</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtVideoCard" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label16" runat="server">NetworkID</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtNetworkID" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label17" runat="server">Printer Type</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtPrinterType" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label18" runat="server">Other Equipment</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtOtherEquipment" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label19" runat="server">OS</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtOS" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label20" runat="server">Application Software</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtApplicationSoftware" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label21" runat="server">Network Card</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtNetworkCard" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label22" runat="server">Network Connection</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtNetworkConnection" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px">
                        <asp:Label id="Label23" runat="server">Update</asp:Label></td>
                    <td>
                        <asp:TextBox id="txtUpdate" runat="server" width="193"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="WIDTH: 115px" colspan="2">
                        <asp:Button id="btnInsert" onclick="btnInsert_Click" runat="server" width="298px" Height="27px" text="INSERT!"></asp:Button>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>


 
Old July 16th, 2003, 11:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

You have redeclared objConnection variable into Page_Load proc in the initialization statement, and initialization affects only into Page_Load proc's scope. You have to remove OleDbConnection declaration keyword in Page_Load proc.

...but the Soon is eclipsed by the Moon
 
Old July 16th, 2003, 11:59 AM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok, thank you that worked
but now i'm having a problem with my insert statement
error message: "Syntax error in INSERT INTO statement."
can you help?

 
Old July 16th, 2003, 12:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

Well,
I see that one field in your table named "Update". This is a keyword, and it is possible that this name is responsible for INSERT INTO error (maybe there are more error sources). You should avoid all names similar to some keywords. Some applications allow you to use them, but generally it is recommended to avoid them.

...but the Soon is eclipsed by the Moon
 
Old July 16th, 2003, 01:08 PM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok i made that change but i'm still getting the same error

 
Old July 16th, 2003, 03:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

Hmm... I think that "User" can be problematical, too. Try to play with some suspicious words used for field names...

...but the Soon is eclipsed by the Moon
 
Old July 16th, 2003, 04:25 PM
aac aac is offline
Authorized User
 
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i tried to change User so that it wouldn't be problematic, so i changed it to UserName and replaced User with UserName throught the code but now when i try and insert data i get this message "The INSERT INTO statement contains the following unknown field name: 'UserName'. Make sure you have typed the name correctly, and try the operation again.", i've looked throught the code many times makeing sure i had spelled it right but i can't seem to figure it out, i also changed the field name in my database to UserName, do you have any idea and will using UserName instead of USer fix the problem with keywords?

 
Old July 16th, 2003, 04:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

I tested particularly Update and User keywords, and I found that both words are problematical. I have replaced Update with Update1 and User with User1 (in the SQL statement and table structure, of course), and code worked OK.

...but the Soon is eclipsed by the Moon





Similar Threads
Thread Thread Starter Forum Replies Last Post
Object reference not set to an instance of an obje GS C# 2005 3 May 21st, 2007 10:11 AM
Object reference not set to an instance of an obje rturner003 ASP.NET 2.0 Professional 0 December 26th, 2006 08:30 PM
Object reference not set to an instance of an obje williadn General .NET 0 January 18th, 2005 05:39 PM
Object reference not set to an instance of an obje bekim Classic ASP Basics 1 August 9th, 2004 01:25 AM





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