Insert vb.net into sql w/comboBox and datetimepicker - sql-server

what am I doing wrong??? I have 7 textbox, 1 datetimepicker and 1 combobox in my form. When I click the save button, It doesn't do nothing, just like a new button without code, not even the customer saved message or error message.
Private Sub btnSaveCust_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveCust.Click
Try
Dim sex As String
sex = ""
If cbSex.SelectedIndex >= 0 Then
sex = cbSex.Items(cbSex.SelectedValue).ToString
End If
Dim conex As New SqlConnection("Data Source=localhost;Initial Catalog=BD_;Integrated Security=True")
Dim query As String = "SELECT * FROM tableCustomer WHERE ID=#ID"
Dim cmd1 As SqlCommand = New SqlCommand(query, conex)
cmd1.Parameters.AddWithValue("#ID", txtID.Text)
cmd1.Parameters.AddWithValue("#[Today date]", txtToday.Text)
cmd1.Parameters.AddWithValue("#Name", txtName.Text)
cmd1.Parameters.AddWithValue("#Middlename", txtMiddle.Text)
cmd1.Parameters.AddWithValue("#[Last name]", txtLastName.Text)
cmd1.Parameters.AddWithValue("#Birth", DateTimePickerBirth.Value)
cmd1.Parameters.AddWithValue("#Age", txtAge.Text)
cmd1.Parameters.AddWithValue("#Sex", cbSex.SelectedValue)
cmd1.Parameters.AddWithValue("#Phone", txtPhone.Text)
cmd1.Parameters.AddWithValue("#E-mail", txtEmail.Text)
conex.Open()
Using read As SqlDataReader = cmd1.ExecuteReader()
If read.HasRows Then
MsgBox("ID '" & txtID.Text & "' already in DB. Enter another ID", MessageBoxIcon.Error)
txtID.Focus()
Else
read.Close()
Dim cmd As SqlCommand = New SqlCommand _
("Insert into [BD_].[dbo].[tableCustomer] ([ID],[Today date],[Name],[Middlename],[Last name],[Birth],[Age],[Sex],[Phone],[E-mail]) values ('" + txtID.Text + "','" + txtToday.Text + "','" + txtName.Text + "','" + txtMiddle.Text + "','" + txtLastName.Text + "','" + DateTimePickerBirth.ToString("dd MM yyyy") + "','" + txtAge.Text + "','" + sex + "','" + txtPhone.Text + "','" + txtEmail.Text + "')", conex)
conex.Open()
cmd.ExecuteNonQuery()
MsgBox("Customer '" & txtName.Text & "' saved.", MessageBoxIcon.Information)
conex.Close()
End If
End Using
Catch ex As Exception
End Try
End Sub

If you have access to the SQL profiler (in SQL Server) you can catch the SQL being sent from your app, copy, paste and run that directly in SQL Server query window and you should see any errors... Alternatively, add your data using a Stored Procedure, you'll find it is a lot easier to get working...

Related

Windows service using VB.NET doesn't work?

What my program supposed to do : My goal is to create a windows service which acts as a mediator between multiple SQL databases. In total there are 3 different tables in 3 different servers In detail, when this service runs it should oversee the data in the "Table1" and copy it to the "Table2" in periodical time(every 1 minute). But tricky part is it cannot paste duplicate records, has to check for "Table2" 's ID field and validate for not pasting the same record with the same ID.I've added a diagram for understanding purposes of what my goal is
What I've done : So far I've developed the code completely but the issue is it only copies data from one table(specifically "NOR_LABOR" according to the diagram I've attached) to "DEV_Test_Nor_Data" in the "MES_DEV" Database(con1 to con2). According to the diagram con1, con3, con4 are tables "NOR_LABOR", "SETTER_LABOR" and "wrap_labor" respectively. con2 is the destination which is "MES_DEV" DB.
Can anybody figure out why does other table's data(con3 to con2 & con4 to con2) won't copy?
My code - Data Collector
Imports System.Configuration
Imports System.Data.SqlClient
Public Class DataCollector
Dim con1, con2, con3, con4 As New SqlConnection
Dim timer1 As Timers.Timer
Dim p_oConn As New Wisys.AllSystem.ConnectionInfo
Protected Overrides Sub OnStart(ByVal args() As String)
con1 = New SqlConnection("Data Source=NORMAC-CTMS\SQLEXPRESS;Database=Normac Data;Integrated Security=true")
con1.Open()
con2 = New SqlConnection("Data Source=STLEDGSQL01;Database=MES_DEV;Integrated Security=true")
con2.Open()
con3 = New SqlConnection("Data Source=201706-SETTER1\SQLEXPRESS;Database=Edge;Integrated Security=true")
con3.Open()
con4 = New SqlConnection("Data Source=PRINTER\SQLEXPRESS;Database=Wrapper Data;Integrated Security=true")
con4.Open()
timer1 = New Timers.Timer()
timer1.Interval = 5000
AddHandler timer1.Elapsed, AddressOf OnTimedEvent
timer1.Enabled = True
FileIO.WriteLog("Service has started")
End Sub
Protected Overrides Sub OnStop()
timer1.Enabled = False
FileIO.WriteLog("Service has stopped")
con1.Close()
con2.Close()
con3.Close()
con4.Close()
End Sub
Private Sub OnTimedEvent(obj As Object, e As EventArgs)
Dim cmd1, cmd2, cmd3 As SqlCommand
'Connecting the Normac Data table
Dim da1 As SqlDataAdapter = New SqlDataAdapter("select ID, trx_date, work_order, department, work_center, operation_no, operator, total_labor_hours, feet_produced, item_no, posted, labor_feet_produced from NOR_LABOR", con1)
Dim cb1 As SqlCommandBuilder = New SqlCommandBuilder(da1)
Dim dt1 As DataTable = New DataTable()
da1.Fill(dt1)
'Connecting the Setter_Labor table
Dim da2 As SqlDataAdapter = New SqlDataAdapter("select ID, trx_date, work_order, department, work_center, operation_no, operator, total_labor_hours, labor_feet_produced, item_no, posted from Setter_Labor", con3)
Dim cb2 As SqlCommandBuilder = New SqlCommandBuilder(da2)
Dim dt2 As DataTable = New DataTable()
da2.Fill(dt2)
'Connecting the Wrap_Labor table
Dim da3 As SqlDataAdapter = New SqlDataAdapter("select ID, trx_date, work_order, Department, work_center, operation_no, operator, total_labor_hrs, job_start, job_end, qty_ordered, qty_produced, item_no, lot_no, default_bin, posted, wrapped, total_shift_hrs, check_emp, machine, operation_complete from wrap_labor", con4)
Dim cb3 As SqlCommandBuilder = New SqlCommandBuilder(da3)
Dim dt3 As DataTable = New DataTable()
da3.Fill(dt3)
Dim i, j, k As Integer
'Inserting into DEV_Test_Nor_Data table
For Each dr As DataRow In dt1.Rows
cmd1 = New SqlCommand("Insert into DEV_Test_Nor_Data values('" & dr(0) & "','" & dr(1) & "','" & dr(2) & "','" & dr(3) & "','" & dr(4) & "','" & dr(5) & "','" & dr(6) & "','" & dr(7) & "','" & dr(8) & "','" & dr(9) & "','" & dr(10) & "','" & dr(11) & "')", con2)
i = cmd1.ExecuteNonQuery()
Next
'Inserting into DEV_Test_Set_Lbr table
For Each dr As DataRow In dt2.Rows
cmd2 = New SqlCommand("Insert into DEV_Test_Set_Lbr values('" & dr(0) & "','" & dr(1) & "','" & dr(2) & "','" & dr(3) & "','" & dr(4) & "','" & dr(5) & "','" & dr(6) & "','" & dr(7) & "','" & dr(8) & "','" & dr(9) & "','" & dr(10) & "')", con2)
j = cmd2.ExecuteNonQuery()
Next
'Inserting into DEV_Test_Wrp_Lbr table
For Each dr As DataRow In dt3.Rows
cmd3 = New SqlCommand("Insert into DEV_Test_Wrp_Lbr values('" & dr(0) & "','" & dr(1) & "','" & dr(2) & "','" & dr(3) & "','" & dr(4) & "','" & dr(5) & "','" & dr(6) & "','" & dr(7) & "','" & dr(8) & "','" & dr(9) & "','" & dr(10) & "','" & dr(11) & "','" & dr(12) & "','" & dr(13) & "','" & dr(14) & "','" & dr(15) & "','" & dr(16) & "','" & dr(17) & "','" & dr(18) & "','" & dr(19) & "','" & dr(20) & "')", con2)
k = cmd3.ExecuteNonQuery()
Next
da1.Update(dt1)
cmd1.Dispose()
dt1.Dispose()
da1.Dispose()
da2.Update(dt2)
cmd2.Dispose()
dt2.Dispose()
da2.Dispose()
da3.Update(dt3)
cmd3.Dispose()
dt3.Dispose()
da3.Dispose()
End Sub
End Class
Reference2
Reference3
Reference4
See if this helps. It may be the parameterized queries are your entire problem.
Public Class DataCollector
'Question text said one minute
Private timer1 As New Timers.Timer(60000)
Protected Overrides Sub OnStart(ByVal args() As String)
AddHandler timer1.Elapsed, AddressOf OnTimedEvent
timer1.Enabled = True
FileIO.WriteLog("Service has started")
End Sub
Protected Overrides Sub OnStop()
timer1.Enabled = False
FileIO.WriteLog("Service has stopped")
End Sub
Private Sub OnTimedEvent(obj As Object, e As EventArgs)
' DEV_Test_Nor_Data Table
ProcessOneTable("DEV_Test_Nor_Data", 12,
"Data Source=NORMAC-CTMS\SQLEXPRESS;Database=Normac Data;Integrated Security=true",
"SELECT ID, trx_date, work_order, department, work_center, operation_no, operator, total_labor_hours, feet_produced, item_no, posted, labor_feet_produced FROM NOR_LABOR"
)
' DEV_Test_Set_Lbr Table
ProcessOneTable("DEV_Test_Set_Lbr", 11,
"Data Source=201706-SETTER1\SQLEXPRESS;Database=Edge;Integrated Security=true",
"SELECT ID, trx_date, work_order, department, work_center, operation_no, operator, total_labor_hours, labor_feet_produced, item_no, posted from Setter_Labor"
)
' DEV_Test_Wrp_Lbr Table
ProcessOneTable("DEV_Test_Wrp_Lbr", 21,
"Data Source=PRINTER\SQLEXPRESS;Database=Wrapper Data;Integrated Security=true",
"SELECT ID, trx_date, work_order, Department, work_center, operation_no, operator, total_labor_hrs, job_start, job_end, qty_ordered, qty_produced, item_no, lot_no, default_bin, posted, wrapped, total_shift_hrs, check_emp, machine, operation_complete from wrap_labor"
)
End Sub
Private EdgeConnStr As String = "Data Source=STLEDGSQL01;Database=MES_DEV;Integrated Security=true"
Private Sub ProcessOneTable(destTableName As String, ParameterCount As Integer, sourceConnectionString AS String, sourceSql As String)
Dim data As New DataTable()
Using sourceConn As New SqlConnection(sourceConnectionString), _
da As New SqlDataAdapter(sourceSql, sourceConn)
da.Fill(data)
End Using
Dim paramList As String = String.Join(",", Enumerable.Range(0, ParameterCount).Select(Function(p) $"#p{p}"))
' Assumes first parateter (#p0) is always the ID.
Dim sql As String = $"INSERT INTO {destTableName} SELECT {paramList} WHERE NOT EXISTS(SELECT ID FROM {destTableName} WHERE ID = #p0)"
Using cn As New SqlConnection(EdgeConnStr), _
cmd As New SqlCommand(sql, cn)
For i As Integer = 0 To ParameterCount - 1
cmd.Parameters.Add($"#p{i}", SqlDbType.VarChar)
Next i
cn.Open()
For Each dr As DataRow In data.Rows
For i As Integer = 0 to ParameterCount - 1
cmd.Parameters(i).Value = dr(i)
Next i
cmd.ExecuteNonQuery()
Next dr
End Using
End Sub
End Class
It sounds like you also need to worry about merging the data, but start with this anyway; it fixes the HUGE GAPING SECURITY ISSUE in the original code, as well as isolating the important part of the code down to the minimum possible method size. This will make it easier to refactor just that part to also worry about what IDs may already exist... but I'll let you make an attempt at that yourself first (hint: INSERT + SELECT + WHERE NOT EXISTS() all in the same query)

I want to update my data of my database in VB.net but I get Error Syntax

This is my problem:
When I click update button, I don't know how to fix this error:
My Error Message is:
"Error: syntax error in union query"
This is my code:
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
TestConnection()
Try
Dim cmd As OleDbCommand
Dim sql As String
sql = "(UPDATE tblUsers SET Username = '" & txtUserName.Text & "', Password = '" & txtUserPassword.Text &
"', Usertype = '" & cbousertype.Text & "', WHERE UserID = '" & txtUserID.Text & "');"
cmd = New OleDbCommand(sql, Conn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Error: " & ex.Message)
End Try
End Sub
Is it wrong?
Now my problem has been solved thank you very much
i changed my code to use a parameters and then it work
Now my code is :
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
TestConnection()
Dim cmd As OleDbCommand
Dim sql As String
sql = "UPDATE tblUsers SET Username=?, [Password]=?, Usertype=? where UserID=?"
cmd = New OleDbCommand(sql, Conn)
cmd.Parameters.AddWithValue("#p1", txtUserName.Text)
cmd.Parameters.AddWithValue("#p2", txtUserPassword.Text)
cmd.Parameters.AddWithValue("#p3", cbousertype.Text)
cmd.Parameters.AddWithValue("#p4", txtUserID.Text)
cmd.ExecuteNonQuery()
MsgBox("Data Has Been Updated", MsgBoxStyle.Information, "Updated")
ShowUser()
End Sub

update button codes for VB.net with sql-server

I have create a library management system. here if I want to update a book's particular record its updating all the records in the SQL-server database. how can I write code for update a particular record only. here is my code,
Private Sub btnedit_Click(sender As Object, e As EventArgs) Handles btnedit.Click
con.ConnectionString = "data source=hp-pc\sqlexpress; initial catalog=Library_DB;integrated security= true"
con.Open()
Dim comd As New SqlCommand("update Book set Book_Id='" & TextBox1.Text & "',Bk_Name='" & TextBox2.Text & "',Author_Name='" & TextBox3.Text & "', Year_of_release='" & TextBox4.Text & "',Availability_of_bks='" & TextBox5.Text & "'", con)
comd.ExecuteNonQuery()
MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Add a WHERE clause in your SQL command to specify which book will be updated..
use the ID number of the book you want to update.
and avoid concatenating in your sql command, use parameter #
Dim comd As New SqlCommand("update Book set Book_Id=#bookID, Bk_Name=#bkName, Author_Name=#author, Year_of_release=#release, Availability_of_bks=#avail WHERE Book_Id=#whereID", con)
comd.Parameters.Add("#bookID", SqlDbType.String).Value = TextBox1.Text
comd.Parameters.Add("#bkName", SqlDbType.String).Value = TextBox2.Text
comd.Parameters.Add("#author", SqlDbType.String).Value = TextBox3.Text
comd.Parameters.Add("#release", SqlDbType.String).Value = TextBox4.Text
comd.Parameters.Add("#avail", SqlDbType.String).Value = TextBox5.Text
comd.Parameters.Add("#whereID", SqlDbType.String).Value = "Book ID HERE"
comd.ExecuteNonQuery()
MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)
You need to add a WHERE clause to your SqlCommand so that SQL Server knows what record to update. Without a WHERE clause, it will update the entire table. See below:
con.ConnectionString = "data source=hp-pc\sqlexpress; initial catalog=Library_DB;integrated security= true"
con.Open()
Dim comd As New SqlCommand("update Book set Book_Id='" & TextBox1.Text & "',Bk_Name='" & TextBox2.Text & "',Author_Name='" & TextBox3.Text & "', Year_of_release='" & TextBox4.Text & "',Availability_of_bks='" & TextBox5.Text & "' WHERE Book_Id='{**Put your book id here**}'", con)
comd.ExecuteNonQuery()
MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

VB net Transferring DGV1 TO DGV2

I'm trying to Transfer DataGridView1 VALUES to another DataGridV 2..
that has a Database (MS ACCESS) But this code won't work help please
Dim MyConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; data source=..\ItemInssu.accdb")
Dim insertCommand As New OleDb.OleDbCommand("INSERT INTO tbLItemIssuance(ItemCode, DateIssued, Quantity) VALUES ('" & ItemInssuance.DataGridView2.Rows(ItemInssuance.DataGridView2.Rows.Count - 2).Cells(0).Value.ToString & "','" & ItemInssuance.DataGridView2.Rows(ItemInssuance.DataGridView2.Rows.Count - 2).Cells(1).Value.ToString & "','" & ItemInssuance.DataGridView2.Rows(ItemInssuance.DataGridView2.Rows.Count - 2).Cells(2).Value.ToString & "')", MyConnection)
Try
MyConnection.Open()
insertCommand.ExecuteNonQuery()
ItemInssuance.Show()
Catch ex As Exception
Finally
MyConnection.Close()
End Try

Additional information: Syntax error in INSERT INTO statement

I tried out to connect my database(ms-access) to Visual basic.But it came up with the following error:
A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error in INSERT INTO statement.
If there is a handler for this exception, the program may be safely continued.
I used the following code.please see if there is any error..please help me out for it..
The Code is:
Private Sub frmGive_Load(sender As Object, e As EventArgs) Handles Me.Load
con = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\AntivirUS Vandry\Documents\Visual Studio 2013\Projects\Give And Get\dbaseMain.mdb")
Dim sql As String = "Select * from tblGive"
Dim dbcmd As OleDbCommand = New OleDbCommand(sql, con)
con.Open()
Dim dbadapter As OleDbDataAdapter = New OleDbDataAdapter(sql, con)
Dim db As DataSet = New DataSet("TABLE")
dbadapter.Fill(db, "TABLE")
'create new instance of table so that row can be accessed
Dim dt As New DataTable
dt = db.Tables("TABLE")
CmbGenre.Text = dt.Rows(0)(0)
CmbLanguage.Text = dt.Rows(0)(1)
txtNMovie.Text = dt.Rows(0)(2)
txtFName.Text = dt.Rows(0)(3)
txtLname.Text = dt.Rows(0)(4)
CmbClass.Text = dt.Rows(0)(5)
txtnull.Text = dt.Rows(0)(6)
End Sub
There are some codes in between them.Including textboxes and combo boxes.
Public Sub submit()
con = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\AntivirUS Vandry\Documents\Visual Studio 2013\Projects\Give And Get\dbaseMain.mdb")
con.Open()
Dim sql As String
sql = "Insert into tblGive (Genre,Language,NMovie,FName,LName,Class,SaveDate)" + "VALUES (" & CmbGenre.Text & "','" & CmbLanguage.Text & "','" & txtNMovie.Text & "','" & txtFName.Text & "','" & txtLname.Text & "','" & CmbClass.Text & "','" & txtnull.Text & "')"
MsgBox(sql)
Dim dbcmd As OleDbCommand
dbcmd = New OleDbCommand(sql, con)
dbcmd.ExecuteNonQuery()
MsgBox("Saved")
End Sub
You are missing a single quote at the beginning of the values keyword.
In other words,
VALUES (" & CmbGenre.Text & "','" & CmbLanguage.Text &
should be
VALUES ('" & CmbGenre.Text & "','" & CmbLanguage.Text &

Resources