Quickstart Problem

Discussion in 'ASP .Net' started by =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?=, Sep 27, 2005.

  1. I'm trying to use the Insert.aspx Quickstart and I'm getting a NULL pointer
    exception. Any help?

    <code>
    <script language="VB" runat="server">
    Dim myConnection As SqlConnection

    Sub Page_Load(Src As Object, e As EventArgs)
    ' Create a connection to the SQL Server
    myConnection = New SqlConnection("Data Source=SERVER;" _
    & "Initial Catalog=database;User Id=user;Password=password;")
    ' Check whether this page is a postback. If it is not
    ' a postback, call a custom BindGrid function.
    If Not IsPostBack Then
    BindGrid()
    End If
    End Sub

    ' Implement an AddAsset_Click function. This function does some data
    ' validation on the input form and builds a parameterized command
    containing
    ' all the fields of the input form. Then it executes this command to the
    ' database and tests (using the try command) whether the data was added.
    ' Finally, it rebinds the DataGrid to show the new data.
    Sub AddAsset_Click(Sender As Object, e As EventArgs)
    Dim myCommand As SqlCommand
    Dim insertCmd As String
    ' Check the Cube and Computer Serial input values and make sure they
    are not
    ' empty. If they are empty, show a message to the user and rebind the
    DataGrid.
    If (cube.Value = "" Or computer_serial.Value = "")
    Message.InnerHtml = "ERROR: You MUST enter a Cube and Computer S/N "
    Message.Style("color") = "red"
    BindGrid()
    Exit Sub
    End If
    ' Build a SQL INSERT statement string for all the input-form
    ' field values.
    insertCmd = "insert into myTable values (@cube, @monitor_type,
    @monitor_serial, @acd, " _
    & "@ext, @computer_type, @computer_serial, @hostname,
    @second_comp_serial, @second_hostname);"
    ' Initialize the SqlCommand with the new SQL string.
    myCommand = New SqlCommand(insertCmd, myConnection)

    ' Create new parameters for the SqlCommand object and
    ' initialize them to the input-form field values.

    'Cube data
    myCommand.Parameters.Add(New SqlParameter("@cube", _
    SqlDbType.VarChar, 50))
    myCommand.Parameters("@cube").Value = cube.Value

    'Monitor Type data
    myCommand.Parameters.Add(New SqlParameter("@monitor_type", _
    SqlDbType.VarChar, 50))
    myCommand.Parameters("@monitor_type").Value = monitor_type.Value

    'Monitor Serial data
    myCommand.Parameters.Add(New SqlParameter("@monitor_serial", _
    SqlDbType.VarChar, 50))
    myCommand.Parameters("@monitor_serial").Value = monitor_serial.Value

    'ACD (position ID) data
    myCommand.Parameters.Add(New SqlParameter("@acd", _
    SqlDbType.VarChar, 4))
    myCommand.Parameters("@acd").Value = acd.Value

    'Extension data
    myCommand.Parameters.Add(New SqlParameter("@ext", _
    SqlDbType.VarChar, 12))
    myCommand.Parameters("@ext").Value = ext.Value

    'Computer Type data
    myCommand.Parameters.Add(New SqlParameter("@computer_type", _
    SqlDbType.VarChar, 20))
    myCommand.Parameters("@computer_type").Value = computer_type.Value

    'Computer Serial data
    myCommand.Parameters.Add(New SqlParameter("@computer_serial", _
    SqlDbType.VarChar, 50))
    myCommand.Parameters("@computer_serial").Value = computer_serial.Value

    'Hostname data
    myCommand.Parameters.Add(New SqlParameter("@hostname", _
    SqlDbType.VarChar, 50))
    myCommand.Parameters("@hostname").Value = hostname.Value

    '2nd Computer Serial data
    myCommand.Parameters.Add(New SqlParameter("@second_comp_serial", _
    SqlDbType.VarChar,50))
    myCommand.Parameters("@second_comp_serial").Value =
    second_comp_serial.Value

    '2nd Hostname data
    myCommand.Parameters.Add(New SqlParameter("@second_hostname", _
    SqlDbType.VarChar,50))
    myCommand.Parameters("@second_hostname").Value = second_hostname.Value

    myCommand.Connection.Open() THIS IS WHERE THE EXCEPTION IS
    ' Test whether the new row can be added and display the
    ' appropriate message box to the user.
    Try
    myCommand.ExecuteNonQuery()
    Message.InnerHtml = "<b>Record Added</b><br>" & insertCmd
    Catch ex As SqlException
    If ex.Number = 2627 Then
    Message.InnerHtml = "ERROR: A record already exists with " _
    & "the same Computer Serial Number"
    Else
    Message.InnerHtml = "ERROR: Could not add record, please " _
    & "ensure the fields are correctly filled out"
    Message.Style("color") = "red"
    End If
    End Try

    myCommand.Connection.Close()
    BindGrid()
    End Sub

    ' BindGrid connects to the database and implements a SQL
    ' SELECT query to get all the data in the table
    ' of the database.
    Sub BindGrid()
    Dim myConnection As SqlConnection
    Dim myCommand As SqlDataAdapter
    ' Create a connection to the "database" SQL Server
    myConnection = New SqlConnection("data source=SERVER;" _
    & "user id=user;password=password;initial catalog=Database")
    ' Connect to the SQL database using a SQL SELECT query to get all
    ' the data from the table.
    myCommand = New SqlDataAdapter("SELECT * FROM myTable", _
    myConnection)
    ' Create and fill a new DataSet.
    Dim ds As DataSet = New DataSet()
    myCommand.Fill(ds)
    ' Bind the DataGrid control to the DataSet.
    dgInserted.DataSource = ds
    dgInserted.DataBind()
    End Sub
    </script>
    </code>


    I'm getting a NULL pointer exception on:

    myCommand.Connection.Open()


    Stack Trace:


    [NullReferenceException: Object reference not set to an instance of an
    object.]
    ASP.conInsert_aspx.AddAsset_Click(Object Sender, EventArgs e) in
    C:\inventory\Insert.aspx:96
    System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e)

    System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument)
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    System.Web.UI.Page.ProcessRequestMain()

    Everything looks good to me. Most of the code is from the MSDN Quickstart
    and I've just tweaked it to allow for my database.

    AM I MISSING SOMETHING?
     
    =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?=, Sep 27, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?RXVzdGljZSBTY3J1YmI=?=

    Pat Guest

    Eustice mabe something is wrong with your connectionString?
    PAtrick

    "Eustice Scrubb" <> wrote in message
    news:...
    > I'm trying to use the Insert.aspx Quickstart and I'm getting a NULL

    pointer
    > exception. Any help?
    >
    > <code>
    > <script language="VB" runat="server">
    > Dim myConnection As SqlConnection
    >
    > Sub Page_Load(Src As Object, e As EventArgs)
    > ' Create a connection to the SQL Server
    > myConnection = New SqlConnection("Data Source=SERVER;" _
    > & "Initial Catalog=database;User Id=user;Password=password;")
    > ' Check whether this page is a postback. If it is not
    > ' a postback, call a custom BindGrid function.
    > If Not IsPostBack Then
    > BindGrid()
    > End If
    > End Sub
    >
    > ' Implement an AddAsset_Click function. This function does some data
    > ' validation on the input form and builds a parameterized command
    > containing
    > ' all the fields of the input form. Then it executes this command to

    the
    > ' database and tests (using the try command) whether the data was

    added.
    > ' Finally, it rebinds the DataGrid to show the new data.
    > Sub AddAsset_Click(Sender As Object, e As EventArgs)
    > Dim myCommand As SqlCommand
    > Dim insertCmd As String
    > ' Check the Cube and Computer Serial input values and make sure they
    > are not
    > ' empty. If they are empty, show a message to the user and rebind

    the
    > DataGrid.
    > If (cube.Value = "" Or computer_serial.Value = "")
    > Message.InnerHtml = "ERROR: You MUST enter a Cube and Computer

    S/N "
    > Message.Style("color") = "red"
    > BindGrid()
    > Exit Sub
    > End If
    > ' Build a SQL INSERT statement string for all the input-form
    > ' field values.
    > insertCmd = "insert into myTable values (@cube, @monitor_type,
    > @monitor_serial, @acd, " _
    > & "@ext, @computer_type, @computer_serial, @hostname,
    > @second_comp_serial, @second_hostname);"
    > ' Initialize the SqlCommand with the new SQL string.
    > myCommand = New SqlCommand(insertCmd, myConnection)
    >
    > ' Create new parameters for the SqlCommand object and
    > ' initialize them to the input-form field values.
    >
    > 'Cube data
    > myCommand.Parameters.Add(New SqlParameter("@cube", _
    > SqlDbType.VarChar, 50))
    > myCommand.Parameters("@cube").Value = cube.Value
    >
    > 'Monitor Type data
    > myCommand.Parameters.Add(New SqlParameter("@monitor_type", _
    > SqlDbType.VarChar, 50))
    > myCommand.Parameters("@monitor_type").Value = monitor_type.Value
    >
    > 'Monitor Serial data
    > myCommand.Parameters.Add(New SqlParameter("@monitor_serial", _
    > SqlDbType.VarChar, 50))
    > myCommand.Parameters("@monitor_serial").Value = monitor_serial.Value
    >
    > 'ACD (position ID) data
    > myCommand.Parameters.Add(New SqlParameter("@acd", _
    > SqlDbType.VarChar, 4))
    > myCommand.Parameters("@acd").Value = acd.Value
    >
    > 'Extension data
    > myCommand.Parameters.Add(New SqlParameter("@ext", _
    > SqlDbType.VarChar, 12))
    > myCommand.Parameters("@ext").Value = ext.Value
    >
    > 'Computer Type data
    > myCommand.Parameters.Add(New SqlParameter("@computer_type", _
    > SqlDbType.VarChar, 20))
    > myCommand.Parameters("@computer_type").Value = computer_type.Value
    >
    > 'Computer Serial data
    > myCommand.Parameters.Add(New SqlParameter("@computer_serial", _
    > SqlDbType.VarChar, 50))
    > myCommand.Parameters("@computer_serial").Value =

    computer_serial.Value
    >
    > 'Hostname data
    > myCommand.Parameters.Add(New SqlParameter("@hostname", _
    > SqlDbType.VarChar, 50))
    > myCommand.Parameters("@hostname").Value = hostname.Value
    >
    > '2nd Computer Serial data
    > myCommand.Parameters.Add(New SqlParameter("@second_comp_serial", _
    > SqlDbType.VarChar,50))
    > myCommand.Parameters("@second_comp_serial").Value =
    > second_comp_serial.Value
    >
    > '2nd Hostname data
    > myCommand.Parameters.Add(New SqlParameter("@second_hostname", _
    > SqlDbType.VarChar,50))
    > myCommand.Parameters("@second_hostname").Value =

    second_hostname.Value
    >
    > myCommand.Connection.Open() THIS IS WHERE THE EXCEPTION IS
    > ' Test whether the new row can be added and display the
    > ' appropriate message box to the user.
    > Try
    > myCommand.ExecuteNonQuery()
    > Message.InnerHtml = "<b>Record Added</b><br>" & insertCmd
    > Catch ex As SqlException
    > If ex.Number = 2627 Then
    > Message.InnerHtml = "ERROR: A record already exists with " _
    > & "the same Computer Serial Number"
    > Else
    > Message.InnerHtml = "ERROR: Could not add record, please " _
    > & "ensure the fields are correctly filled out"
    > Message.Style("color") = "red"
    > End If
    > End Try
    >
    > myCommand.Connection.Close()
    > BindGrid()
    > End Sub
    >
    > ' BindGrid connects to the database and implements a SQL
    > ' SELECT query to get all the data in the table
    > ' of the database.
    > Sub BindGrid()
    > Dim myConnection As SqlConnection
    > Dim myCommand As SqlDataAdapter
    > ' Create a connection to the "database" SQL Server
    > myConnection = New SqlConnection("data source=SERVER;" _
    > & "user id=user;password=password;initial catalog=Database")
    > ' Connect to the SQL database using a SQL SELECT query to get all
    > ' the data from the table.
    > myCommand = New SqlDataAdapter("SELECT * FROM myTable", _
    > myConnection)
    > ' Create and fill a new DataSet.
    > Dim ds As DataSet = New DataSet()
    > myCommand.Fill(ds)
    > ' Bind the DataGrid control to the DataSet.
    > dgInserted.DataSource = ds
    > dgInserted.DataBind()
    > End Sub
    > </script>
    > </code>
    >
    >
    > I'm getting a NULL pointer exception on:
    >
    > myCommand.Connection.Open()
    >
    >
    > Stack Trace:
    >
    >
    > [NullReferenceException: Object reference not set to an instance of an
    > object.]
    > ASP.conInsert_aspx.AddAsset_Click(Object Sender, EventArgs e) in
    > C:\inventory\Insert.aspx:96
    > System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e)
    >
    >

    System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandl
    er.RaisePostBackEvent(String eventArgument)
    > System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    > sourceControl, String eventArgument)
    > System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    > System.Web.UI.Page.ProcessRequestMain()
    >
    > Everything looks good to me. Most of the code is from the MSDN Quickstart
    > and I've just tweaked it to allow for my database.
    >
    > AM I MISSING SOMETHING?
    >
    >
     
    Pat, Sep 27, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. t322y
    Replies:
    1
    Views:
    3,527
  2. LL

    QuickStart sample?

    LL, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    344
  3. Abhishek Srivastava

    IIS configuration problems with quickstart samples

    Abhishek Srivastava, Jan 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    398
    Dhaval Faria
    Jan 2, 2004
  4. Bryan Ax
    Replies:
    0
    Views:
    304
    Bryan Ax
    Apr 13, 2004
  5. NNTP
    Replies:
    2
    Views:
    990
    =?Utf-8?B?S2V2aW4gVG9kZA==?=
    Mar 18, 2005
Loading...

Share This Page