473,378 Members | 1,360 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,378 software developers and data experts.

Insert.aspx throwing exception

In line coding problem. Here's my code:

Expand|Select|Wrap|Line Numbers
  1. <script language="VB" runat="server">
  2. Dim myConnection As SqlConnection
  3.  
  4. Sub Page_Load(Src As Object, e As EventArgs)
  5. ' Create a connection to the SQL Server
  6. myConnection = New SqlConnection("Data Source=SERVER;" _
  7. & "Initial Catalog=database;User Id=user;Password=password;")
  8. ' Check whether this page is a  postback. If it is not
  9. ' a  postback, call a custom BindGrid function.
  10. If Not IsPostBack Then
  11. BindGrid()
  12. End If
  13. End Sub
  14.  
  15. ' Implement an AddAsset_Click function. This function does some data
  16. ' validation on the input form and builds a parameterized command
  17. containing
  18. ' all the fields of the input form.  Then it executes this command to the
  19. ' database and tests (using the try command) whether the data was added.
  20. ' Finally, it rebinds the DataGrid to show the new data.
  21. Sub AddAsset_Click(Sender As Object, e As EventArgs)
  22. Dim myCommand As SqlCommand
  23. Dim insertCmd As String
  24. ' Check the Cube and Computer Serial input values and make sure they
  25. are not
  26. '  empty. If they are empty, show a message to the user and rebind the
  27. DataGrid.
  28. If (cube.Value = "" Or computer_serial.Value = "")
  29. Message.InnerHtml = "ERROR: You MUST enter a Cube and Computer S/N "
  30. Message.Style("color") = "red"
  31. BindGrid()
  32. Exit Sub
  33. End If
  34. ' Build a SQL INSERT statement string for all the input-form
  35. ' field values.
  36. insertCmd = "insert into myTable values (@cube, @monitor_type,
  37. @monitor_serial, @acd, " _
  38. & "@ext, @computer_type, @computer_serial, @hostname,
  39. @second_comp_serial, @second_hostname);"
  40. ' Initialize the SqlCommand with the new SQL string.
  41. myCommand = New SqlCommand(insertCmd, myConnection)
  42.  
  43. ' Create new parameters for the SqlCommand object and
  44. ' initialize them to the input-form field values.
  45.  
  46. 'Cube data
  47. myCommand.Parameters.Add(New SqlParameter("@cube", _
  48. SqlDbType.VarChar, 50))
  49. myCommand.Parameters("@cube").Value = cube.Value
  50.  
  51. 'Monitor Type data
  52. myCommand.Parameters.Add(New SqlParameter("@monitor_type", _
  53. SqlDbType.VarChar, 50))
  54. myCommand.Parameters("@monitor_type").Value = monitor_type.Value
  55.  
  56. 'Monitor Serial data
  57. myCommand.Parameters.Add(New SqlParameter("@monitor_serial", _
  58. SqlDbType.VarChar, 50))
  59. myCommand.Parameters("@monitor_serial").Value = monitor_serial.Value
  60.  
  61. 'ACD (position ID) data
  62. myCommand.Parameters.Add(New SqlParameter("@acd", _
  63. SqlDbType.VarChar, 4))
  64. myCommand.Parameters("@acd").Value = acd.Value
  65.  
  66. 'Extension data
  67. myCommand.Parameters.Add(New SqlParameter("@ext", _
  68. SqlDbType.VarChar, 12))
  69. myCommand.Parameters("@ext").Value = ext.Value
  70.  
  71. 'Computer Type data
  72. myCommand.Parameters.Add(New SqlParameter("@computer_type", _
  73. SqlDbType.VarChar, 20))
  74. myCommand.Parameters("@computer_type").Value = computer_type.Value
  75.  
  76. 'Computer Serial data
  77. myCommand.Parameters.Add(New SqlParameter("@computer_serial", _
  78. SqlDbType.VarChar, 50))
  79. myCommand.Parameters("@computer_serial").Value = computer_serial.Value
  80.  
  81. 'Hostname data
  82. myCommand.Parameters.Add(New SqlParameter("@hostname", _
  83. SqlDbType.VarChar, 50))
  84. myCommand.Parameters("@hostname").Value = hostname.Value
  85.  
  86. '2nd Computer Serial data
  87. myCommand.Parameters.Add(New SqlParameter("@second_comp_serial", _
  88. SqlDbType.VarChar,50))
  89. myCommand.Parameters("@second_comp_serial").Value =
  90. second_comp_serial.Value
  91.  
  92. '2nd Hostname data
  93. myCommand.Parameters.Add(New SqlParameter("@second_hostname", _
  94. SqlDbType.VarChar,50))
  95. myCommand.Parameters("@second_hostname").Value = second_hostname.Value
  96.  
  97. myCommand.Connection.Open()   THIS IS WHERE THE EXCEPTION IS
  98. ' Test whether the new row can be added and  display the
  99. ' appropriate message box to the user.
  100. Try
  101. myCommand.ExecuteNonQuery()
  102. Message.InnerHtml = "<b>Record Added</b><br>" & insertCmd
  103. Catch ex As SqlException
  104. If ex.Number = 2627 Then
  105. Message.InnerHtml = "ERROR: A record already exists with " _
  106. & "the same Computer Serial Number"
  107. Else
  108. Message.InnerHtml = "ERROR: Could not add record, please " _
  109. & "ensure the fields are correctly filled out"
  110. Message.Style("color") = "red"
  111. End If
  112. End Try
  113.  
  114. myCommand.Connection.Close()
  115. BindGrid()
  116. End Sub
  117.  
  118. ' BindGrid connects to the database and implements a SQL
  119. ' SELECT query to get all the data in the table
  120. ' of the database.
  121. Sub BindGrid()
  122. Dim myConnection As SqlConnection
  123. Dim myCommand As SqlDataAdapter
  124. ' Create a connection to the "database" SQL Server
  125. myConnection = New SqlConnection("data source=SERVER;" _
  126. & "user id=user;password=password;initial catalog=Database")
  127. ' Connect to the SQL database using a SQL SELECT query to get all
  128. ' the data from the table.
  129. myCommand = New SqlDataAdapter("SELECT * FROM myTable", _
  130. myConnection)
  131. ' Create and fill a new DataSet.
  132. Dim ds As DataSet = New DataSet()
  133. myCommand.Fill(ds)
  134. ' Bind the DataGrid control to the DataSet.
  135. dgInserted.DataSource = ds
  136. dgInserted.DataBind()
  137. End Sub
  138. </script>
  139.  
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
0 1507

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
11
by: suzy | last post by:
i am trying to write aspx login system and the login process requires a validity check of username and password. i have been told that raising exception is costly, but i want a custom error...
11
by: Chris Fink | last post by:
I have setup an Oracle table which contains a blob field. How do I insert data into this field using C# and ADO.net?
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
by: John Lau | last post by:
Hi, I am looking at the MS KB Article 306355: HOW TO: Create Custom Error Reporting Pages in ASP.NET by Using Visual C# .NET This article describes how to redirect errors to a custom html...
1
by: ven | last post by:
hello i`m makin a asp.net service with database connection where i have an insert with decimal value, when i run my function i get this error : There are fewer columns in the INSERT statement...
1
by: Michael Persaud | last post by:
Hello, I am trying to insert into a SQL2000 dbase some records i am havinf some difficulty with the code below: Dim Reports If CboReportsTo.Text.ToString > "" Then
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
3
by: Blarneystone | last post by:
Ok, I've got a simple access 97 db. named S_tracking.mdb It has two tables 1- Jobs 2- Employees I've set up the references: Imports System.Data.OleDb Imports System.IO Imports System.data
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.