Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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 October 9th, 2005, 12:23 AM
Authorized User
 
Join Date: Oct 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object reference not ....how to fix


I keep receiving "Object reference not set to an instance of an object" error when trying to run this code:

<%@ Page Language="vb" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    Sub Page_Load

        'lblquestion.text="Do you want to add another employee"
        username.text = "User Name:"
        password.text = "Password:"
        fname.text = "First Name:"
        lastName.text = "Last Name:"
        email.text = "Email:"


    End Sub
         Sub addUser(Sender As Object, E As EventArgs)
           Dim connectionString As String
           Dim queryString As String
           Dim data As New DataSet()
           Dim dbConnection As OleDbConnection
           Dim dataAdapter As OleDbDataAdapter


    ' set the connection and query details
           connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
                           "Data Source=" & Server.MapPath("db1.mdb")
           queryString = "SELECT username, password, frist_name, last_name, email FROM members;"

           ' open the connection and set the command
           dbConnection = New OledbConnection(connectionString)
           dataAdapter = New OledbDataAdapter(queryString, dbConnection)

           ' fill the dataset with the data
           dataAdapter.Fill(data, "members")


           ' -----------------------------------------------------------------
           ' add a new row to the table
           Dim table As DataTable
           Dim newRow As DataRow

           table = data.Tables(" members")

           newRow = table.NewRow()

           newRow("username") = txtfname.text
           newRow("password") = txtpass.text
           newRow("frist_name") = textfname.text
           newRow("last_name") = textlname.text
           newRow("email") = textemail.text

           table.Rows.Add(newRow)


          ' bind the data grid to the new data
           DataGrid.DataSource = table
           DataGrid.DataBind()

           ' ================================================== ===============
           ' generate the update commands
           Dim commandBuilder As OleDbCommandBuilder

           commandBuilder = New OleDbCommandBuilder(dataAdapter)

           dataAdapter.InsertCommand = commandBuilder.GetInsertCommand()

           ' ================================================== ===============
           ' update the data store
           dataAdapter.Update(data, "members")


            '================================================= ================

         End Sub

</script>
<html>
<head>
</head>
<body>
    <asp:Label id="lblquestion" runat="server"></asp:Label>
    <form runat="server">
        <table border="0">
            <tbody>
                <tr>
                    <td>
                        <asp:Label id="username" runat="server"></asp:Label></td>
                    <td>
                        <asp:TextBox id="txtfname" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="password" runat="server"></asp:Label></td>
                    <td>
                        <asp:TextBox id="txtpass" runat="server" textmode="password"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="fname" runat="server"></asp:Label></td>
                    <td>
                        <asp:TextBox id="textfname" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="lastName" runat="server"></asp:Label></td>
                    <td>
                        <asp:TextBox id="textlname" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="email" runat="server"></asp:Label></td>
                    <td>
                        <asp:TextBox id="textemail" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:button id="Add" onclick="addUser" runat="server" text="Add New"></asp:button>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
    <asp:DataGrid id="DataGrid" runat="server" visible="true"></asp:DataGrid>
</body>
</html>


thax for your help


 
Old October 9th, 2005, 01:59 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

What line is the error occuring on?


 
Old October 9th, 2005, 02:11 AM
Authorized User
 
Join Date: Oct 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here it is... i try ti fix but...:( i hope to help me to make it work ...thank you

Object reference not set to an instance of an object.


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 45: table = data.Tables(" members")
Line 46:
Line 47: newRow = table.NewRow()
Line 48:
Line 49: newRow("username") = textname.text


Source File: D:\assign2\newUser.aspx Line: 47

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   ASP.newUser_aspx.addUser(Object Sender, EventArgs E) in D:\assign2\newUser.aspx:47
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +83
   System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277



 
Old October 9th, 2005, 02:17 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Try this instead of trying to assign the table to a variable:

newRow = data.tables("members").NewRow()



 
Old October 9th, 2005, 05:44 AM
Authorized User
 
Join Date: Oct 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanx jbenson001,it works now but...

if i display it to DataGrid, it work fine but when i update to database, it show this message....however as i know , when you use the CommandBuilder object that automatically generates SQL commands the SelectCommand as basis. so you don't worry about the actual way the commands are constructed.

Syntax error in INSERT INTO statement.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.

Source Error:

Line 38: dbCommand.Parameters.Add(LastNameParam)
Line 39:
Line 40: dbCommand.ExecuteNonQuery()
Line 41:
Line 42: dbConnection.Close()


Source File: D:\assign2\adduser.aspx Line: 40

Stack Trace:

[OleDbException (0x80040e14): Syntax error in INSERT INTO statement.]
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextE rrorHandling(Int32 hr) +41
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextF orSingleResult(tagDBPARAMS dbParams, Object& executeResult) +122
   System.Data.OleDb.OleDbCommand.ExecuteCommandText( Object& executeResult) +92
   System.Data.OleDb.OleDbCommand.ExecuteCommand(Comm andBehavior behavior, Object& executeResult) +65
   System.Data.OleDb.OleDbCommand.ExecuteReaderIntern al(CommandBehavior behavior, String method) +112
   System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +66
   ASP.adduser_aspx.addUser(Object sender, EventArgs e) in D:\assign2\adduser.aspx:40
   System.Web.UI.WebControls.Button.onclick(EventArgs e) +83
   System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277

can you help me to fix it ? thanh cu` :D


 
Old October 9th, 2005, 05:47 AM
Authorized User
 
Join Date: Oct 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry, i'm wrong...this is the correct one.

Server Error in '/' Application.

Syntax error in INSERT INTO statement.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.

Source Error:

Line 77: ' ================================================== ===============
Line 78: ' update the data store
Line 79: dataAdapter.Update(data,"members")
Line 80:
Line 81:


Source File: D:\assign2\newUser.aspx Line: 79

Stack Trace:

[OleDbException (0x80040e14): Syntax error in INSERT INTO statement.]
   System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) +1644
   System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) +152
   ASP.newUser_aspx.addUser(Object Sender, EventArgs E) in D:\assign2\newUser.aspx:79
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +83
   System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +58
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277

 
Old October 9th, 2005, 11:32 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Try using a Try .. Catch block to get a more descriptive error message.

response.write ex.Message.
Also try to write out the Insert or Update command so you can see how it is being constructed.

 
Old December 28th, 2005, 05:30 AM
Authorized User
 
Join Date: Dec 2004
Posts: 69
Thanks: 0
Thanked 5 Times in 5 Posts
Send a message via Yahoo to whiterainbow
Default

The syntax error may cause because of the database field mismatch in your code. If you are passing a string to a numerical field or if you didn't finished the complete code of insert command only will generate the problem. so check with the datas you are passing to the commandtext object and the datatype of the parameter you are sending to the commandtext object

Thanks and Regards,

Senthil Kumar M.

[email protected]
http://www.leanlearn.tk





Similar Threads
Thread Thread Starter Forum Replies Last Post
object reference not set to instant of an object shahidrasul ASP.NET 2.0 Basics 1 September 5th, 2008 02:01 PM
Please Help Me to fix this ERRORNYA:"object refere delion Pro Visual Basic 2005 3 May 17th, 2007 11:40 AM
Object Reference not set to an instance of object srinivas_chakka ASP.NET 1.0 and 1.1 Professional 0 February 8th, 2006 11:56 AM





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