Forum: ASP.NET |
Thema:
Fehler beim Editieren |
Von:
G. Guest (
19.04.2004 10:33) |
Hallo,
beim Editieren meiner Datensätze bekomme ich folgende Fehlermeldun:
"System.IndexOutOfRangeException: Index was outside the bounds of the array"
Dabei wird folgende Zeile rot angezeigt:
myCommand.Parameters(cols(i-1)).Value = colvalue
Mein Code:
...
Public Sub MyDataGrid_Update(sender As Object, _
E As DataGridCommandEventArgs)
Dim updateCmd As String = "UPDATE inventor SET id = @Id," _
& " firstname = @firstname, lastname = @lastname, citizenship = @citizenship," _
& " street = @street, town = @town, country = @country," _
& " lab = @lab, pgroup = @pgroup, email = @email," _
& " phone_home = @phone_home, phone_office = @phone_office," _
& " comment = @comment WHERE id = @Id;"
Dim myCommand As OleDbCommand = New OleDbCommand(updateCmd, _
myConnection)
myCommand.Parameters.Add(New OleDbParameter("@Id", OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@firstname", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@lastname", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@citizenship", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@street", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@town", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@country", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@lab", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@pgroup", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@email", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@phone_home", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@phone_office", _
OleDbType.VarChar))
myCommand.Parameters.Add(New OleDbParameter("@comment", _
OleDbType.VarChar))
' Initialize the OleDbCommand "@ID" parameter to the ID of the row
' that must be clicked.
myCommand.Parameters("@Id").Value = _
MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
' Create an array of column names.
Dim cols() As String = {"@Id","@firstname","@lastname","@citizenship","@town","@country","@lab", _
"@pgroup", "@email","@phone_home","@phone_office","@comment"}
' Skipping the first, second, and last columns, iterate through the
' columns, checking for empty values. If an empty value is found,
' display a message box. Also initialize the OleDbCommand
' parameter values.
Dim numCols As Integer = E.Item.Cells.Count
Dim i As Integer
Dim colvalue As String
Dim txtBox As Textbox
For i = 2 To numCols-1
txtBox = E.Item.Cells(i).Controls(0)
colvalue = txtBox.Text
If (i<6 And colvalue = "")
Message.InnerHtml = "ERROR: Null values not allowed for " _
& "Author ID, Name or Phone"
Message.Style("color") = "red"
Exit Sub
End If
myCommand.Parameters(cols(i-1)).Value = colvalue
Next i
' Append the last field, converting true/false values to 0/1.
txtBox=E.Item.Cells(numCols-1).Controls(0)
If (String.Compare(txtBox.Text, "true", true) = 0) Then
myCommand.Parameters("@comment").Value = "1"
Else
myCommand.Parameters("@comment").Value = "0"
' Connect to the database and update the information.
myCommand.Connection.Open()
' Test whether the data was updated, and display the
' appropriate message to the user.
Try
myCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Updated.</b><br>"
MyDataGrid.EditItemIndex = -1
Catch ex As OleDbException
Message.InnerHtml = "ERROR: A record already exists" _
& " with the same primary key"
Message.Style("color") = "red"
End Try
' Close the connection.
myCommand.Connection.Close()
' Rebind the DataGrid to show the updated information.
BindGrid()
End If
End Sub
...
Kann mir jemand sagen was ich hier falsch mache?
Betreff |
Von |
Datum |
|
|
Re: Fehler beim Editieren
Sieht so aus, als ob in der Anweisung <br><br>Dim cols() As String = {"@Id","@firstname","@lastname","@citizenship","@town","@country","@lab", _<br>"@pgroup",... |
|
|
|
|
|
Klaus
Holster
|
19.04.2004 11:18 |
|
|
Re: Fehler beim Editieren
Es geht es, danke für den Hinweis.<br>Ich habe aber noch ein Problem, id ist bei mir AutoIncrement-Wert, deswegen bekomme ich folgende Meldung<br>" System.Data.OleDb.OleDbException: 'id' kann nicht... |
|
|
|
|
|
G.
Guest
|
19.04.2004 20:51 |
|
|
Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!