Error IndexOutOfRangeException was unhandled - vba

Public Class Form1
Dim provider As String
Dim datafile As String
Dim connstring As String
Public myconnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
provider = "Provider=Microsoft.Ace.OLEDB.12.0"
datafile = "Data Source=C:\Users\fess\Desktop\test\compress.accdb"
connstring = provider & ";" & datafile
myconnection.ConnectionString = connstring
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
myconnection.Open()
Dim str As String
str = "SELECT 'name' FROM test2 WHERE 'ID'='1'"
Dim cmd As OleDbCommand = New OleDbCommand(str, myconnection)
dr = cmd.ExecuteReader
TextBox1.Text = dr(str).ToString
myconnection.Close()
End Sub
End Class

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
myconnection.Open()
Dim str As String
str = "SELECT ID from test2 where name='bry'"
Dim cmd As OleDbCommand = New OleDbCommand(str, myconnection)
dr = cmd.ExecuteReader
While dr.Read()
TextBox1.Text = dr("ID").ToString
End While
myconnection.Close()
Found my mistake after a bit of searching

Related

How can i Bind data to new bindings

error message "Child list for field dbo cannot be created" in last line code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ds1.Reset()
con1 = New SqlConnection(Strcon)
con1.Open()
strsql1 = "SELECT * FROM dbo.rasmi where Personel_ID=123456"
da1 = New SqlDataAdapter(strsql1, con1)
da1.Fill(ds1, "dbo.rasmi")
con1.Close()
DataGridView1.DataBindings.Clear()
DataGridView1.DataBindings.Add(New Binding("datasource", ds1, "dbo.rasmi"))
End Sub
Imports System.Threading
Imports System.Data
Imports System.Data.SqlClient
Public Class Form2
Dim Strcon As String = "Data Source=.;Initial Catalog=Pooyesh_Reprt;Persist Security
Info=True;User ID=sa;Password=789456321"
Dim con1 As SqlConnection
Dim strsql1 As String = ""
Dim da1 As SqlDataAdapter
Dim ds1 As New DataSet
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim TypeOfLanguage = New System.Globalization.CultureInfo("fa") ' or "fa-IR" for b
Farsi(Iran)
InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(TypeOfLanguage)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ds1.Reset()
con1 = New SqlConnection(Strcon)
con1.Open()
strsql1 = "SELECT * FROM dbo.rasmi where Personel_ID=123456"
da1 = New SqlDataAdapter(strsql1, con1)
da1.Fill(ds1, "dbo.rasmi")
con1.Close()
DataGridView1.DataBindings.Clear()
DataGridView1.DataBindings.Add(New Binding("datasource", ds1, "dbo.rasmi"))
End Sub
End Class

Search Data in Multiple Table and Found Result in Multiple GridView .VB

Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class search
Dim conn As New SqlConnection
Dim da As New SqlDataAdapter
Dim strcon As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Application\Abhi\Software\Software\Vansh.mdf;Integrated Security=True;User Instance=True"
Private Sub search_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Application\Abhi\Software\Software\Vansh.mdf;Integrated Security=True;User Instance=True"
conn = New SqlConnection(strcon)
conn.Open()
loaddata()
' txtsearch.Enabled = False
' txtsearch.Text = "Student Id is auto number"
End Sub
Public Sub loaddata()
conn = New SqlConnection(strcon)
conn.Open()
Dim str As String = "Select * From Payment "
da = New SqlDataAdapter(str, conn)
Dim ds As New DataSet
da.Fill(ds, "Payment")
DataGridView1.DataSource = ds.Tables("Payment")
da.Dispose()
conn.Close()
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
For Each txt In {txtmo, txtsearch}
txt.Clear()
Next
End Sub
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub btnhome_Click(sender As System.Object, e As System.EventArgs) Handles btnhome.Click
Me.Hide()
Dim rd As New Home_Page
rd.Show()
End Sub
Private Sub txtsearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtsearch.TextChanged
Try
conn = New SqlConnection(strcon)
conn.Open()
If txtsearch.Text = " " Then
loaddata()
Else
conn = New SqlConnection(strcon)
conn.Open()
Dim Str As String = "Select Date, Name, Amount from Payment where Name like '" + txtsearch.Text + "%' "
da = New SqlDataAdapter(Str, conn)
Dim ds As New DataSet
da.Fill(ds, "Payment")
DataGridView1.DataSource = ds.Tables("Payment")
da.Dispose()
conn.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub txtmo_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtmo.TextChanged
Try
conn = New SqlConnection(strcon)
conn.Open()
If txtsearch.Text = " " Then
loaddata()
Else
conn = New SqlConnection(strcon)
conn.Open()
Dim Str As String = "Select Date, Name, Amount from Payment where Date like '" + txtmo.Text + "%' "
da = New SqlDataAdapter(Str, conn)
Dim ds As New DataSet
da.Fill(ds, "Payment")
DataGridView1.DataSource = ds.Tables("Payment")
da.Dispose()
conn.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
Try
conn = New SqlConnection(strcon)
conn.Open()
If txtsearch.Text = " " Then
loaddata()
Else
conn = New SqlConnection(strcon)
conn.Open()
Dim Str As String = "Select Date, Name, Amount from Payment where Date like '" + DateTimePicker1.Text + "%' "
da = New SqlDataAdapter(Str, conn)
Dim ds As New DataSet
da.Fill(ds, "Payment")
DataGridView1.DataSource = ds.Tables("Payment")
da.Dispose()
conn.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataGridView2_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
End Sub
End Class

how to search and update data into ms access

my problem is how to search data and update it in ms access and vb.net
my code:
Imports System.Data.OleDb
Public Class Form5
Dim provider As String
Dim dataFile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
Private Sub Form5_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "C:\Users\Admin\Desktop\project database\Database5.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
myConnection.Open()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
TextBox8.Clear()
TextBox9.Clear()
TextBox10.Clear()
TextBox11.Clear()
Dim str As String
str = "SELECT * FROM registration WHERE (Name = '" & TextBox1.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
TextBox2.Text = dr("No").ToString
TextBox3.Text = dr("Name").ToString
TextBox4.Text = dr("Age").ToString
TextBox5.Text = dr("Gender").ToString
TextBox6.Text = dr("Phone").ToString
TextBox7.Text = dr("Address").ToString
TextBox8.Text = dr("Desease").ToString
TextBox9.Text = dr("RoomNo").ToString
TextBox10.Text = dr("Building").ToString
TextBox11.Text = dr("RoomType").ToString
End While
myConnection.Close()
End Sub
End Class
the error I'm getting:
"No value given for one or more required parameters."}
Isn't the issue here that when you click button3 you clear the textbox1 where I assume you type the name you wish to search. So its throwing you an error that there is nothing in that field. And even if you type something after wards, and click the button again, it will clear it again before it starts searching. so basically, you're searching for nothing.

The ConnectionString property has not been initialized. Error?

Imports System.Data.OleDb
Public Class Money_Donated
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
All_Donations.Show()
Me.Close()
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "C:\Users\Derick\Desktop\ICE 9\Donors.accdb"
End Sub
Dim provider As String
Dim dataFile As String
Dim connString As String
Public dr As OleDbDataReader
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim myConnection As OleDbConnection = New OleDbConnection()
myConnection.ConnectionString = connString
connString = provider & dataFile
TextBox1.Clear()
myConnection.Open()
Dim str As String
str = "SELECT * FROM Donors WHERE (Code = '" & Donators121.Amount_of_money_donatedTextBox.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
myConnection.Close()
While dr.Read()
TextBox1.Text = dr("Description").ToString
End While
End Sub
End Class
It looks like you are setting the connection string with an empty string. You need to swap the line connString = provider & dataFile with the line myConnection.ConnectionString = connString. Otherwise, you are setting the connection string of the connection object to an empty string.
Dim myConnection As OleDbConnection = New OleDbConnection()
connString = provider & dataFile
myConnection.ConnectionString = connString
TextBox1.Clear()
Also, as OneFineDay suggests you should use parameters rather than creating SQL strings as your code is currently vulnerable to SQL injection attacks.

The ConnectionString property has not been initialized help databases

I have been trying to work this out for the last 4 weeks and cannot find the answer I've just been going around in circles. I have had multiple errors with con As New OleDb.OleDbConnection and connection string and ISAM's
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim da As OleDb.OleDbDataAdapter
Public sql As String
Dim ds As New DataSet
Dim dbPassword As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "C:\Users\Edward\Desktop\Edward\DataBase Login Window\Users.mdb;Jet OLEDB:Database"
con.ConnectionString = dbProvider & dbSource
con.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sql = "SELECT * FROM users WHERE accUsername='" & TextBox1.Text & "'AND accPass='" & TextBox2.Text & "'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "users")
If ds.Tables("users").Rows.Count = 1 Then
Dim isAdmin As Boolean = ds.Tables("users").Rows(0).Item(4)
If isAdmin = True Then
MsgBox("Logged in.")
Form4.Show()
Form4.Username = TextBox1.Text
Form4.Password = TextBox2.Text
ElseIf isAdmin = False Then
MsgBox("Logged in user.")
Form6.Show()
Form6.Username = TextBox1.Text
Form6.Password = TextBox2.Text
End If
Else
MsgBox("Incorrect Username or Password")
End If
Me.Hide()
ds.Clear()
Me.Refresh()
End Sub
Private Sub createanaccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createanaccount.Click
Form2.ShowDialog()
End Sub
End Class
in the button you should set the connectionString here is an example.
dim con as new SQLConnection/OLEDBConnection
con.ConnectionString="Provide your connection string"
con.open
I hope this will definitely solve your problem