报错为:行 185:TB(1) = e.Item.Cells(1).Controls(0)--指定的转换无效
sub updatedata(sender as object,E as datagridcommandeventargs)
dim conn as oledbconnection
dim cmd as oledbcommand
dim sql as string
dim provider = "provider=microsoft.jet.oledb.4.0"
dim database = "data source=" & server.mappath("gsmjzsj.mdb")
conn = new oledbconnection(provider & ";" & database)
conn.open()
sql = "update 网优专业培训记录 set id=@id,培训时间=@培训时间,培训课程=@培训课程,文件名称=@文件名称,主讲人=@主讲人, 参加人员=@参加人员 where id=@key"
cmd = new oledbcommand(sql,conn)
cmd.parameters.add(new oledbparameter("@id",oledbtype.integer))
cmd.parameters.add(new oledbparameter("@培训时间",oledbtype.dbdate))
cmd.parameters.add(new oledbparameter("@培训课程",oledbtype.char))
cmd.parameters.add(new oledbparameter("@文件名称",oledbtype.char))
cmd.parameters.add(new oledbparameter("@主讲人",oledbtype.char))
cmd.parameters.add(new oledbparameter("@参加人员",oledbtype.char))
cmd.parameters.add(new oledbparameter("@key",oledbtype.integer))
dim TB(6) as TextBox
TB(1) = e.Item.Cells(1).Controls(0)
TB(2) = e.Item.Cells(2).Controls(0)
TB(3) = e.Item.Cells(3).Controls(0)
TB(4) = e.Item.Cells(4).Controls(0)
TB(5) = e.Item.Cells(5).Controls(0)
TB(6) = e.Item.Cells(6).Controls(0) cmd.parameters("@key").value=mydatagrid.datakeys(e.item.itemindex)
cmd.parameters("@id").value = TB(1).Text
cmd.parameters("@培训时间").value = TB(2).Text
cmd.parameters("@培训课程").value = TB(3).Text
cmd.parameters("@文件名称").value = TB(4).Text
cmd.parameters("@主讲人").value = TB(5).Text
cmd.parameters("@参加人员").value = TB(6).Text
cmd.executenonquery()
conn.close()
MyDataGrid.edititemindex =-1
opendatabase_and_bindtodatagrid()
end sub
sub updatedata(sender as object,E as datagridcommandeventargs)
dim conn as oledbconnection
dim cmd as oledbcommand
dim sql as string
dim provider = "provider=microsoft.jet.oledb.4.0"
dim database = "data source=" & server.mappath("gsmjzsj.mdb")
conn = new oledbconnection(provider & ";" & database)
conn.open()
sql = "update 网优专业培训记录 set id=@id,培训时间=@培训时间,培训课程=@培训课程,文件名称=@文件名称,主讲人=@主讲人, 参加人员=@参加人员 where id=@key"
cmd = new oledbcommand(sql,conn)
cmd.parameters.add(new oledbparameter("@id",oledbtype.integer))
cmd.parameters.add(new oledbparameter("@培训时间",oledbtype.dbdate))
cmd.parameters.add(new oledbparameter("@培训课程",oledbtype.char))
cmd.parameters.add(new oledbparameter("@文件名称",oledbtype.char))
cmd.parameters.add(new oledbparameter("@主讲人",oledbtype.char))
cmd.parameters.add(new oledbparameter("@参加人员",oledbtype.char))
cmd.parameters.add(new oledbparameter("@key",oledbtype.integer))
dim TB(6) as TextBox
TB(1) = e.Item.Cells(1).Controls(0)
TB(2) = e.Item.Cells(2).Controls(0)
TB(3) = e.Item.Cells(3).Controls(0)
TB(4) = e.Item.Cells(4).Controls(0)
TB(5) = e.Item.Cells(5).Controls(0)
TB(6) = e.Item.Cells(6).Controls(0) cmd.parameters("@key").value=mydatagrid.datakeys(e.item.itemindex)
cmd.parameters("@id").value = TB(1).Text
cmd.parameters("@培训时间").value = TB(2).Text
cmd.parameters("@培训课程").value = TB(3).Text
cmd.parameters("@文件名称").value = TB(4).Text
cmd.parameters("@主讲人").value = TB(5).Text
cmd.parameters("@参加人员").value = TB(6).Text
cmd.executenonquery()
conn.close()
MyDataGrid.edititemindex =-1
opendatabase_and_bindtodatagrid()
end sub
TB(2) = e.Item.Cells(2).Controls(0)
TB(3) = e.Item.Cells(3).Controls(0)
TB(4) = e.Item.Cells(4).Controls(0)
TB(5) = e.Item.Cells(5).Controls(0)
TB(6) = e.Item.Cells(6).Controls(0)
改成
TB(1) = (TextBox)e.Item.Cells(1).Controls(0)
TB(2) = (TextBox)e.Item.Cells(2).Controls(0)
TB(3) = (TextBox)e.Item.Cells(3).Controls(0)
TB(4) = (TextBox)e.Item.Cells(4).Controls(0)
TB(5) = (TextBox)e.Item.Cells(5).Controls(0)
TB(6) = (TextBox)e.Item.Cells(6).Controls(0)
他说的就对了。
dim TB(6) as TextBox
TB(1) = e.Item.Cells(1).Controls(0)
TB(2) = e.Item.Cells(2).Controls(0)
TB(3) = e.Item.Cells(3).Controls(0)
TB(4) = e.Item.Cells(4).Controls(0)
TB(5) = e.Item.Cells(5).Controls(0)
TB(6) = e.Item.Cells(6).Controls(0)
改为string TB(1) =(TextBox)e.Item.Cells(1).Controls(0)
string TB(2) = (TextBox)e.Item.Cells(2).Controls(0)
string TB(3) = (TextBox)e.Item.Cells(3).Controls(0)
string TB(4) = (TextBox)e.Item.Cells(4).Controls(0)
string TB(5) = (TextBox)e.Item.Cells(5).Controls(0)
string TB(6) = (TextBox)e.Item.Cells(6).Controls(0)把
cmd.parameters("@id").value = TB(1).Text
为cmd.parameters("@id").value = TB(1)其他的也这样改
应该就可以了
改成
TB(1) = (TextBox)e.Item.Cells(1).Controls(0)
---------------------------------------
上述写法是C#里面的写法,在VB里面要如下:
TB(1) = ctype(e.Item.Cells(1).Controls(0),TextBox)
这个也不行呀,运行起来还是说:指定的转换无效。
改为
dim TB(6) as New TextBox试试~~
<script language="VB" runat="server"> Dim MyConnection As SqlConnection Sub Page_Load(Sender As Object, E As EventArgs) MyConnection = New _
SqlConnection("server=(local)\NetSDK;database=pubs;Integrated Security=SSPI") If Not (IsPostBack)
BindGrid()
End If
End Sub Sub MyDataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs) MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
BindGrid()
End Sub Sub MyDataGrid_Cancel(Sender As Object, E As DataGridCommandEventArgs) MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub Sub MyDataGrid_Update(Sender As Object, E As DataGridCommandEventArgs) Dim DS As DataSet
Dim MyCommand As SqlCommand Dim UpdateCmd As String = "UPDATE Authors SET au_id = @Id, au_lname = @LName, " & _
"au_fname = @FName, phone = @Phone, address = @Address, city = @City, " & _
"state = @State, zip = @Zip, contract = @Contract where au_id = @Id" MyCommand = New SqlCommand(UpdateCmd, MyConnection) MyCommand.Parameters.Add(New SqlParameter("@Id", SqlDbType.NVarChar, 11))
MyCommand.Parameters.Add(New SqlParameter("@LName", SqlDbType.NVarChar, 40))
MyCommand.Parameters.Add(New SqlParameter("@FName", SqlDbType.NVarChar, 20))
MyCommand.Parameters.Add(New SqlParameter("@Phone", SqlDbType.NChar, 12))
MyCommand.Parameters.Add(New SqlParameter("@Address", SqlDbType.NVarChar, 40))
MyCommand.Parameters.Add(New SqlParameter("@City", SqlDbType.NVarChar, 20))
MyCommand.Parameters.Add(New SqlParameter("@State", SqlDbType.NChar, 2))
MyCommand.Parameters.Add(New SqlParameter("@Zip", SqlDbType.NChar, 5))
MyCommand.Parameters.Add(New SqlParameter("@Contract", SqlDbType.NVarChar,1)) MyCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex)) Dim Cols As String() = {"@Id","@LName","@FName","@Phone","@Address", _
"@City","@State","@Zip","@Contract"} Dim NumCols As Integer = E.Item.Cells.Count
Message.InnerHtml = "" Dim I As Integer
For I=2 To NumCols-2 'skip first, second and last column Dim CurrentTextBox As System.Web.UI.WebControls.TextBox
CurrentTextBox = E.Item.Cells(I).Controls(0)
Dim ColValue As String = CurrentTextBox.Text
' check for invalid values
Select Case Cols(i-1)
Case "@LName"
If Not InputValidator.IsValidAnsiName(colvalue)
Message.InnerHtml &= "ERROR: Last Name - " & InputValidator.AnsiNameErrorString & "<br>"
End If
Case "@FName"
If Not InputValidator.IsValidAnsiName(colvalue)
Message.InnerHtml &= "ERROR: First Name - " & InputValidator.AnsiNameErrorString & "<br>"
End If
Case "@Phone"
If Not InputValidator.IsValidAnsiPhoneNumber(colvalue)
Message.InnerHtml &= "ERROR: Phone - " & InputValidator.AnsiPhoneErrorString & "<br>"
End If
Case "@Address"
If Not InputValidator.IsValidAnsiAddress(colvalue)
Message.InnerHtml &= "ERROR: Address - " & InputValidator.AnsiAddressErrorString & "<br>"
End If
Case "@City"
If Not InputValidator.IsValidAnsiCityOrState(colvalue)
Message.InnerHtml &= "ERROR: City - " & InputValidator.AnsiCityStateErrorString & "<br>"
End If
Case "@State"
If Not InputValidator.IsValidAnsiTwoCharacterState(colvalue)
Message.InnerHtml &= "ERROR: State - " & InputValidator.AnsiTwoCharacterStateErrorString & "<br>"
End If
Case "@Zip"
If Not InputValidator.IsValidFiveDigitZipCode(colvalue)
Message.InnerHtml &= "ERROR: Zip Code - " & InputValidator.AnsiBasicZipCodeErrorString & "<br>"
End If
End Select ' Check for null values in required fields
If I<6 And ColValue = ""
Message.InnerHtml &= "ERROR: Null values not allowed for " & Cols(i-1) & "<br>"
End If MyCommand.Parameters(Cols(I-1)).Value = ColValue
Next If Message.InnerHtml <> ""
Message.Style("color") = "red"
Return
End If ' Append last row, converting true/false values to 0/1
Dim ContractTextBox As System.Web.UI.WebControls.TextBox
ContractTextBox = E.Item.Cells(NumCols-1).Controls(0)
If ContractTextBox.Text.ToLower(CultureInfo.InvariantCulture) = "true"
MyCommand.Parameters("@Contract").Value = "1"
Else
MyCommand.Parameters("@Contract").Value = "0"
End If MyCommand.Connection.Open() Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Updated</b><br>" & UpdateCmd.ToString()
MyDataGrid.EditItemIndex = -1
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "ERROR: A record already exists with the same " & _
"primary key"
Else
Message.InnerHtml = "ERROR: Could not update record, please " & _
"ensure the fields are correctly filled out"
End If
Message.Style("color") = "red"
End Try MyCommand.Connection.Close() BindGrid()
End Sub Sub BindGrid() Dim DS As DataSet
Dim MyCommand As SqlDataAdapter
MyCommand = new SqlDataAdapter("select * from Authors", MyConnection) DS = new DataSet()
MyCommand.Fill(DS, "Authors") MyDataGrid.DataSource=DS.Tables("Authors").DefaultView
MyDataGrid.DataBind()
End Sub</script>