System.FormatException: Input string was not in a correct format.

liunx

Guest
I'm new to ASP.NET, so I'm working with the Sam's "Teach Yourself ASP.NET in 24 Hours" book examples. I've created an ASP.NET web form that edits a SQL Server 2k DB table named "AN_details" that contains 3 columns: ArticleID (autonumber Integer), Title (VarChar), and Article (VarChar). It worked fine last week, but now I'm getting this error message whenever I try to "Update" or "Delete" a record from the DB table through a DataGrid:

Server Error in '/' Application.
--------------------------------------------------------------------
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

Line 27: Sub dbAN_Update(sender As Object, e As DataGridCommandEventArgs)
Line 28: 'Determine the value of the ArticleID Column
Line 29: Dim ArticleID as Integer = e.Item.Cells(1).Text
Line 30:
Line 31: 'Reference each TextBox

I've Googled this error message, and I found several forum posts on it, but none of the solutions worked for me. I've made sure that the datatype in the DB table matches the datatype on the form (Integer) for the ID column, but it's still not working. Has anyone dealt with this error before? If so, do you see what I'm doing wrong? Thanks for any & all help.

ASP.NET CODE:
<%@ Page Language="VB" Debug="true" EnableSessionState="false" %>
<script runat="server">

Function AN() As System.Data.IDataReader
'NEED TO: Pull XML data into a DataReader
Dim connectionString As String = "server='SERVERNAME'; user id='UID'; password='PW'; database='DBNAME'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [ArticleID],[Title],[Article] FROM [AN_details]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Sub dbAN_Edit(sender As Object, e As DataGridCommandEventArgs)
dbAN.EditItemIndex = e.Item.ItemIndex

dbAN.DataSource = AN()
dbAN.DataBind()
End Sub

Sub dbAN_Update(sender As Object, e As DataGridCommandEventArgs)
'Determine the value of the ArticleID Column
Dim ArticleID as Integer = e.Item.Cells(1).Text

'Reference each TextBox
Dim txtTitle as TextBox = e.Item.Cells(2).Controls(0)
Dim txtArticle as TextBox = e.Item.Cells(3).Controls(0)

update_AN(ArticleID, txtTitle.Text, txtArticle.Text)

dbAN.EditItemIndex = -1

dbAN.DataSource = AN()
dbAN.DataBind()
End Sub

Sub dbAN_Cancel(sender As Object, e As DataGridCommandEventArgs)
dbAN.EditItemIndex = -1

dbAN.DataSource = AN()
dbAN.DataBind()
End Sub

Function update_AN(ByVal ArticleID As Integer, ByVal Title As String, ByVal Article As String) As Integer
Dim connectionString As String = "server='SERVERNAME'; user id='UID'; password='PW'; database='DBNAME'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "UPDATE [AN_details] SET [Article]=@Article, [Title]=@Title WHERE ([AN_details].[ArticleID] = @"& _
"ArticleID)"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_ArticleID As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_ArticleID.ParameterName = "@ArticleID"
dbParam_ArticleID.Value = ArticleID
dbParam_ArticleID.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_ArticleID)
Dim dbParam_Title As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_Title.ParameterName = "@Title"
dbParam_Title.Value = Title
dbParam_Title.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_Title)
Dim dbParam_Article As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_Article.ParameterName = "@Article"
dbParam_Article.Value = Article
dbParam_Article.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_Article)

Dim rowsAffected As Integer = 0
dbConnection.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try

Return rowsAffected
End Function

Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
dbAN.DataSource = AN()
dbAN.DataBind()
End If
End Sub

</script>
<html>
<head>
<link href=http://www.webdeveloper.com/forum/archive/index.php/"/scriptlibrary/stylesheet_dg.css" rel="stylesheet" />
</head>
<body>
<form runat="server">
<p>
</p>
<h1>Appraisal Newsletter Edit Page
</h1>
<p align="center">
<asp:DataGrid id="dbAN" runat="server" BorderWidth="1px" CellPadding="1" Width="100%" OnEditCommand="dbAN_Edit" OnUpdateCommand="dbAN_Update" OnCancelCommand="dbAN_Cancel" AutoGenerateColumns="False">
<SelectedItemStyle verticalalign="Top"></SelectedItemStyle>
<EditItemStyle verticalalign="Top"></EditItemStyle>
<AlternatingItemStyle verticalalign="Top" backcolor="#FFFFCC"></AlternatingItemStyle>
<ItemStyle verticalalign="Top"></ItemStyle>
<HeaderStyle font-bold="True" forecolor="#FFFFFF" backcolor="#330066"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="ArticleID" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Title" HeaderText="Titles"></asp:BoundColumn>
<asp:BoundColumn DataField="Article" HeaderText="Articles"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Delete" CancelText="Cancel" EditText="Delete"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
</p>
<p>
<strong>Date: </strong>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<strong>Volume: </strong>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<strong>Number: </strong>
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
</p>
<p align="center">
<asp:Button id="btn_Add" runat="server" Text="Add New Item"></asp:Button>
<asp:Button id="btn_Clear" runat="server" Text="Clear All Data"></asp:Button>
<asp:Button id="btn_Preview" runat="server" Text="Preview"></asp:Button>
<asp:Button id="btn_Upload" runat="server" Text="Upload to Site"></asp:Button>
</p>
</form>
</body>
</html>I've found the solution! It was as simple as changing those lines of code from:
Sub dbAN_Update(sender As Object, e As DataGridCommandEventArgs)
'Determine the value of the ArticleID Column
Dim ArticleID as Integer = e.Item.Cells(1).Text

'Reference each TextBox
Dim txtTitle as TextBox = e.Item.Cells(2).Controls(0)
Dim txtArticle as TextBox = e.Item.Cells(3).Controls(0)

to:
Sub dbAN_Update(sender As Object, e As DataGridCommandEventArgs)
'Determine the value of the ArticleID Column
Dim ArticleID as Integer = e.Item.Cells(0).Text

'Reference each TextBox
Dim txtTitle as TextBox = e.Item.Cells(1).Controls(0)
Dim txtArticle as TextBox = e.Item.Cells(2).Controls(2)
It's always something small & stupid. Anyway, see yah.
ff
 
Back
Top