I am using Web Matrix and created a simple 'Editable DataGrid' displaying only 3 fields:
X_ID, Account, Name
When I click on the Edit link, all 3 of these fields open up in a textbox to edit, but when I'm done editing them, when I click 'Update' to finish, I get the following error on the page:
System.Data.SqlClient.SqlException: Must declare the variable '@X_ID'. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at ASP.EditException_aspx.DataGrid_Update(Object Sender, DataGridCommandEventArgs E) in c:\inetpub\ 99
[/red]
And here is a snippet of my code (and where I believe the problem is located):
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(2).Controls(0), TextBox).Text
Dim acct As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim name As String = CType(e.Item.Cells(4).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 = "INSERT INTO tblX(Account, Name) VALUES (@Account, @Name)"
Else
UpdateCommand.CommandText = "UPDATE tblX SET Account = @Account, Name = @Name WHERE X_ID = @X_ID"
End If
UpdateCommand.Parameters.Add("@Account", SqlDbType.VarChar, 50).Value = acct
UpdateCommand.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = name
' execute the command
Try
myConnection.Open()
99. UpdateCommand.ExecuteNonQuery()[/red]
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
[/blue]
What I can't figure out is how to update the X_ID as well. How can I do this for id?
X_ID, Account, Name
When I click on the Edit link, all 3 of these fields open up in a textbox to edit, but when I'm done editing them, when I click 'Update' to finish, I get the following error on the page:
System.Data.SqlClient.SqlException: Must declare the variable '@X_ID'. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at ASP.EditException_aspx.DataGrid_Update(Object Sender, DataGridCommandEventArgs E) in c:\inetpub\ 99
[/red]
And here is a snippet of my code (and where I believe the problem is located):
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(2).Controls(0), TextBox).Text
Dim acct As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim name As String = CType(e.Item.Cells(4).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 = "INSERT INTO tblX(Account, Name) VALUES (@Account, @Name)"
Else
UpdateCommand.CommandText = "UPDATE tblX SET Account = @Account, Name = @Name WHERE X_ID = @X_ID"
End If
UpdateCommand.Parameters.Add("@Account", SqlDbType.VarChar, 50).Value = acct
UpdateCommand.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = name
' execute the command
Try
myConnection.Open()
99. UpdateCommand.ExecuteNonQuery()[/red]
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
[/blue]
What I can't figure out is how to update the X_ID as well. How can I do this for id?