Expand|Select|Wrap|Line Numbers
- <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>
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?