471,306 Members | 853 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 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 1422

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by suzy | last post: by
11 posts views Thread by Chris Fink | last post: by
40 posts views Thread by Kevin Yu | last post: by
6 posts views Thread by John Lau | last post: by
1 post views Thread by ven | last post: by
1 post views Thread by Michael Persaud | last post: by
20 posts views Thread by Mark Harrison | last post: by
reply views Thread by rosydwin | last post: by

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.