Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm adding a row to my database using a stored procedure and a SQLCommand object. I then pass back the id of my new row to the routine and use this id to pass to another form to open up the details form to edit the new row. But when I get to the frmProduct.Show line I get an exception that says "Object cannot be cast from DBNull to other types". Anyone any ideas what this could be?? My code looks like this...

       Dim cmd As New SqlCommand
       cmd.CommandText = "spAddProduct"
       cmd.CommandType = CommandType.StoredProcedure
       cmd.Parameters.Add("@ProductId", SqlDbType.Int)
       cmd.Parameters("@ProductId").Direction = ParameterDirection.Output
       cmd.Connection = conn
       If conn.State = ConnectionState.Closed Then conn.Open()
       cmd.ExecuteNonQuery()
       FillGrid()
       conn.Close()
       Dim frmProd As frmProduct = New frmProduct
       frmProd.ProductID = cmd.Parameters("@ProductId").Value
       frmProd.Show()

 

and the exception is thrown of the last line.

 

Thanks in advance

Posted
I've checked the parameter and it's definitely returning the new id value. i've also checked to make sure the record is commited to the database properly before trying to call up the record.
Posted

In your debugger, there should be an option in the debug menu that reads "exceptions". Pick this, and then for

Common Language Runtime Exceptions, use the radio button

"when unhandled ... jump to debugger".

 

Now test again.

 

Your debugger wll highlight the exact line (in green) where the error occured. Now with right mouse click you can inspect all variables and objects in that line.

.nerd

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...