i'm new learning visual studio. and i get an error
Connection property has not been initialized
This my code:
Imports System.Data.SqlClient
Public Class Form1
Dim con As SqlConnection
Dim cm As SqlCommand
Dim dr As SqlDataReader
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
TextBox2.PasswordChar = ""
Else
TextBox2.PasswordChar = "•"
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.MaxLength = 50
TextBox2.PasswordChar = "•"
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("pls fill the data", MsgBoxStyle.Information)
Exit Sub
Else
Call Koneksi()
cm = New SqlCommand("select*from login where username='" & TextBox1.Text & "'and password" & TextBox2.Text & "'", con)
cm.Connection = con
dr = cm.ExecuteReader
dr.Read()
last.Show()
Me.Close()
If dr.HasRows Then
MsgBox("succes", MsgBoxStyle.Information, "information")
Else
MsgBox("wrong password/username", MsgBoxStyle.Critical)
TextBox1.Clear()
TextBox2.Clear()
End If
End If
End Sub
End Class
Related
Imports MySql.Data.MySqlClient
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim mstream As New System.IO.MemoryStream()
picture_me.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
mstream.Close()
Try
cmd = New MySqlCommand("insert into piccs(Photo,id) values(#image_data,#image_id")
cmd.Parameters.AddWithValue("#image_data", Nothing)
cmd.Parameters.AddWithValue("#image_id", arrImage)
cmd.ExecuteNonQuery()
MsgBox("saved")
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
conn()
End Sub
Private Sub browser_image_Click(sender As Object, e As EventArgs) Handles browser_image.Click
Me.find_image.FileName = Nothing
If find_image.ShowDialog() = DialogResult.OK Then
' Dim query1 As String
' cmd = New MySqlCommand
'cmd.Connection = connect
'query1 = "Insert into picss(id, Photo) values('" & TextBox1.Text & "','" & find_image.SafeFileNames(x) & "',#IMAGE,'" & Batch & "')"
If Not Me.find_image.FileName = Nothing Then
Me.picture_me.ImageLocation = find_image.FileName
End If
End If
End Sub
End Class
Somebody check my code for inserting image into the database.I know there's some error but can't figure it out.When i run the code it worked but when i click the done button it always says NullReferenceException Here's What I've got
Imports MySql.Data.MySqlClient
Imports System.IO
Public Class adminreg
Dim con As MySqlConnection
Dim cmd As MySqlCommand
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim reader As MySqlDataReader
Dim mstream As New System.IO.MemoryStream
PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
mstream.Close()
con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"
Try
con.Open()
Dim query As String
query = "insert into dat.login (idlogin,user,password,name,position,ownpic) values ('" & id.Text & "','" & usertxt.Text & "','" & passtxt.Text & "','" & nmetxt.Text & "','" & postxt.Text & "',#ownpic)"
cmd.Parameters.AddWithValue("#ownpic", arrImage)
cmd = New MySqlCommand(query, con)
reader = cmd.ExecuteReader
MessageBox.Show("Data Saved")
con.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub
Private Sub adminreg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
OpenFileDialog1.Filter = "imahe lang (*.jpg, *.bmp, *.png) | *.jpg; *.bmp; *.png| all files (*.*) | *.*"
If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Login.Show()
Me.Hide()
End Sub
End Class
Any Suggestion is highly appreciated Thank you...Just a beginner in VB.net
Private Sub pbStudentImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbStudentImage.Click, txtStudentImage.Enter, txtStudentImage.Click
Try
Dim fileOpener As OpenFileDialog = New OpenFileDialog()
fileOpener.Filter = "Image files | *.jpg"
If fileOpener.ShowDialog() = Windows.Forms.DialogResult.OK Then
pbStudentImage.Image = Image.FromFile(fileOpener.FileName)
txtStudentImage.Text = fileOpener.FileName
End If
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub btnSaveImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveImage.Click
Dim con As MySqlConnection = New MySqlConnection(ConString)
Dim cmd As MySqlCommand
Dim fs As FileStream
Dim br As BinaryReader
Try
If txtFirstName.Text.Length > 0 And txtStudentImage.Text.Length > 0 Then
Dim FileName As String = txtStudentImage.Text
Dim ImageData() As Byte
fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
br = New BinaryReader(fs)
ImageData = br.ReadBytes(CType(fs.Length, Integer))
br.Close()
fs.Close()
Dim CmdString As String = "INSERT INTO Students(FirstName, LastName, Image, Address) VALUES(#FirstName, #LastName, #Image, #Address)"
cmd = New MySqlCommand(CmdString, con)
cmd.Parameters.Add("#FirstName", MySqlDbType.VarChar, 45)
cmd.Parameters.Add("#LastName", MySqlDbType.VarChar, 45)
cmd.Parameters.Add(";#Image", MySqlDbType.Blob)
cmd.Parameters.Add("#Address", MySqlDbType.VarChar, 100)
cmd.Parameters("#FirstName").Value = txtFirstName.Text
cmd.Parameters("#LastName").Value = txtLastName.Text
cmd.Parameters("#Image").Value = ImageData
cmd.Parameters("#Address").Value = txtAddress.Text
con.Open()
Dim RowsAffected As Integer = cmd.ExecuteNonQuery()
If (RowsAffected > 0) Then
MsgBox("Image saved successfully!")
End If
con.Close()
Else
MsgBox("Incomplete data!", MsgBoxStyle.Critical, "")
End If
Catch ex As Exception
MsgBox(ex.ToString())
Finally
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Sub
Private Sub pbStudentImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbStudentImage.Click, txtStudentImage.Enter, txtStudentImage.Click
Try
Dim fileOpener As OpenFileDialog = New OpenFileDialog()
fileOpener.Filter = "Image files | *.jpg"
If fileOpener.ShowDialog() = Windows.Forms.DialogResult.OK Then
pbStudentImage.Image = Image.FromFile(fileOpener.FileName)
txtStudentImage.Text = fileOpener.FileName
End If
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub pbStudentImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbStudentImage.Click, txtStudentImage.Enter, txtStudentImage.Click
Try
Dim fileOpener As OpenFileDialog = New OpenFileDialog()
fileOpener.Filter = "Image files | *.jpg"
If fileOpener.ShowDialog() = Windows.Forms.DialogResult.OK Then
pbStudentImage.Image = Image.FromFile(fileOpener.FileName)
txtStudentImage.Text = fileOpener.FileName
End If
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub btnSaveImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveImage.Click
Dim con As MySqlConnection = New MySqlConnection(ConString)
Dim cmd As MySqlCommand
Dim fs As FileStream
Dim br As BinaryReader
Imports MySql.Data.MySqlClient
Public Class Form1
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BrandsTableAdapter.Fill(Me.StoreDataSet.brands)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server=localhost;userid=root;password=;database=brands"
Dim READER As MySqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "insert into database.brands(id,name,,created_at,updated_at) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
MessageBox.Show("Data saved")
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
End Class
I'm almost done with a trial program where I add, edit and delete stuff from my MySQL database.
But I can't seem to make the delete button to work.
Here's my code for the Delete Button:
If IDNo = Nothing Then
MsgBox("Please choose an item to delete.", MsgBoxStyle.Exclamation)
Else
Dim sqlQuery As String = "DELETE FROM tbl_adbms_test WHERE IDNo='" & IDNo & "'"
Dim sqlCommand As New MySqlCommand
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.ExecuteNonQuery()
End With
MsgBox("Successfully deleted an item.", MsgBoxStyle.Information)
Me.LoadPeople()
End If
The ERROR
http://stivigan.us.to/images/delete_error.jpg
And here's the rest of my Main Form.
Imports MySql.Data.MySqlClient
Public Class frm_main
Public sConnection As New MySqlConnection
Public IDNo As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD = loadedro; DATABASE = adbms_test_db"
End If
LoadPeople()
End Sub
Public Sub LoadPeople()
Dim sqlQuery As String = "SELECT * FROM tbl_adbms_test"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim TABLE As New DataTable
Dim i As Integer
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(TABLE)
End With
list_view_people.Items.Clear()
For i = 0 To TABLE.Rows.Count - 1
With list_view_people
.Items.Add(TABLE.Rows(i)("IDNo"))
With .Items(.Items.Count - 1).SubItems
.Add(TABLE.Rows(i)("LastName"))
.Add(TABLE.Rows(i)("FirstName"))
End With
End With
Next
End Sub
Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_add.Click
frm_add.ShowDialog()
End Sub
Private Sub btn_modify_Click(sender As Object, e As EventArgs) Handles btn_modify.Click
If IDNo = Nothing Then
MsgBox("Please choose a record to modify.", MsgBoxStyle.Exclamation)
Else
Dim sqlQuery As String = "SELECT LastName, FirstName FROM tbl_adbms_test WHERE IDNo='" & list_view_people.SelectedItems(0).Text & "'"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim sqlTable As New DataTable
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(sqlTable)
End With
frm_modify.IDNo = list_view_people.SelectedItems(0).Text
frm_modify.LastName = sqlTable.Rows(0)("LastName")
frm_modify.FirstName = sqlTable.Rows(0)("FirstName")
frm_modify.ShowDialog()
End If
End Sub
Private Sub list_view_people_MouseClick(sender As Object, e As MouseEventArgs) Handles list_view_people.MouseClick
IDNo = list_view_people.SelectedItems(0).Text
End Sub
Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click
If IDNo = Nothing Then
MsgBox("Please choose an item to delete.", MsgBoxStyle.Exclamation)
Else
Dim sqlQuery As String = "DELETE FROM tbl_adbms_test WHERE IDNo='" & IDNo & "'"
Dim sqlCommand As New MySqlCommand
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.ExecuteNonQuery()
End With
MsgBox("Successfully deleted an item.", MsgBoxStyle.Information)
Me.LoadPeople()
End If
End Sub
End Class
Add Form
Public Class frm_add
Public sConnection As New MySqlConnection
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD = loadedro; DATABASE = adbms_test_db"
End If
End Sub
Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD = loadedro; DATABASE = adbms_test_db"
sConnection.Open()
End If
Dim sqlQuery As String = "INSERT INTO tbl_adbms_test(IDNo,LastName,FirstName) VALUES(NULL,'" & txt_last_name.Text & "','" & txt_first_name.Text & "')"
Dim sqlCommand As New MySqlCommand
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.ExecuteNonQuery()
End With
MsgBox("The data was saved.", MsgBoxStyle.Information)
Dispose()
Close()
frm_main.LoadPeople()
End Sub
End Class
Edit Form
Imports MySql.Data.MySqlClient
Public Class frm_modify
Friend IDNo As Integer
Friend LastName As String
Friend FirstName As String
Public sConnection As New MySqlConnection
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txt_last_name.Text = LastName
txt_first_name.Text = FirstName
End Sub
Private Sub btn_update_Click(sender As Object, e As EventArgs) Handles btn_update.Click
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = "SERVER = localhost; USERID = root; PASSWORD = loadedro; DATABASE = adbms_test_db"
sConnection.Open()
End If
Dim sqlQuery As String = "UPDATE tbl_adbms_test SET LastName='" & txt_last_name.Text & "', FirstName='" & txt_first_name.Text & "' WHERE IDNo='" & IDNo & "'"
Dim sqlCommand As New MySqlCommand
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.ExecuteNonQuery()
End With
MsgBox("Record updated successfully.", MsgBoxStyle.Information)
Dispose()
Close()
frm_main.LoadPeople()
End Sub
End Class
Thanks in advance. :)
You don't have the connection open before executing the Delete command.
This is a common scenario when you keep a global connection object around in your code.
You gain nothing and there are always situations in which you end with the connection in a wrong state
You could write
Dim sqlQuery As String = "DELETE FROM tbl_adbms_test WHERE IDNo=#id"
if sConnection.ConnectionState = ConnectionState.Closed Then
sConnection.Open
End If
Dim sqlCommand As New MySqlCommand
With sqlCommand
.CommandText = sqlQuery
.Connection = sConnection
.Parameters.AddWithValue("#id", IDNo)
.ExecuteNonQuery()
End With
but I really suggest to remove your usage of the global connection object and replace it with a local MySqlConnection that will be created just when you use it and closed/destroyed after the usage. This is the intended usage of the Using Statement
Dim sqlQuery As String = "DELETE FROM tbl_adbms_test WHERE IDNo=#id"
Using con = new MySqlConnection(connstring)
Using cmd = new MySqlCommand(sqlQuery, con)
con.Open
With cmd
.Parameters.AddWithValue("#id", IDNo)
.ExecuteNonQuery()
End With
End Using
End Using
Notice also that I have removed the string concatenation in your sqlQuery and used a safer parameterized approach (albeit in this scenario and if the ListView is not editable there are no real risk of sql injection)
This code is now working. i want to save an image in database please help me how i can work on this
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
TextBox1.Text = OpenFileDialog1.FileName
CaptionTextBox.Text = OpenFileDialog1.SafeFileName
' ImagePictureBox.Image = image.FromFile(TextBox1.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
updateRecord("insert into uploadimages(caption,image) values('" + CaptionTextBox.Text + "', '#ImagePictureBox' )")
End Sub
Imports MySql.Data.MySqlClient
Imports System.Drawing.Imaging
Dim cn As New MySqlConnection
Dim con As New MySqlConnection
Dim cmd As New MySqlCommand
Dim dr As MySqlDataReader
Dim da As MySqlDataAdapter
Dim dt As New DataTable
Dim abc As String
private sub from1()
cn.ConnectionString = "Server=localhost; user id=root; password=; database = school"
cmd.Connection = cn
cn.Open()
end sub
Private Sub images()
Dim arrImage() As Byte
Dim strImage As String
Dim myMs As New IO.MemoryStream
If Not IsNothing(Me.PictureBox5.Image) Then
Me.PictureBox5.Image.Save(myMs, Me.PictureBox5.Image.RawFormat)
arrImage = myMs.GetBuffer
strImage = "1000"
Else
arrImage = Nothing
strImage = "NULL"
End If
cmd.CommandText = "INSERT INTO admision(name, photo) VALUES('" & Me.TextBox1.Text & "'," & _
strImage & ")"
If strImage = "1000" Then
cmd.Parameters.Add(strImage, MySqlDbType.Blob).Value = arrImage
End If
MsgBox("Data save successfully!")
clear()
cmd.ExecuteNonQuery()
cn.Close()
End Sub