We help IT Professionals succeed at work.

Failed to convert parameter value from a String to a Int32.

I have the following code:

 Protected Sub CompleteGridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles CompleteGridView1.RowCommand
        Dim currentRowIndex As Integer = Int32.Parse(e.CommandArgument)

        Dim ProductID As String = CompleteGridView1.DataKeys(currentRowIndex).Value
        If Roles.IsUserInRole(Membership.GetUser.UserName.ToString, "ELP_Transport") Then
            Response.Redirect("~/Foxconn/CT/Metrics_Detail_Transport.aspx?Key='" & ProductID & "'")
        Else
            Response.Redirect("~/Foxconn/CT/Metrics_Detail.aspx?Key='" & ProductID & "'")
        End If

    End Sub

which calls the Metrics Detail page on page load:

I get the error on the second line:

Dim CT_Key As SqlParameter = cmd.Parameters.Add("@CT_Key", SqlDbType.Int)
CT_Key.Value = Request.QueryString("Key").ToString

rdr = cmd.ExecuteReader() ---> error: Input string was not in a correct format.


Thank you
Comment
Watch Question

Senior Engineer
Top Expert 2010
Commented:
You're redirecting to:
/Foxconn/CT/Metrics_Detail.aspx?Key='1234'
Then in Metric_Details the SqlCommand is trying to convert '1234' from a string to a number and is failing because of the quotes.
Try redirecting to:

/Foxconn/CT/Metrics_Detail.aspx?Key=1234

Protected Sub CompleteGridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles CompleteGridView1.RowCommand
        Dim currentRowIndex As Integer = Int32.Parse(e.CommandArgument)

        Dim ProductID As String = CompleteGridView1.DataKeys(currentRowIndex).Value
        If Roles.IsUserInRole(Membership.GetUser.UserName.ToString, "ELP_Transport") Then
            Response.Redirect("~/Foxconn/CT/Metrics_Detail_Transport.aspx?Key=" & ProductID & "")
        Else
            Response.Redirect(String.Format("~/Foxconn/CT/Metrics_Detail.aspx?Key={0}", ProductID))
        End If

End Sub

Open in new window

Author

Commented:
Thank you!
This might be a stupid question but why the different syntax between the response.redirects (see bold)

Response.Redirect("~/Foxconn/CT/Metrics_Detail_Transport.aspx?Key=" & ProductID & "")
Else
Response.Redirect(String.Format("~/Foxconn/CT/Metrics_Detail.aspx?Key={0}", ProductID))
Todd GerbertSenior Engineer
Top Expert 2010

Commented:
Sorry...meant to mention that I personally prefer to use String.Format instead of concatenating strings - that way my string isn't littered with "" & var & "" & asdf & etc, I find it easier to read.
String.Format("~/CarDetails.aspx?Cylinders={0}&Doors={1}&Color={2}&Displacement={3}&FrontWheelDrive={4}&Mileage={5}", _
  cylinders, _
  doors, _
  color, _
  displacement, _
  isFrontWheelDr, _
  miles)
Versus:
"~/CarDetails.aspx?Cylinders=" & cylinders & "&Doors=" & doors & "&Color=" & color & "&Displacement=" & displacement & "&FrontWheelDrive=" & isFrontWheelDr & "&Mileage=" & miles

Author

Commented:
Gotcha, thanks for the clarification.