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

Can't ADD Row to DataGrid

Status
Not open for further replies.

net123

Programmer
Oct 18, 2002
167
US
I am trying to add a new row to a DataGrid Control via an 'Add' link. When I click on 'Add', I get an error. It seems to have a problem with my primary key (X_ID). On my SQL Server 2000 table, X_ID is of type int(4). According to the error messages below, they are not compatible with the code that I have below. Quite odd indeed for I can't seem to find the root of the problem.

Below one will find the ERROR and part of the CODE. I have also put 3 * next to line 197. Thank you for your assistance.

=====================================

======== ERROR CONTENTS ========
System.FormatException: Input string was not in a correct format. at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.String.System.IConvertible.ToInt32(IFormatProvider provider) at System.Convert.ToInt32(Object value) at System.Data.Common.Int32Storage.Set(Int32 record, Object value) at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <> in X_ID Column. Expected type is Int32.

Source Error:

Line 195: ' add a new blank row to the end of the data
Line 196: Dim rowValues As Object() ={&quot;&quot;, &quot;&quot;, &quot;&quot;}
Line 197: ds.Tables(0).Rows.Add(rowValues)
Line 198:
Line 199: ' figure out the EditItemIndex, last record on last page

Source File: c:\inetpub\ Line: 197

Stack Trace:

[ArgumentException: System.FormatException: Input string was not in a correct format.
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ToInt32(Object value)
at System.Data.Common.Int32Storage.Set(Int32 record, Object value)
at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <> in X_ID Column. Expected type is Int32.]
System.Data.DataColumn.set_Item(Int32 record, Object value) +100
System.Data.DataTable.NewRecordFromArray(Object[] value) +178
System.Data.DataRowCollection.Add(Object[] values) +14
ASP.EditX_aspx.AddNew_Click(Object Sender, EventArgs E) in c:\inetpub\ System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +108
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1277

======== END OF ERROR CONTENTS ========
[/red]

======== PART OF CODE =========

Sub DataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)

' update the database with the new values

' get the edit text boxes
Dim id As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Dim acct As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim name As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text

' TODO: update the Command value for your application
Dim myConnection As New SqlConnection(ConnectionString)
Dim UpdateCommand As SqlCommand = new SqlCommand()
UpdateCommand.Connection = myConnection

If AddingNew = True Then
UpdateCommand.CommandText = &quot;INSERT INTO tblX(Account, Name) VALUES (@Account, @Name)&quot;
Else
UpdateCommand.CommandText = &quot;UPDATE tblX SET Account = @Account, Name = @Name WHERE X_ID = @X_ID&quot;
End If

UpdateCommand.Parameters.Add(&quot;@Account&quot;, SqlDbType.VarChar, 50).Value = acct
UpdateCommand.Parameters.Add(&quot;@Name&quot;, SqlDbType.VarChar, 50).Value = name
UpdateCommand.Parameters.Add(&quot;@X_ID&quot;, SqlDbType.Int, 4).Value = id

' execute the command
Try
myConnection.Open()
UpdateCommand.ExecuteNonQuery()

Catch ex as Exception
Message.Text = ex.ToString()

Finally
myConnection.Close()

End Try

' Resort the grid for new records
If AddingNew = True Then
DataGrid1.CurrentPageIndex = 0
AddingNew = false
End If

' rebind the grid
DataGrid1.EditItemIndex = -1
BindGrid()

End Sub

======== MORE CODE =========

Sub AddNew_Click(Sender As Object, E As EventArgs)

' add a new row to the end of the data, and set editing mode 'on'

CheckIsEditing(&quot;&quot;)

If Not isEditing = True Then

' set the flag so we know to do an insert at Update time
AddingNew = True

' add new row to the end of the dataset after binding

' first get the data
Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlDataAdapter(SelectCommand, myConnection)

Dim ds As New DataSet()
myCommand.Fill(ds)

' add a new blank row to the end of the data
Dim rowValues As Object() = {&quot;&quot;, &quot;&quot;, &quot;&quot;}
*** Line 197: ds.Tables(0).Rows.Add(rowValues)

' figure out the EditItemIndex, last record on last page
Dim recordCount As Integer = ds.Tables(0).Rows.Count

If recordCount > 1 Then

recordCount -= 1
DataGrid1.CurrentPageIndex = recordCount \ DataGrid1.PageSize
DataGrid1.EditItemIndex = recordCount Mod DataGrid1.PageSize

End If

' databind
DataGrid1.DataSource = ds
DataGrid1.DataBind()

End If

End Sub

======= END OF CODE =============
[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top