Results 1 to 8 of 8

Thread: [RESOLVED] Whats wrong with this code??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Resolved [RESOLVED] Whats wrong with this code??

    VB Code:
    1. Private Sub tbUser_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbUser.KeyPress
    2.         Dim con As New OleDb.OleDbConnection
    3.         Dim cmd As New OleDb.OleDbCommand
    4.         Dim da As New OleDb.OleDbDataAdapter
    5.         Dim ds As New DataSet
    6.         If e.KeyChar = Convert.ToChar(Keys.Enter) Then
    7.             Try
    8.                 con.ConnectionString = "Provider = Microsoft.JET.OLEDB.4.0 ; data source = (local); " & _
    9.                 "initial catalog=dbGA ; integrated security =SSPI;"
    10.                 con.Open()
    11.                 cmd = con.CreateCommand
    12.                 cmd.CommandText = "SELECT * FROM tbl_login"
    13.                 da.SelectCommand = cmd
    14.                 DataGrid1.DataSource = ds
    15.                 DataGrid1.DataMember = "tbl_login"
    16.                 da.Fill(ds, "tbl_login")
    17.                 con.Close()
    18.             Catch ex As Exception
    19.                 MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly)
    20.             End Try
    21.         End If
    22.     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..

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Re: Whats wrong with this code??

    wat objects??? sori if i have lots of questions....

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Re: Whats wrong with this code??

    ahh ok... so u mean that i should put it everytime that form loads??????

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Re: Whats wrong with this code??

    ok thanks....

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width