Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PageIndexChanged gives error from 2nd page to 1st page 1

Status
Not open for further replies.

clanm

Programmer
Dec 26, 2005
237
US
With only one record on my 2nd page in my datagrid, when I go from page 2 to page 1 in my Datagrid (ASP.NET v1.1), I get:

Input string was not in a correct format.

[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value, NumberFormatInfo NumberFormat) +193
Microsoft.VisualBasic.CompilerServices.IntegerType.FromString(String Value) +92

[InvalidCastException: Cast from string "" to type 'Integer' is not valid.]
Microsoft.VisualBasic.CompilerServices.IntegerType.FromString(String Value) +206
Work_PackageVBConvert_P3E.SARESTableSubform.DataGrid1_DeleteCommand(Object source, DataGridCommandEventArgs e) +185
System.Web.UI.WebControls.DataGrid.OnDeleteCommand(DataGridCommandEventArgs e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +589
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292

****************************************************************

I have no idea what's going on or why this is happening. If I add another record to my datagrid, and now I have two rows on my second page, I can go to the first page successfully.

The PageIndexChanged routine is simply:
DataGrid1.CurrentPageIndex = e.NewPageIndex
Call DG()

Any ideas?

Thanks!
 
There are two statements that point to the error here:

1) Input string was not in a correct format.
2) Cast from string "" to type 'Integer' is not valid.

The first tells you that something was not in the correct format. The second tells you that a String value is trying to be converted to an Integer and is failing. So, basically, you have some data in a format that is incorrect. Fix it and your problems will go away!



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
So, why would adding another record to the 2nd page make it work correctly then?

Thanks for the reply!
 
It's hard to say as I don't know what data you are working with or how it is being displayed.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
If you find out the reason why you get the error, that will also answer your second question.
 
All,

thanks for the reply.

When I turned on debug, the error was in my deletecommand on the line
myCommand.Parameters(0).Value = CType(e.Item.Cells(15).Text, Integer)......even though that's the PK for the table:
Dim deleteCmd As String = "usp_DeleteFromSARes"
Dim sCon1 As New SqlConnection
sCon1.ConnectionString = Session("DBDDL")
Try
sCon1.Open()
Dim myCommand As SqlCommand = New SqlCommand(deleteCmd, sCon1)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@intUID", System.Data.SqlDbType.Int, 4))

myCommand.Parameters(0).Value = CType(e.Item.Cells(15).Text, Integer)
myCommand.ExecuteNonQuery()
sCon1.Close()

'This is to control if the items deleted were on the last page
If DataGrid1.Items.Count Mod DataGrid1.PageSize = 1 And _
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 And _
DataGrid1.CurrentPageIndex <> 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
End If
Call BuildMainDG()
Catch ex As Exception
lblStatus.Text = "Line 480 DG Delete Error: " & ex.Message
Finally
sCon1.Close()
End Try

The error here gives cast from type string "" to type integer not valid.
 
And what does "e.Item.Cells(15).Text" equal?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Sorry...this is the PK for the table, which is a bound column in the datagrid.

<asp:BoundColumn Visible="False" DataField="UniqueIdentifier" ReadOnly="True" HeaderText="UniqueIdentifier"></asp:BoundColumn>
 
No, I mean if you debug the code, what is the value of e.Item.Cells(15).Text when the error occurs? My guess (based on the error message) is that it is an empty String and therefore it can't convert it to an Integer.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm,

I tried putting the break where I set my SPROC value,
myCommand.Parameters(0).Value

.... but when I mouse over, I don't get any value. F5 just takes me to the exception error, and F8 won't do anything:

Try
sCon1.Open()
Dim myCommand As SqlCommand = New SqlCommand(deleteCmd, sCon1)

myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@intUID", System.Data.SqlDbType.Int, 4))
'break on line below
myCommand.Parameters(0).Value = CType(e.Item.Cells(15).Text.ToString, Integer)

myCommand.ExecuteNonQuery()
sCon1.Close()

'This is to control if the items deleted were on the last page
If DataGrid1.Items.Count Mod DataGrid1.PageSize = 1 And _
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 And _
DataGrid1.CurrentPageIndex <> 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
End If
Call BuildMainDG()
Catch ex As Exception
lblStatus.Text = " Line 480 DG Delete Error: " & ex.Message
Finally
sCon1.Close()
End Try

One quesiton I have is why is this even getting looked @ when I'm using the paging of the datagrid and not doing any deleting?

thanks for the help!
 
but when I mouse over, I don't get any value
That's what I was getting at - I think that e.Item.Cells(15).Text doesn't have a value so it can't possible convert it to an Integer.

For example, try this before you set your parameters value:
Code:
Dim myString as String = ""
myString = e.Item.Cells(15).Text
My guess is after the second line has executed, the string will still be empty.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
K....I put in a new string variable, strDGUID, and the break is on that line, and it's = Nothing:

Try
sCon1.Open()
Dim myCommand As SqlCommand = New SqlCommand(deleteCmd, sCon1)

myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@intUID", System.Data.SqlDbType.Int, 4))

Dim strDGUID As String = e.Item.Cells(15).Text.ToString

myCommand.Parameters(0).Value = CType(e.Item.Cells(15).Text.ToString, Integer)

myCommand.ExecuteNonQuery()
sCon1.Close()

...rest of try catch excluded

So, why isn't this thrown while going from page 1 to 2, but only from page 2 to 1? Hmmm...I got some diggin' to do.

The field for e.Item.Cells(15) is a PK, so it can't be NULL or blank since it's an autonumber...at least in the back end.
 
What's weird is I had the earlier version of the same datagrid and it works correctly (this earlier one when I click to go to page 1 from page 2). All we changed for this new datagrid was the EditItem template, so when the user clicked on the "Edit" link to edit a row, there were several dropdown lists and text boxes in one column, compared to the original grid where the layout stayed the same, we just converted some columns to a dropdownlist and others to a textbox. So basically, this new datagrid allows the user to see all the "editable" columns in one column, whereas before they were spread out. The delete command is exactly the same.
 
Well...I've come to a semi resolution. the user can page back and forth from page 2 to page 1 w/o any issues.

I had to add the following else code in this Finally control structure, along with building the datagrid again in the delete command catch block:

Finally
sCon1.Close()
If DataGrid1.Items.Count Mod DataGrid1.PageSize = 1 And _
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 And _
DataGrid1.CurrentPageIndex <> 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
Else
'added below line
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex
End If
'added below line
Call BuildMainDG()

This will do for now, as at least the datagrid still shows all records rather than nothing and making the user worried. It's an extra call to the server, but hopefully when we go to 2.0, we won't have this issue.
End Try
 
ca8msm,

The following is a fix I was able to do. It's the best solution I've come up with in order for the user not to lose data from the UI.

Thanks for all the help!

Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
If intDisable = 1 Then Exit Sub 'Use this to enable/disable Edit/Update/Delete for dgrids based on Rev#
'Un-Hide the "Locate" and "ChkCode" columns
DataGrid1.Columns(0).Visible = True
DataGrid1.Columns(3).Visible = True

Dim deleteCmd As String = "usp_DeleteFromSARes"
Dim sCon1 As New SqlConnection
sCon1.ConnectionString = Session("DBDDL")
Try
sCon1.Open()
Dim myCommand As SqlCommand = New SqlCommand(deleteCmd, sCon1)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Add(New System.Data.SqlClient.SqlParameter("@intUID", System.Data.SqlDbType.Int, 4))
Dim strDGUID As String = e.Item.Cells(15).Text.ToString
myCommand.Parameters(0).Value = CType(e.Item.Cells(15).Text.ToString, Integer)
myCommand.ExecuteNonQuery()
sCon1.Close()
DataGrid1.EditItemIndex = -1
Call BuildMainDG()
'DataGrid1.EditItemIndex = -1
'DataGrid1.DataBind()
Catch ex As InvalidCastException 'This exception is for paging from 2nd to 1st page w/ 1 record on page2
lblStatus.Text = "481 Null RefEx:"
Call BuildMainDG()
Catch ex As Exception
lblStatus.Text = "484 DG Error: " & ex.Message
Call BuildMainDG()
Finally
sCon1.Close()
'This is to control if the items deleted were on the last page
If DataGrid1.Items.Count Mod DataGrid1.PageSize = 1 And _
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1 And _
DataGrid1.CurrentPageIndex <> 0 Then
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1
Else
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex
End If
End Try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top