It looks like you're new here. If you want to get involved, click one of these buttons!
Currently I am using Visual Basic 2008 to create a password vault. An application to hold all my passwords in one location, that way, I dont need to keep them in a fire-safe vault. I am having a lot of issues with this when trying to add information INTO a database. Right now, it's bombing out at the "objOleDbCommand.ExecuteNonQuery() statement.
Here is my code from start to finish. It may look choppy, and it is. Right now, I have no idea what I am doing and the teacher is refusing to help. To provide a little more light on the situation. I have asked students who have passed this class to help, and none of them can.
'Date: 4/27/2010
'Purpose: To store and protect passwords.
Option Strict On
Option Explicit On
Imports System.Data.OleDb
Public Class frmCreate
Inherits System.Windows.Forms.Form
Dim sCN As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=information.accdb; Persist Security Info=False;"
Private Sub frmCreate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'Clear the Textboxes
txtUserName.Clear()
txtPassword.Clear()
txtEmail.Clear()
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim strSQL As String
Dim objOleDbConnection As New OleDbConnection(sCN)
Dim objOleDbCommand As New OleDbCommand
'Open Database
objOleDbCommand.Connection = objOleDbConnection
objOleDbCommand.Connection.Open()
strSQL = "INSERT INTO Users (User_Name, Password, Email) VALUES ('" & txtUserName.Text & "','" & txtPassword.Text & "','" & txtEmail.Text & "')"
objOleDbCommand.CommandText = strSQL
'Execute SQL Statement
objOleDbCommand.ExecuteNonQuery()
'Close connection
objOleDbConnection.Close()
objOleDbConnection.Dispose()
objOleDbCommand.Dispose()
End Sub
Private Sub txtPassword_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPassword.TextChanged
txtPassword.PasswordChar = "*"c
txtPassword.MaxLength = 8
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Close the application
Me.Close()
End Sub
End Class
KillerTwinkie - That one guy who used to mod mmorpg.com's forums.
Comments
.Net or no?
What do you mean by "bombing out"?
Does it compile correctly and "bomb out" at runtime when you try to complete the SQL transaction? If so, try two things:
1. Surround objOleDbCommand.ExecuteNonQuery() in a try catch block and see if it throws an SqlException. I don't really write VB but the syntax should be as simple as:
"Try
objOleDbCommand.ExecuteNonQuery()
Catch ex As SqlException"
Establish a for loop in the Catch block that iterates through ex.Errors(); it'll have four values, ex.Errors().Message, ex.Errors().LineNumber, ex.Errors().Source, ex.Errors().Procedure; you're probably only interested in the Message and theres probably only one, so you could just go with displaying the string of ex.Errors(0).Message wherever you want.
2. Assign the integer returned by objOleDbCommand.ExecuteNonQuery() to a variable and read it out somewhere as well for debugging purposes. It returns the number of affected rows by your query.
All and all I could probably help more if you elaborated more on what you meant by "bomb out". If your problem is happening runtime though and it is a SqlException, capture those error messages. It could be as simple as a syntax error or your database isn't configured right (Access is it? Can't use an MSSQL .mdf?)
Yeah, it's VB.Net 2008. Honestly wish we could use 2010, but that would make things easy, and I'm assuming a lot of industry is still using 2005; i'm sure some smaller shops are using an even older version.
The issue was resolved using the Try Catch, thanks Seph . You seem to always come around when I need some advice and I appreciate that a lot. Sorry for the ill-description. I was frustrated to no end when I wrote that.
When I was "Bombing Out" at compile time (running the app) it would spit out a Syntax Error in the SQL. I found out that SQL was concatinating two lines because I missed a space in its syntax... Two days of horror because of a space...
In case you're interested this is what I did for the Try Catch.
Try
'Instantiate an object that opend the datase
Dim objConnection As New OleDbConnection(sCN)
objConnection.Open()
'check www.w3chools.com for auto help also
'create the SQL statement ' (User_Name, Password, Email)" _
strSQL = "INSERT INTO Users" _
& " VALUES ('" & txtUserName.Text & "','" & txtPassword.Text _
& "','" & txtEmail.Text & "')"
'open the command
Dim objCommand As New OleDbCommand(strSQL, objConnection)
'execute command
objCommand.ExecuteNonQuery()
'close connection
objConnection.Close()
objCommand.Dispose()
objConnection.Dispose()
Catch ex As Exception
MessageBox.Show("ex")
End Try
KillerTwinkie - That one guy who used to mod mmorpg.com's forums.