Salve,
ho un problema:
IndexOutofRangeExxeption non è stata gestita
Indice oltre i limiti della matrice.
Con vb 2012 e un db fatto in access chiamato emp
Devo effettuare la lettura per la chiave "anno" di n_record formati inizialmente da due campi (anno; popolazione).
Il codice scritto funziona finchè ho la necessità di un terzo campo (in realtà arriveranno ad 13 campi in totale.)
A questo punto modifico:
codice:
sqlStr = "insert into info values(" & txtEid.Text & ", " & txtEname.Text & ")"
a
codice:
sqlStr = "insert into info values(" & txtEid.Text & ", " & txtEname.Text & ", " & txtprova.Text & ")"
e modifico
codice:
dr = cmd.ExecuteReader()
If dr.HasRows = True Then
While dr.Read()
txtEid.Text = dr(0)
txtEname.Text = dr(1)
a
codice:
dr = cmd.ExecuteReader()
If dr.HasRows = True Then
While dr.Read()
txtEid.Text = dr(0)
txtEname.Text = dr(1)
txtprova.Text = dr(2)
ECCO IL CODICE:
codice:
Public Class Form1
Dim con As System.Data.OleDb.OleDbConnection
Dim cmd As System.Data.OleDb.OleDbCommand
Dim dr As System.Data.OleDb.OleDbDataReader
Dim sqlStr As String
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
con = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\0_2p\emp.accdb")
con.Open()
MsgBox("Connessione Riuscita")
sqlStr = "insert into info values(" & txtEid.Text & ", " & txtEname.Text & ", " & txtprova.Text & ")"
' aggiungo il mio terzo campo da leggere e da inserire il dato in txtprova.text
cmd = New OleDb.OleDbCommand(sqlStr, con)
cmd.ExecuteNonQuery()
MsgBox("Record inserito correttamente")
con.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim id As Integer
id = Val(InputBox("Inserire anno di riferimento"))
con = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\0_2p\emp.accdb")
con.Open()
MsgBox("Connessione Riuscita")
sqlStr = "Select * from info where anno =" & id & ""
' la mia tabella si chiama anno
cmd = New OleDb.OleDbCommand(sqlStr, con)
dr = cmd.ExecuteReader()
If dr.HasRows = True Then
While dr.Read()
txtEid.Text = dr(0)
txtEname.Text = dr(1)
txtprova.Text = dr(2)
' il mio errore
End While
End If
MsgBox("Record trovato")
con.Close()
End Sub
End Class
GRAZIE