[RESOLVED] Whats wrong with this code??
VB Code:
Private Sub tbUser_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbUser.KeyPress
Dim con As New OleDb.OleDbConnection
Dim cmd As New OleDb.OleDbCommand
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
If e.KeyChar = Convert.ToChar(Keys.Enter) Then
Try
con.ConnectionString = "Provider = Microsoft.JET.OLEDB.4.0 ; data source = (local); " & _
"initial catalog=dbGA ; integrated security =SSPI;"
con.Open()
cmd = con.CreateCommand
cmd.CommandText = "SELECT * FROM tbl_login"
da.SelectCommand = cmd
DataGrid1.DataSource = ds
DataGrid1.DataMember = "tbl_login"
da.Fill(ds, "tbl_login")
con.Close()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly)
End Try
End If
End Sub
Everytime I run this program and presses enter a messagebox will show saying that...
"Error: Microsoft OLE DB Service Components: Multiple-step OLE DB Operation generated errors. Check each OLE DB status value, if available. No work was done"
I hope someone could solve this...
Thanks..
By the way using VB.NET 2003 and ACCESS 2003..
Re: Whats wrong with this code??
At which point does throw the exception? Please always provide details like that. If it's at the Open method then it's because your connection string is screwed up. I could be wrong but that doesn't look like a valid connection string for Access to me. If the connection is failing to open then visit www.connectionstrings.com and see what you should have.
On another note, why do you create four new objects and then test the key press, only to throw the objects away if it wasn't the Enter key? That measn that every time the user types a character you will create and discard those four objects. That's a bit wasteful don't you think? Test the key first and then only create the objects if you need them. Don't get caught in that "must declare variables at top of method no matter what" trap.
Re: Whats wrong with this code??
wat objects??? sori if i have lots of questions....
Re: Whats wrong with this code??
You've only got four lines of code before you test the key so the possibilities are few. You create an OleDbConnection object, an OleDbCommand object, an OleDbDataAdapter object and a DataSet object that all simply get discarded if the key pressed was not Enter. That's like baking a birthday cake every day and then checking if it's anyone's birthday.
Re: Whats wrong with this code??
ahh ok... so u mean that i should put it everytime that form loads??????
Re: Whats wrong with this code??
You should put it wherever is most appropriate, but don't create them over and over if you aren't going to use them. You might want to creat them every time the user presses the Enter key, but if so they should be inside the If block so that you know you need them before creating them.
Re: Whats wrong with this code??
Re: Whats wrong with this code??
You both declare and instantiate the variables right off, but don't use the instances if the wrong key is pressed. To fix that, you can do the declaration as you have:
Dim con As OleDb.OleDbConnection
But then you will need to instantiate them when you need them:
con = New OleDb.OleDbConnection
The first line will simply tell the compiler to allocate the space for a variable, but it won't be created until you use the second line.