I tried everything that I can do - I'm a beginner at programming. I wanted to update my database in xampp using VB or Visual Studio but can't seem to do it.
This is my code for that form.:
Imports MySql.Data.MySqlClient
Public Class Form4
Public MysqlConn As MySqlConnection
Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public Sub MysqlConnection()
MysqlConn = New MySqlConnection()
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=bank"
'OPENING THE MysqlConnNECTION
MysqlConn.Open()
End Sub
Public Sub add()
Dim sql As String
Dim TempTable As New DataTable
sql = "update cbank set Balance = Balance + " & TextBox2.Text & "where AccountID = " & TextBox1.Text & "and PIN = " & TextBox3.Text & ";"
'bind the connection and query
With cmd
.Connection = MysqlConn
.CommandText = sql
End With
da.SelectCommand = cmd
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MysqlConnection()
add()
End Sub
End Class
There is no error it just that there is no output also
Keep you data objects local so you can be sure they are closed and disposed. A Using...End Using block ensures this even if there is an error. You can make your connection string a form level variable so you can use it anywhere but that is the only form level variable you need.
You can pass your connection string directly to the constructor of the connection.
You can pass your sql command text and the connection directly to the constructor of the command.
Please always use Parameters. Not only wil it save you from misplacing quotes but it will help ensure that correct datatypes are sent to the database. The most important thing is it helps protect you database from sql injection which can destroy your database. I had to guess at the datatypes in your database. Check the database and adjust the code accordingly.
Private ConnString As String = "server=localhost;user id=root;password=;database=bank"
Private Sub add()
Using cn As New MySqlConnection(ConnString)
Using cmd As New MySqlCommand("update cbank set Balance = Balance + #Balance where AccountID = #ID and PIN = #PIN;", cn)
cmd.Parameters.Add("#Balance", MySqlDbType.Decimal).Value = CDec(TextBox2.Text)
cmd.Parameters.Add("#ID", MySqlDbType.Int32).Value = CInt(TextBox1.Text)
cmd.Parameters.Add("#PIN", MySqlDbType.Int32).Value = CInt(TextBox3.Text)
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
Quotes problem.
Imports MySql.Data.MySqlClient
Public Class Form4
Public MysqlConn As MySqlConnection
Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public Sub MysqlConnection()
MysqlConn = New MySqlConnection()
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=bank"
'OPENING THE MysqlConnNECTION
MysqlConn.Open()
End Sub
Public Sub add()
Dim sql As String
Dim TempTable As New DataTable
sql = "update cbank set Balance = Balance + " & TextBox2.Text & "where AccountID = " & TextBox1.Text & "and PIN = " & TextBox3.Text & ";"
bind the connection and query
With cmd
.Connection = MysqlConn
.CommandText = sql
End With
da.SelectCommand = cmd
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Hide()
Form2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MysqlConnection()
add()
End Sub
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
Imports MySql.Data.MySqlClient
Public Class Forgot_password
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim con As New MySqlConnection("host=localhost;username=root; password=godzilla408421;database=program")
Dim cmd As New MySqlCommand
Dim dr As MySqlDataReader
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT * FROM program.login where Username ='" & useridtxt.Text & "' and Secret_question='" & questiontxt.Text & "' and answer='" & answertxt.Text & "'"
dr = cmd.ExecuteReader <<<<(HERE IS MY PROBLEM,IT TELLS ME THAT,"UNKNOW COLUMN'SECRET_QUESTION IN WHERE CLAUSE',PLEASE PLEASE PLEASE HELP"0>>>>
If Not dr Is Nothing Then
dr.Read()
passwordtxt.Text = dr(1)
dr.Close()
Else
MsgBox("Usename or password doesnt match")
End If
End Sub
End Class
First check your table whether you have Secret_question field or not, maybe you put it plural like Secret_questions. Then it's better to modify your code a bit like the following
cmd.CommandText = "SELECT * FROM program.login where Username =?Username and Secret_question=?Secret_question and answer=?answer;"
cmd.Parameters.AddWithValue("?Username", useridtxt.Text.Trim())
cmd.Parameters.AddWithValue("?Secret_question", questiontxt.Text.Trim())
cmd.Parameters.AddWithValue("?answer", answertxt.Text.Trim())
dr = cmd.ExecuteReader
I have this code in log in form, but I don't know the use of adodb.connection, please anyone help me to fix it. I don't know why the word adodb to me has an zigzag error line.
Imports System.Collections.ObjectModel
Imports System.Data
Imports System.Data.SqlClient
Public Class LoginForm1
Dim rs_login As New adodb.Recordset
Dim cn_login As New adodb.Connection
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
rs_login = cn_login.Execute("select * from dbo.studentinfo where [Username] = '" & UsernameTextBox.Text & "' And [Password] = '" & PasswordTextBox.Text & "'")
If rs_login.RecordCount = 0 Then
MsgBox("Invalid Username!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
Exit Sub
Else
rs_login = cn_login.Execute("select * from dbo.USERPASS where [Username] = '" & UsernameTextBox.Text & "' And [Password] = '" & PasswordTextBox.Text & "'")
If rs_login.RecordCount = 0 Then
MsgBox("Invalid Username", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
Exit Sub
Else
user.Show()
Me.Close()
End If
End If
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
Home.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SI.Show()
Me.Close()
End Sub
Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With cn_login
.CursorLocation = ADODB.CursorLocationEnum.adUseClient
.Provider = "SQLOLEDB.1"
.CommandTimeout = 0
Dim con As New SqlConnection With {.ConnectionString = "Server=Danica-pc; database=SI;user=dandan;pwd=danica;"}
.Open()
End With
End Sub
End Class
You are using a database access code written for VBA. Probably for MS Access. In VB.NET this works very differently. You would use this Imports statement:
Imports System.Data.SqlClient
In VB.NET you would do something like this
Const StandardSecurityConnection As String = _
"Server=Danica-pc;Database=SI;User Id=dandan;Password=danica;"
Const TrustedConnection As String = _
"Server=Danica-pc;Database=SI;Trusted_Connection=True;"
Using conn As New SqlConnection(StandardSecurityConnection) 'Or TrustedConnection
Dim sql As String = _
"SELECT * FROM dbo.studentinfo WHERE Username = #usr AND Password = #pwd"
Using command As New SqlCommand(sql, conn)
command.Parameters.AddWithValue("#usr", UsernameTextBox.Text)
command.Parameters.AddWithValue("#pwd", PasswordTextBox.Text)
conn.Open()
Using dr As SqlDataReader = command.ExecuteReader()
Dim userCol AS Integer = dr.GetOrdinal("Username")
Dim pwdCol AS Integer = dr.GetOrdinal("Password")
While dr.Read()
ConSole.WriteLine("User = {0}, Password = {1}",
dr.GetString(userCol), dr.GetString(pwdCol))
End While
End Using
End Using
End Using
Every time I run this code for putting some Student information, when I click to save it, there are always appear messsage in cmd.Executenonquery()..please help of this..
Imports System.Collections.ObjectModel
Imports System.Data.SqlClient
Imports System.Data
Public Class SI
Dim con As New SqlConnection With {.ConnectionString = "Server=Danica-pc; database=SI;user=dandan;pwd=danica;"}
Dim cmd As New SqlCommand
Dim query As String
Dim stuid, i As Integer
Dim studentID As Integer
Dim StudentBindingSource As Object
Dim TableAdapterManager As Object
Private Sub StudentBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.StudentBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.SIDataSet)
End Sub
Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Kasarian.Click
End Sub
Private Sub SI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SIDataSet.Studentinfo' table. You can move, or remove it, as needed.
Me.StudentinfoTableAdapter.Fill(Me.SIDataSet.Studentinfo)
End Sub
Private Sub getData()
i = DataGridView1.CurrentCell.RowIndex()
studentID = i
End Sub
Private Sub dataReload()
familynem.Clear()
middlenem.Clear()
givennem.Clear()
usename.Clear()
accpass.Clear()
confirmpass.Clear()
Try
Dim sql As String = "Select * from Studentinfo"
Dim myAdapter As New SqlDataAdapter(sql, con)
con.Open()
Dim myDataset As New DataSet()
myAdapter.Fill(myDataset, "SI")
DataGridView1.DataSource = myDataset
DataGridView1.DataMember = "SI"
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub famliynem_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles familynem.TextChanged
End Sub
Private Sub stat_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stat.SelectedIndexChanged
End Sub
Private Sub HomeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HomeToolStripMenuItem.Click
Home.Show()
Me.Hide()
End Sub
Private Sub EventsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EventsToolStripMenuItem.Click
EventsForm.Show()
Me.Hide()
End Sub
Private Sub ProductsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProductsToolStripMenuItem.Click
Products.Show()
Me.Hide()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
Home.Show()
Me.Close()
End
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
Dim genderval As String
Dim birthdate As String
birthdate = bday.Value.ToString()
If babae.Checked = True Then
genderval = "Female"
Else
genderval = "Male"
End If
query = "insert into studentinfo(Lastname,Firstname,middlename,birthdate,gender,username)""values('" & familynem.Text & "','" & givennem.Text & "','" & middlenem.Text & "','" & birthdate & "','" & genderval & "','" & usename.text & "')"
con.Open()
cmd = New SqlCommand(query, con)
*cmd.ExecuteNonQuery()*
con.Close()
dataReload()
user.Show()
Me.Hide()
End Sub
End Class
You have an unwanted "" in this line:
query = "insert into studentinfo(Lastname,Firstname,middlename,birthdate,gender,username)""values('" & familynem.Text & "','" & givennem.Text & "','" & middlenem.Text & "','" & birthdate & "','" & genderval & "','" & usename.text & "')"
Also, I recommend that you look into using SQL parameters to pass the values:
Edit: You can use SQL parameters by replacing this code:
query = "insert into studentinfo(Lastname,Firstname,middlename,birthdate,gender,username)""values('" & familynem.Text & "','" & givennem.Text & "','" & middlenem.Text & "','" & birthdate & "','" & genderval & "','" & usename.text & "')"
con.Open()
cmd = New SqlCommand(query, con)
*cmd.ExecuteNonQuery()*
con.Close()
with:
Using conn As New SqlConnection("YOUR CONNECTION STRING")
Dim query = "INSERT INTO studentinfo(Lastname,Firstname,middlename,birthdate,gender,username) VALUES(#familynem, #givennem, #middlenem, #birthdate, #genderval, #usename)"
Using cmd As New SqlCommand(query, conn)
cmd.Parameters.AddWithValue("#familynem", familynem.Text)
cmd.Parameters.AddWithValue("#givennem", givennem.Text)
cmd.Parameters.AddWithValue("#middlenem", middlenem.Text)
cmd.Parameters.AddWithValue("#birthdate", birthdate)
cmd.Parameters.AddWithValue("#genderval", genderval.Text)
cmd.Parameters.AddWithValue("#usename", usename.Text)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
The Using constructs take care of calling .Dispose() for you, and you should not have connections hanging around. SQL parameters help prevent SQL injection attacks, and stop the query from breaking if you have a name like O'Reilly, where the apostrophe would be a problem.
Imports MySql.Data.MySqlClient
Imports MySql.Data
Public Class Form1
Dim dbcon As New MySqlConnection("Data Source=VINCENT-PC\SQLEXPRESS;Initial Catalog=Group1;Integrated Security=True")
Dim sqlcmd As String
Dim mysqlcmd As MySqlCommand
Dim dr As MySqlDataReader
Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
Try
sqlcmd = "Select * from Sample1 where num=" + txtname.Text
mysqlcmd = New MySqlCommand(sqlcmd, dbcon)
dbcon.Open()
dr = mysqlcmd.ExecuteReader
While dr.Read
txtfname = dr.Item("fname")
txtlname = dr.Item("lname")
End While
dr.Close()
dbcon.Close()
Catch ex As Exception
MsgBox("Error: " & vbCrLf & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub
End Class
this is the code that i wrote but it seemed like it doesn't connect to my database please help me and thank you in advance
Try the Following code :
Imports System.Data.SqlClient
Dim Conn As SqlConnection
Dim sqlcmd As String
Dim mysqlcmd As SqlCommand
Dim dr As SqlDataReader
Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
dim m_CurrConstr as String
dim m_svrname as String
dim m_Currentdbf as String
m_Srvname="VINCENT-PC\SQLEXPRESS"
m_Currentdbf ="[YourDBName]"
Try
m_CurrConStr = "Server=" & m_Srvname & ";Initial Catalog=" & m_Currentdbf & ";User ID=sa;Password=as;Trusted_Connection=False;"
Conn = new Sqlconnection(m_currConstr)
sqlcmd = "Select * from Sample1 where num=" + txtname.Text
mysqlcmd = New SqlCommand(sqlcmd, Connn)
Conn.Open()
dr = mysqlcmd.ExecuteReader
While dr.Read
txtfname = dr.Item("fname")
txtlname = dr.Item("lname")
End While
dr.Close()
Conn.Close()
Catch ex As Exception
Messagebox.Show("Error: " & vbCrLf & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub
Mark as Answer if I answered your query