davidh Posted February 18, 2004 Posted February 18, 2004 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 Quote
Administrators PlausiblyDamp Posted February 18, 2004 Administrators Posted February 18, 2004 It looks like the @ProductId parameter is being returned as a NULL, you may need to check it befrore refering to it or make sure the Stored Procedure sets it to a non-null value. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
davidh Posted February 19, 2004 Author Posted February 19, 2004 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. Quote
Heiko Posted February 19, 2004 Posted February 19, 2004 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. Quote .nerd
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.