pascalmartin
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.AddWithVa lue("@Prod ID", SqlDbType.Int).Value = CType(PricingGrid.Columns( 1), BoundField).DataField
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.AddWithVa
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").Val ue = 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.
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(ProdI dentity)
But the method returns the following error message:
Error accessing database.
Failed to convert parameter value from a String to a Int32.
Dim ProdIdentity As SqlParameter = New SqlParameter("@ProdID", SqlDbType.Int)
ProdIdentity.Value = CType(PricingGrid.Columns(
myCom.Parameters.Add(ProdI
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.
ASKER
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
myCom.Parameters.Add(New SqlParameter("@ProdID", SqlDbType.Int, 4, "prodId"))
myCom.Parameters("@ProdID"
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.
ASKER
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"
"myCom.Parameters("@ProdID
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.DataGrid Item.Cells (1).Text, Integer)
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"
ASKER
Hi, the following statement:
myCom.Parameters("@ProdID" ).Value = CType(PricingGrid.DataGrid Item.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.RowInde x).Cells(1 ).Text
But it won't work either, the DB exception returns:
"Failed to convert parameter value from a String to a Int32."
myCom.Parameters("@ProdID"
does not work a "DataGridItem" is not a member of "System.Web.UI.WebControls
I also tried the following:
myCom.Parameters("@ProdID"
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.R owIndex).D ataItem, DataRowView)
myCom.Parameters("@ProdID" ).Value = Val(drv("ProdID"))
Dim drv As DataRowView = CType(PricingGrid.Rows(e.R
myCom.Parameters("@ProdID"
ASKER
Line: 4723
Error: Sys.WebForms.PageRequestMa nagerServe rErrorExce ption: 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.
Error: Sys.WebForms.PageRequestMa
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"
and the code works like a charm, the only challenge is to pass this INT value from the boundfield to the DB.
ASKER
my boudfied is set to visible=false does it matter?
ASKER
Ok, the following works :
myCom.Parameters("@ProdID" ).Value = PricingGrid.Rows(e.RowInde x).Cells(1 ).Text
only if my boundfield is visible...that a progress, but i want it invisible??
myCom.Parameters("@ProdID"
only if my boundfield is visible...that a progress, but i want it invisible??
Did you get a DataRowView from GridViewRow.DataItem?
ASKER
Actually I am binding the gridview from a sqldatasource, I could not get the DataRowView
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Not satisfied with the answer