473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Quickstart Problem

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.OnServe rClick(EventArgs e)

System.Web.UI.HtmlControls.HtmlInputButton.System. Web.UI.IPostBackEventHandler.RaisePostBackEvent(St ring eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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?
Nov 19 '05 #1
1 1519
Pat
Eustice mabe something is wrong with your connectionString?
PAtrick

"Eustice Scrubb" <Eu***********@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
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.OnServe rClick(EventArgs e)

System.Web.UI.HtmlControls.HtmlInputButton.System. Web.UI.IPostBackEventHandl
er.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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?

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Damon | last post by:
I installed a .NET on my windowsXP pro. And, I ran "C:\Program Files\Microsoft Visual Studio ..NET\FrameworkSDK\Samples\Setup\ConfigSamples.exe" and got same virtual directories. when I use...
0
by: Scott Dunaway | last post by:
I have Windows XP Pro with the newest SP and all updates. I have Visual Studio .NET 2002 When I installed VS.NET I didn't add iis nor the SQL server but then I decided to reverse that decision...
0
by: myleslawrence | last post by:
I hope this is the correct forum for this because I'm at wits end. I have a Win 2003 server, SQL server 2000 and the .Net framework sdk installed. The ASP.NET VB Quickstart tutorials have web...
1
by: t322y | last post by:
Hi All I installed Microsoft.Net Framework Version:1.0.3705.209 (included ASP.NET Version:1.0.3705.0) in my Win 2000 SP3 3 weeks ago. It worked well all until i installed a free softwore GoMono. I...
1
by: Abhishek Srivastava | last post by:
Hello All, I have installed the quick start samples that come along with .net sdk. I am facing some strange behavior on my machine. I would be gratefull if someone could explain what is going...
2
by: NNTP | last post by:
Hi, I tried to setup the quickstart and tutorial of ASP.NET framework in Window XP prof. with SP2. The system is running IIS and FrontPage 2000 server extension. I also downloaded the...
4
by: myleslawrence | last post by:
I hope this is the correct forum for this because I'm at wits end. I have a Win 2003 server, SQL server 2000 and the .Net framework sdk installed. The ASP.NET VB Quickstart tutorials have web...
3
by: moondaddy | last post by:
I'm checking out the Ent Lib for the first time and tried to run the Quickstart for data access. upon clicking the first button in the sample app I get an error saying there may be an error trying...
3
by: stacy | last post by:
On my XP Pro laptop, I have installed IIS, MSDE and QuickStart Samples but am encountering the following error when I click on any of the QuickStarts on the Microsoft.NET Framework SDK QuickStarts...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.