I got exception, column cannot be null, Did not save to my database.
Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
con.close()
con.open()
cmd = New Odbc.OdbcCommand("INSERT INTO db.table(firstname,lastname) VALUES(#f1,#f2)", con)
cmd.Parameters.AddWithValue("#f1", textboxfirstname.Text)
cmd.Parameters.AddWithValue("#f2", textboxlastname.Text)
cmd.ExecuteNonQuery()
End Sub
But If I will use this code its working
Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
con.close()
con.open()
cmd = New Odbc.OdbcCommand("INSERT INTO db.table(firstname) VALUES('" & textboxfirstname.Text & "')", con)
cmd.ExecuteNonQuery()
End Sub
Related
I wanted to make a connection to my Clients.mdf database with MySql but I'm not finding out where the error is.
Imports MySql.Data.MySqlClient
Imports Microsoft.Reporting.WinForms
Public Class Form8
Private Kon As New
I wanted to make a connection to my Clients.mdf database with MySql but I'm not finding out where the error is
MySqlConnection("Server=C:\Users\Utilizador\Desktop\
ClinicaProject\Clinica.mdf ;user id= ;database= Clinica.mdf")
Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub VoltarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VoltarToolStripMenuItem.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cmd As MySqlCommand
Dim adp As New MySqlDataAdapter
Dim sql As String
sql = "Select * From Consultas Where Data = '" & DateTimePicker1.Text & "'"
Try
Me.DataSet1.Clear()
Kon.Open()
cmd = New MySqlCommand(sql, Kon)
adp.SelectCommand = cmd
adp.Fill(Me.DataSet1.Consultas)
cmd.Dispose()
adp.Dispose()
Kon.Close()
Catch ex As Exception
Kon.Close()
MessageBox.Show(ex.Message)
End Try
Me.ReportViewer1.RefreshReport()
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
End Sub
End Class
Imports MySql.Data.MySqlClient
Public Class Forgot_Password_form
Dim con As New MySqlConnection("host=localhost;username=root;password=system;database=bike")
Dim cmd As New MySqlCommand
Dim dr As MySqlDataReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
con.Open()
cmd.Connection = con
cmd.CommandText = "select * from login where userid='" & useridtxt.Text & "'and question='" & question.Text & "'and answer='" & answertxt.Text & "'"
dr = cmd.ExecuteReader
If Not dr Is Nothing Then
dr.Read()
answer.Text = dr(1)
dr.Close()
End If
Catch ex As Exception
MessageBox.Show("UserID or answer is incorrect")
End Try
con.Close()
End Sub
Private Sub Exit__Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exit_.Click
End
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Login_Menu.Show()
Me.Hide()
End Sub
Private Sub useridtxt_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles useridtxt.LostFocus
Try
con.Open()
cmd.Connection = con
cmd.CommandText = "select * from login where userid='" & useridtxt.Text & "'"
dr = cmd.ExecuteReader
If Not dr Is Nothing Then
dr.Read()
question.Text = dr(3)
dr.Close()
End If
Catch ex As Exception
End Try
End Sub
End Class
I have an application of image loader, but what I want is to have a procedure for next and previous button so that I can view them one by one.
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.Click
Try
disconnect()
connect()
cmd = New Odbc.OdbcCommand("Select * FROM tblemployee WHERE lname ='" & Trim(TextBox1.Text.TrimEnd()) & "' OR fname ='" & Trim(TextBox1.Text.TrimEnd()) & "'", con)
dr = cmd.ExecuteReader
If dr.Read() Then
Dim bytBLOBData() As Byte = dr("emp_pix")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
PictureBox1.Image = Image.FromStream(stmBLOBData)
Else
MessageBox.Show("No Information Record, Please Last Name only!")
End If
Catch ex As Exception
Debug.WriteLine("Please try again" & ex.Message)
End Try
End Sub
How can I manipulate for next and previous button?
Private Sub Next_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Next.Click
end sub
'Do Here
Private Sub Previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Previous.Click
end sub
'Do Here
I wrote this update code for mysql server using vb.net and its not working.
Dim sqlstring = "Update initial_nom set f_name = '" &
TextBox1.Text & "',s_name = '" &
TextBox2.Text & "',th_name = '" &
TextBox3.Text & "',fo_name = '" &
TextBox4 & "',adm_type = '" &
ComboBox4.SelectedItem.ToString() & "'
where (app_no = " & TextBox5.Text & ")"
cmd.ExecuteNonQuery()
Try this:
Dim connectionString As String = "Server=my_server;Database=my_db;Uid=my_username;Pwd=my_password;"
Dim SQLConnection As New MySqlConnection(connectionString)
Dim SQLCommand As New MySqlCommand()
With SQLCommand
.CommandText = "UPDATE initial_nom SET f_name = #f_name, s_name = #s_name, th_name = #th_name, fo_name = #fo_name, adm_type = #adm_type WHERE app_no = #app_no"
.Connection = SQLConnection
.Parameters.AddWithValue("#f_name", TextBox1.Text)
.Parameters.AddWithValue("#s_name", TextBox2.Text)
.Parameters.AddWithValue("#th_name", TextBox3.Text)
.Parameters.AddWithValue("#fo_name", TextBox4.Text)
.Parameters.AddWithValue("#app_no", TextBox5.Text)
.Parameters.AddWithValue("#adm_type", ComboBox4.SelectedItem.ToString)
End With
Try
SQLConnection.Open()
SQLCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
Finally
SQLConnection.Close()
End Try
Don't forget to enter your own details in the connection string.
In answer to your other closed question, this should do what you want:
Dim connectionString As String = "Server=my_server;Database=my_db;Uid=my_username;Pwd=my_password;"
Dim SQLConnection As New MySqlConnection(connectionString)
Dim SQLCommand As New MySqlCommand()
With SQLCommand
.CommandText = "INSERT INTO nomination (f_name, s_name, th_name, fo_name, app_no, adm_type) values (#f_name, #s_name, #th_name, #fo_name, #app_no, #adm_type)"
.Connection = SQLConnection
.Parameters.AddWithValue("#f_name", TextBox1.Text)
.Parameters.AddWithValue("#s_name", TextBox2.Text)
.Parameters.AddWithValue("#th_name", TextBox3.Text)
.Parameters.AddWithValue("#fo_name", TextBox4.Text)
.Parameters.AddWithValue("#app_no", TextBox5.Text)
.Parameters.AddWithValue("#adm_type", ComboBox4.SelectedItem.ToString)
End With
Try
SQLConnection.Open()
SQLCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
Finally
SQLConnection.Close()
End Try
Update
I noticed in your other question that you were struggling with ADODB and a connection DSN, there is a far easier way to do this using the MySQL Connector.
Download and install the MySQL Connector from http://dev.mysql.com/downloads/connector/net/
You now need to add a reference to this in your project:
Select Browse and navigate to the Assemblies directory for the MySQL Connector:
Select MySQL.Data.dll and click Ok:
Now all you need to do is add Imports MySql.Data.MySqlClient to the very top of your form code and you will be able to access your database once you have created the correct connection string.
Here is some example code which will give you and idea of how you can perform some simple operations on your DB, this is tested and working (I was bored this morning!).
Imports MySql.Data.MySqlClient
Public Class Form1
Dim connectionString As String = "Server=my_server;Database=my_db;Uid=my_username;Pwd=my_password;"
Dim SQLConnection As New MySqlConnection(connectionString)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 5
ComboBox4.Items.Add(i)
Next
ComboBox4.SelectedIndex = 0
End Sub
Private Sub update_row()
Dim SQLCommand As New MySqlCommand()
With SQLCommand
.CommandText = "UPDATE nomination SET f_name = #f_name, s_name = #s_name, th_name = #th_name, fo_name = #fo_name, adm_type = #adm_type WHERE app_no = #app_no"
.Connection = SQLConnection
.Parameters.AddWithValue("#f_name", TextBox1.Text)
.Parameters.AddWithValue("#s_name", TextBox2.Text)
.Parameters.AddWithValue("#th_name", TextBox3.Text)
.Parameters.AddWithValue("#fo_name", TextBox4.Text)
.Parameters.AddWithValue("#app_no", TextBox5.Text)
.Parameters.AddWithValue("#adm_type", ComboBox4.SelectedItem.ToString)
End With
Try
SQLConnection.Open()
SQLCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
Finally
SQLConnection.Close()
End Try
End Sub
Private Sub insert_row()
Dim SQLCommand As New MySqlCommand()
With SQLCommand
.CommandText = "INSERT INTO nomination (f_name, s_name, th_name, fo_name, app_no, adm_type) values (#f_name, #s_name, #th_name, #fo_name, #app_no, #adm_type)"
.Connection = SQLConnection
.Parameters.AddWithValue("#f_name", TextBox1.Text)
.Parameters.AddWithValue("#s_name", TextBox2.Text)
.Parameters.AddWithValue("#th_name", TextBox3.Text)
.Parameters.AddWithValue("#fo_name", TextBox4.Text)
.Parameters.AddWithValue("#app_no", TextBox5.Text)
.Parameters.AddWithValue("#adm_type", ComboBox4.SelectedItem.ToString)
End With
Try
SQLConnection.Open()
SQLCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
Finally
SQLConnection.Close()
End Try
End Sub
Private Sub select_from()
Dim SQLCommand As New MySqlCommand
Dim SQLAdapter As New MySqlDataAdapter
Dim DTable As New DataTable
With SQLCommand
.CommandText = "SELECT * FROM nomination"
.Connection = SQLConnection
End With
Try
SQLConnection.Open()
SQLAdapter.SelectCommand = SQLCommand
SQLAdapter.Fill(DTable)
DataGridView1.DataSource = DTable
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
Finally
SQLConnection.Close()
End Try
End Sub
Private Sub create_table()
Dim SQLCommand As New MySqlCommand
With SQLCommand
.CommandText = "CREATE TABLE nomination (f_name varchar(50),s_name varchar(50),th_name varchar(50),fo_name varchar(50),app_no varchar(50),adm_type varchar(50))"
.Connection = SQLConnection
End With
Try
SQLConnection.Open()
SQLCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
Finally
SQLConnection.Close()
End Try
End Sub
Private Sub drop_table()
If MsgBox("Are you really sure you want to DROP this table?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Dim SQLCommand As New MySqlCommand
With SQLCommand
.CommandText = "DROP TABLE nomination"
.Connection = SQLConnection
End With
Try
SQLConnection.Open()
SQLCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
Finally
SQLConnection.Close()
End Try
End If
End Sub
Private Sub truncate_table()
If MsgBox("Are you really sure you want to TRUNCATE this table?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Dim SQLCommand As New MySqlCommand
With SQLCommand
.CommandText = "TRUNCATE TABLE nomination"
.Connection = SQLConnection
End With
Try
SQLConnection.Open()
SQLCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
Finally
SQLConnection.Close()
End Try
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
create_table()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
drop_table()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
insert_row()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
update_row()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
select_from()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
truncate_table()
End Sub
End Class
Here's the form I used, it needs Button1 - Button6, TextBox1 - TextBox5, ComboBox4 and a DataGridView control.
Hope this helps you out.
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.