Data Reader giving error!!!
I have a show button.The user will select a data from the dropdown list of the combo and on clicking the show button the corresponding data will be fetched from the database and will be shown in the corresponding fields.
So I did this code:
Code:
Private Sub Show_Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Show_Btn.Click
Using connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=BankAccount.mdb")
connection.Open()
Dim command As New OleDbCommand("Select * from BankAccount", connection)
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
ComboBox1.Items.Add(reader.GetString("BankName"))
ComboBox2.Items.Add(reader.GetString("AccountNo"))
ComboBox3.Items.Add(reader.GetString("BankName"))
ComboBox4.Items.Add(reader.GetString("AccountNo"))
End While
reader.Close()
End Using
End Sub
i get this error:
Code:
Conversion from string "BankName" to type 'Integer' is not valid.
Re: Data Reader giving error!!!
BankName is a String,
you can't save it as Integer
Re: Data Reader giving error!!!
Re: Data Reader giving error!!!
You should pay attention to what Intellisense tells you. As you were typing that code Intellisense would have told you that GetString accepts only an Integer argument. Failing that, read the documentation. If you try to call GetString and it doesn't work then go to the documentation and read about GetString and make sure you're using it properly. You can fix problems for yourself if you pay attention to the information that's available to you.
http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx
If you read that it is obvious that you can't pass a column name to GetString. Always read the documentation first.
Re: Data Reader giving error!!!
Code:
con.Open()
cmd = New OleDbCommand("select * from MLoan where LAcNo=@loanacno", con)
cmd.Parameters.AddWithValue("@loanacno", ComboBox1.Text)
dr = cmd.ExecuteReader
While dr.Read()
ComboBox1.Text = dr(0)
ComboBox2.Text = dr(1)
ComboBox3.Text = dr(2)
ComboBox4.Text = dr(6)
ComboBox5.Text = dr(7)
ComboBox8.Text = dr(10)
ComboBox7.Text = dr(9)
ComboBox9.Text = dr(11)
TextBox1.Text = dr(3)
TextBox2.Text = dr(4)
TextBox3.Text = dr(5)
TextBox4.Text = dr(8)
TextBox5.Text = dr(12)
End While
I did this code for using the datareader.
Is this the correct way to use the datareader?
Re: Data Reader giving error!!!