Link to home
Start Free TrialLog in
Avatar of pascalmartin
pascalmartinFlag for Hong Kong

asked on

Casting with CType

I want the following paramter to return an Integer, but it returns a NVARCHAR instead.
I am using the DataKey field with another parameter and not with the ProdID which is not my identity field in the DB.
Any Solution? Thanks in advance.

myCom.Parameters.AddWithValue("@ProdID", SqlDbType.Int).Value = CType(PricingGrid.Columns(1), BoundField).DataField
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

My understanding is that AddWithValue always returns a NVARCHAR. It's an implicit conversion. Have you tried Parameters.Add(...) instead? And then say Paramenters("@ProgID").Value = CType(PricingGrid.Columns(1), BoundField).DataField on the next line.
AddWithValue takes the parameter name and a value as arguments.  You cannot specify the expected type.  You will have to create a parameter, and add with Parameters.Add as suggested.
Avatar of pascalmartin

ASKER

Hi, Here is what I did:
Dim ProdIdentity As SqlParameter = New SqlParameter("@ProdID", SqlDbType.Int)
ProdIdentity.Value = CType(PricingGrid.Columns(1), BoundField).DataField
myCom.Parameters.Add(ProdIdentity)

But the method returns the following error message:
Error accessing database.
Failed to convert parameter value from a String to a Int32.

If you are getting that exception, it is because the parameter value is blank, and you can convert a blank string to an integer.  One workaround, with Visual Basic, is to convert with the Val function, since it won't raise any exceptions.
How to do that?
You add the parameter first before assigning its value

           myCom.Parameters.Add(New SqlParameter("@ProdID", SqlDbType.Int, 4, "prodId"))
            myCom.Parameters("@ProdID").Value = CType(PricingGrid.Columns(1), BoundField).DataField
What value does CType(PricingGrid.Columns(1), BoundField).DataField return?  DataField would be the string that is the name of the column for a BoundField, which wouldn't convert to an integer.
What I need to pass is the column value and not the name of the column, therefore
"myCom.Parameters("@ProdID").Value = CType(PricingGrid.Columns(1), BoundField).DataField" is probably the wrong statement because it is still returning string instead of an Int32.

I just want to capture the ProdID values of the bounddatafield named "ProdID"
>>"wrong statement because it is still returning string instead of an Int32"
You want the value, not the column title, correct? So is the value of the column already an integer or is it a string?
If a string, then how about:
myCom.Parameters("@ProdID").Value = CType(PricingGrid.DataGridItem.Cells(1).Text, Integer)
Hi, the following statement:
myCom.Parameters("@ProdID").Value = CType(PricingGrid.DataGridItem.Cells(1).Text, Integer)
does not work a "DataGridItem" is not a member of "System.Web.UI.WebControls.GridView"

I also tried the following:
myCom.Parameters("@ProdID").Value = PricingGrid.Rows(e.RowIndex).Cells(1).Text
But it won't work either, the DB exception returns:
"Failed to convert parameter value from a String to a Int32."

Try this (assuming that you are not binding the GridView to an SqlDataSource):

Dim drv As DataRowView = CType(PricingGrid.Rows(e.RowIndex).DataItem, DataRowView)
myCom.Parameters("@ProdID").Value = Val(drv("ProdID"))
Line: 4723
Error: Sys.WebForms.PageRequestManagerServerErrorException: An SqlParameter with ParameterName '@ProdID' is not contained by this SqlParameterCollection.

To make sure that there is no error in my sub as in my SP, I have strongly typed the "ProdID" value like this: myCom.Parameters("@ProdID").Value = 25
and the code works like a charm, the only challenge is to pass this INT value from the boundfield to the DB.
my boudfied is set to visible=false does it matter?
Ok, the following works :
myCom.Parameters("@ProdID").Value = PricingGrid.Rows(e.RowIndex).Cells(1).Text
only if my boundfield is visible...that a progress, but i want it invisible??
Did you get a DataRowView from GridViewRow.DataItem?
Actually I am binding the gridview from a sqldatasource, I could not get the DataRowView
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not satisfied with the answer