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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input String was not in correct format

Status
Not open for further replies.

frmorris

Programmer
Aug 4, 2005
22
0
0
US
I am developing a web form and when I run it. I get an error that says 'Input String was not in correct format'. The error occurs in line one. Could someone help me in resolving this issue. Thanks.

Dim eventid As Integer = e.Item.Cells(1).Text 'Error here
Dim username As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim strevent As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim strdate As String = CType(e.Item.Cells(4).Controls(0), TextBox).Text

Dim update_tblevent As String = _
"Update [tblevent] SET [event] = '" & strevent & "', " & _
"[date] = '" & strdate & "', " & _
"where [eventid] = eventid"

Dim selectedeventid As String = masterDataGrid.DataKeys(e.Item.ItemIndex)
Dim myCommand As New SqlCommand("UPDATE FROM tblEvent WHERE eventID=" & selectedeventid, connEvent)
myCommand.CommandType = CommandType.Text

connEvent.Open()
myCommand.ExecuteNonQuery()
connEvent.Close()
BindGrid()
 
The text of the cell is a String, and you're trying to put it in an Integer variable. You need to cast it as an Integer, preferably checking to make sure it is numeric first.
 
Remeber there is an ASP.NET forum for ASP.NET related questions.

But

Code:
Dim eventid As Integer = val(ctype(e.Item.Cells(1),textbox).Text)

or

Code:
Dim eventid As Integer = convert.toint(ctype(e.Item.Cells(1),textbox).Text)

the second will want you to check if the text you feed it is numeric (hint).

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
What ever happened with this? I have almost the identical code and recieve this same error. The suggestions by chrissie1 would work on any other fields, but since this is the primary key to the database it is only shown as a Label instead of in a textbox like the other fields.

Code:
Dim iProductID As Integer = e.Item.Cells(1).Text 
Dim dblPrice as Double = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim strName as String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim strDesc as String = CType(e.Item.Cells(4).Controls(0), TextBox).Text

Since I am updating the database I need to somehow get the iProductID value for the where clause of the sql statement. When I view the source of the page I see this value is in a <span> tag and the others are inside <input. tags as i would expect. I guess I need to find out how to get the value of the cell where the <span> tag is. Any other suggestions?

Thanks
dok
 
Okay I have fixed my problem and maybe this will help someone in the future. I fixed my original problem by changing the datagrid column to an asp:boundcolumn. Then I recieved the same error on the next line of code. So I determined that it really wasn't a case of what type the string was.

To fix it I added ID's to my textbox's in each column. Then I changed the code as to use e.Item.FindControl("id of textbox").
Code:
Dim iProductID As Integer = e.Item.Cells(1).Text
Dim dblPrice as Double = CType(e.Item.FindControl("txtPrice"), TextBox).Text
Dim strName as String = CType(e.Item.FindControl("txtName"), TextBox).Text
Dim strDesc as String = CType(e.Item.FindControl("txtDesc"), TextBox).Text

Example of the datagrid code. Note I left the ProductID as a read only boundcolumn instead of using an itemtemplate. Changes are in [red]red[/red].
Code:
[red]<asp:BoundColumn HeaderText="Product ID" DataField="ProductID" ReadOnly="True" />[/red]
<asp:TemplateColumn HeaderText="Price">
   <ItemTemplate>
      <asp:Label
           runat="server"
           Text='<%# DataBinder.Eval(Container, "DataItem.UnitPrice") %>'>
      </asp:Label>
   </ItemTemplate>
   <EditItemTemplate>
      <asp:TextBox
           runat="server" 
           [red]ID="txtPrice"[/red] 
           Text='<%# DataBinder.Eval(Container, "DataItem.UnitPrice" %>'>
      </asp:TextBox>
   </EditItemTemplate>
</asp:TemplateColumn>

After making these changes I am able to now edit the rows and update the database without those error messages.

dok

 
You see, this is why there is an ASP.Net forum. In VB.Net we don't have asp:BoundColumn objects. We can not give you pointers on this stuff. So as Chrissie mentioned earlier, I would suggest heading to the ASP.Net forum.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top