please what's the cause of this error in visual basic - exception

System.Data.SqlClient.SqlException: 'An explicit value for the identity column in table 'table1' can only be specified when a column list is used and IDENTITY_INSERT is ON.'
when i run the code it displays the error below
this is what pops up
**this is my code**
Imports System.Data.SqlClient
Public Class ARRIVAL
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\PRINCE\Documents\real.mdf;Integrated Security=True;Connect Timeout=30"
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
disp_data()
End Sub
Private Sub insert_Click(sender As Object, e As EventArgs) Handles insert.Click
cmd = con.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert into table1 values('" & DateTimePicker1.Text & "','" & shiftsupervisorcb.SelectedItem.ToString() & "','" & teammemberscb.SelectedItem.ToString() & "','" & airlinecb.SelectedItem.ToString() & "','" & DateTimePicker2.Text & "','" & conternocb.SelectedItem.ToString() & "','" + TextBox2.Text + "','" + TextBox3.Text + "','" & linemanagercb.SelectedItem.ToString() & "','" & okimechcb.SelectedItem.ToString & "','" & okiribboncb.SelectedItem.ToString() & "','" + ocrcb.SelectedItem.ToString() & "','" & mousecb.SelectedItem.ToString() & "' ,'" & okiprinhcb.SelectedItem.ToString() & "','" & atbcb.SelectedItem.ToString() & "','" & btpcb.SelectedItem.ToString() & "','" & bgrcb.SelectedItem.ToString() & "')"
cmd.ExecuteNonQuery()
disp_data()
MessageBox.Show("record inserted successfully")
End Sub
Public Sub disp_data()
cmd = con.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from table1"
cmd.ExecuteNonQuery()
Dim dt As New DataTable()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Me.Hide()
Dim back = New BORDING
back.Show()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
End Sub
Private Sub bgrcb_SelectedIndexChanged(sender As Object, e As EventArgs) Handles bgrcb.SelectedIndexChanged
End Sub
End Class

Related

I want to insert and retrieve data under one button event in vb.net with sql database

Here i am trying to register student's detail. and at the same time i want to retrieve a unique user id from connected SQL server and that's where i got stuck!
I want to know what i am doing wrong here!
Everything stored in the SQL server is correct and i have make an auto increment code in SQL!
so why its not inserting and retrieving the data simultaneously
Imports MySql.Data.MySqlClient
Public Class Form3
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New MySqlConnection("server=localhost;user id=root;password=root;persistsecurityinfo=True;database=bcaproject")
Dim cmd As New MySqlCommand
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Or TextBox6.Text = "" Then
MessageBox.Show("Information shouldn't be empty...")
Else
con.Open()
cmd.Connection = con
cmd.CommandText = "insert into student(F_name,L_name,Email,Phoneno,Username,Password) values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "')"
cmd.ExecuteNonQuery()
MessageBox.Show("successfully Registered")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
Form2.Show()
Me.Hide()
End If
End Sub
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
Me.Hide()
End Sub
Private Sub TextBox4_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
If Asc(e.KeyChar) <> 8 Then
If Not IsNumeric(e.KeyChar) Then
tipvalidation.Show("Numeric input required", sender, 5000)
e.KeyChar = Nothing
End If
End If
End Sub
End Class

INSERT INTO in MySQL not working but no errors?

I somehow can't insert data into my MySQL database but I know there's no trouble with the query cause there is no error message and it can make it as far as the Success message box. I think the query is right for MySQL but it is not the specific one that I should use for INSERT INTO.
Here's my code:
Imports MySql.Data.MySqlClient
Public Class Register
Dim ServerString As String = "Server=localhost; UserId =root; Password = ; Database = gym;"
Dim MysqlConn As MySqlConnection = New MySqlConnection
Dim COMMAND As MySqlCommand
Dim password, pass As String
Dim member As Integer
Private Sub Register_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToParent()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MysqlConn.ConnectionString = ServerString
Dim READER As MySqlDataReader
password = TextBox2.Text
pass = TextBox3.Text
confirm(password, pass)
If TextBox1.Text = Nothing Or TextBox2.Text = Nothing Or TextBox3.Text = Nothing Or TextBox4.Text = Nothing Or TextBox5.Text = Nothing Or TextBox6.Text = Nothing Or DateTimePicker1.Text = Nothing Or RadioButton1.Checked = False And RadioButton2.Checked = False Then
MsgBox("Please Fill your Information Completely")
Else
MysqlConn.ConnectionString = ServerString
Try
MysqlConn.Open()
Dim query As String
query = "select * from gym.user where user_name ='" & TextBox1.Text & "'"
COMMAND = New MySqlCommand(query, MysqlConn)
READER = COMMAND.ExecuteReader
Dim count As Integer
While READER.Read
count = count + 1
End While
MysqlConn.Close()
If count > 0 Then
MsgBox("User Already Exists")
Else
MysqlConn.Open()
query = "INSERT INTO gym.user(user_name,user_password,user_firstname,user_lastname,user_birthday,user_contact,user_membership) VALUES ('" & TextBox1.Text & "', md5('" & TextBox2.Text & "') ,'" & TextBox4.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Value.Date & "','" & TextBox6.Text & "','" & member & "')"
COMMAND = New MySqlCommand(query, MysqlConn)
MsgBox("USER REGISTERED")
MysqlConn.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End If
End Sub
You're missing ExecuteNonQuery statement:
query = "INSERT INTO gym.user(user_name,user_password,user_firstname,user_lastname,user_birthday,user_contact,user_membership) VALUES ('" & TextBox1.Text & "', md5('" & TextBox2.Text & "') ,'" & TextBox4.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Value.Date & "','" & TextBox6.Text & "','" & member & "')"
COMMAND = New MySqlCommand(query, MysqlConn)
COMMAND.ExecuteNonQuery()

Adding listview items to sqlyog

I'm making a Sales and inventory system at my school. I'd like to save the listview items to my database which is a MySQL sqlyog ultimate.
This is my connection and my code for saving listview to database but I get the error Object variable or with block variable is not set.
Imports MySql.Data.MySqlClient
Module modconnector
Public sqlcmd As New MySqlCommand
Public sqlcon As New MySqlConnection
Public sqladapter As New MySqlDataAdapter
Public sqlreader As MySqlDataReader
Public strsql As String
Sub connect()
sqlcon.ConnectionString = ("server=localhost;user id=root;password= ;database=sais")
sqlcon.Open()
End Sub
Sub savelistview()
sqlcon.Close()
connect()
iCount = ListView1.Items.Count()
If Not ListView1.Items.Count = 0 Then
Do Until iLoop = ListView1.Items.Count
With lvitem
lvitem = ListView1.Items.Item(iLoop)
strsql = "insert into transaction(prod_code,prod_description,prod_price,order,quantity,date) values('" _
& .Items(0).Text & "','" _
& .SubItems(1).Text & "','" _
& .SubItems(2).Text & "','" _
& .SubItems(3).Text & "','" _
& .SubItems(4).Text & "','" _
& .SubItems(5).Text & "','" _
& Label26.Text & "')"
sqlcmd.CommandText = strsql
sqlcmd.Connection = sqlcon
sqladapter.SelectCommand = sqlcmd
sqlcmd.ExecuteNonQuery()
End With
lvitem = Nothing
iLoop = iLoop + 1
Loop
End If
MessageBox.Show("Record Saved!")
sqlcon.Close()
End Sub
End Module

DataGridView select multiple row, check each row

I am trying to select all rows by a button or select different rows with ctrl+click.
But even if I have selected multiple rows, it only checks the first row. Then it rereads it again instead of going to the next row.
I'm not sure either where to put the multiselect code.
Originally, you're only supposed to just click a row then check if conditions are satisfied. However it was requested that you can do multiple and check each row if condition is satisfied.
Here is my code:
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
Dim i As Integer
i = DataGridView1.CurrentRow.Index
_subjectno = DataGridView1.Item(0, i).Value
_title = DataGridView1.Item(1, i).Value
_unit = DataGridView1.Item(2, i).Value
_pre = DataGridView1.Item(3, i).Value
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.MultiSelect = True
DataGridView1.SelectAll()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim selectedItems As DataGridViewSelectedRowCollection = DataGridView1.SelectedRows
For Each selectedItem As DataGridViewRow In selectedItems
add()
Next
End Sub
Sub add()
Try
If IS_EMPTY(txtSno) = True Then Return
cm = New MySqlCommand("select * from tblenroll where subjectnumber like '" & _subjectno & "' and remarks <> 'Failed' and studentno like '" & txtSno.Text & "'", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
MsgBox("Subject is already taken.", vbExclamation)
dr.Close()
Return
Else
dr.Close()
End If
If _pre = "NONE" Or _pre = "2ND YR STANDING" Or _pre = "3RD YR STANDING" Or _pre = "4TH YR STANDING" Or _pre = "5TH YR STANDING" Then
cm = New MySqlCommand("insert into tblenroll (studentno, subjectnumber, ay,semester,dateenrolled, curriculum) values ('" & txtSno.Text & "','" & _subjectno & "','" & txtAY.Text & "','" & txtSem.Text & "','" & Now & "','" & txtCurriculum.Text & "')", cn)
cm.ExecuteNonQuery()
MsgBox(_subjectno & " successfully added.", vbInformation)
LoadEnrolled()
Else
cm = New MySqlCommand("select * from tblenroll where studentno like '" & txtSno.Text & "' and subjectnumber like '" & _subjectno & "' and ay like '" & txtAY.Text & "'", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
MsgBox("Subject is already taken.", vbExclamation)
dr.Close()
Return
Else
dr.Close()
End If
Dim strok As Boolean = False
Dim strArr() As String
Dim count As Integer
Dim strpre As String = _pre
strArr = strpre.Split(", ")
For count = 0 To strArr.Length - 1
cm = New MySqlCommand("select * from tblenroll as e inner join tblsubject as s on e.subjectnumber = s.subjectno where s.subjectno like '%" & Trim(strArr(count)) & "%' and studentno like '" & txtSno.Text & "' and remarks like 'Passed'", cn)
dr = cm.ExecuteReader
dr.Read()
If dr.HasRows Then
dr.Close()
strok = True
Else
MsgBox("Unable to enroll this subject. Pre-requisite " & strArr(count) & ".", vbExclamation)
dr.Close()
Return
End If
Next
If strok = True Then
cm = New MySqlCommand("insert into tblenroll (studentno, subjectnumber, ay, semester, dateenrolled, curriculum) values ('" & txtSno.Text & "','" & _subjectno & "','" & txtAY.Text & "','" & txtSem.Text & "','" & Now & "','" & txtCurriculum.Text & "')", cn)
cm.ExecuteNonQuery()
MsgBox(_subjectno & " successfully added.", vbInformation)
LoadEnrolled()
End If
End If
Catch ex As Exception
MsgBox(ex.Message, vbCritical)
End Try
End Sub
Thank you so much!
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
For Each selectedItem As DataGridViewRow In DataGridView1.SelectedRows
_subjectno = selectedItem.Cells(0).Value
_title = selectedItem.Cells(1).Value
_unit = selectedItem.Cells(2).Value
_pre = selectedItem.Cells(3).Value
add()
Next
End Sub

Object reference not set to an instance of an object - datagridview

When I try add data to my datagrid which should update my database on xampp, I get the error above at: myadapter.SelectCommand.CommandText = sqlquery
I think it is something to do with null values, but I can't seem to fix it.
Here is my form code:
Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports System.Drawing.Printing
Imports System
Imports System.Windows.Forms
Public Class frmClientDetails
Dim form_type As Form
Dim user_table As String
Dim objconnection As New MySqlConnection("Server=localhost;database=ba-solutions;user id=root;password=")
Dim sqlstring As String
Dim reader As MySqlDataReader
Dim objcommand As New MySqlCommand
Dim myadapter As New MySqlDataAdapter
Dim dataset As DataSet
Private Sub frmClientDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DGVClient.Columns.Clear()
Dim dt As New DataTable
objdataadapter.SelectCommand = New MySqlCommand()
objdataadapter.SelectCommand.Connection = objconnection
objdataadapter.SelectCommand.CommandType = CommandType.Text
objdataadapter.SelectCommand.CommandText = "SELECT * FROM Client_Details"
objdataadapter.Fill(dt)
rowposition = 0
DGVClient.DataSource = dt
End Sub
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
frmMainMenu.Show()
Me.Hide()
End Sub
Private Sub btnClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
txtCompanyName.Clear()
cbxCompanyType.Items.Clear()
txtVAT.Clear()
txtPAYE.Clear()
txtAddressLine.Clear()
txtCity.Clear()
txtPostcode.Clear()
txtEmail.Clear()
txtPhoneNumber.Clear()
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
objconnection.Open()
Dim sqlquery As String
sqlquery = "Insert into Client_Details (CompanyName , CompanyType , VATRegistrationNumber , PAYEandTaxReference , AddressLine1 , City , Postcode , Email , PhoneNumber) Values ('" &
txtCompanyName.Text & "','" & cbxCompanyType.Text & "' , '" & txtVAT.Text & "','" & txtPAYE.Text & "' , '" & txtAddressLine.Text & "' , '" & txtCity.Text & "' , '" & txtPostcode.Text & "' , '" &
txtEmail.Text & "' , '" & txtPhoneNumber.Text & "')"
myadapter.SelectCommand.CommandText = sqlquery
myadapter.SelectCommand.CommandType = CommandType.Text
Try
dataset = New DataSet
myadapter.Fill(dataset, "client_details")
MsgBox("info added")
Catch
MsgBox("not added")
End Try
End Sub
End Class
possible duplicate at Your question befor
Also you didn't set connection to your adapter :
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim sqlquery As String
sqlquery = "Insert into Client_Details (CompanyName , CompanyType , VATRegistrationNumber , PAYEandTaxReference , AddressLine1 , City , Postcode , Email , PhoneNumber) Values ('" &
txtCompanyName.Text & "','" & cbxCompanyType.Text & "' , '" & txtVAT.Text & "','" & txtPAYE.Text & "' , '" & txtAddressLine.Text & "' , '" & txtCity.Text & "' , '" & txtPostcode.Text & "' , '" &
txtEmail.Text & "' , '" & txtPhoneNumber.Text & "')"
Try
If objconnection.State = ConnectionState.Open Then
objconnection.Close()
End If
objconnection.Open()
Dim objcommand As New MySqlCommand(sqlquery,objconnection)
objcommand.ExecuteNonQuery()
MsgBox("info added")
Catch
MsgBox("not added")
End Try
End Sub
End Class