Debugging Error in SQL - vb.net

Hi guys: Can any one please help me out in this error? Thanks
Error 1 Operator '&' is not defined for types 'String' and 'System.Windows.Forms.TextBox'. C:\Users\kathy\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 20 20 WindowsApplication1
Imports System.Data.OleDb
Public Class Form1
Public connection As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kathy\Desktop\generalledger.accdb"
Public conn As New OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn.ConnectionString = connection
If conn.State = ConnectionState.Closed Then
conn.Open()
MsgBox("Open")
Else
MsgBox("Closed")
End If
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
Dim sqlQuery As String
sqlQuery = "insert into voucher(VoucherName, Account_Code, Amount) values ('" & TxtVoucher & "', " & TxtAmount & ", '" & TxtAccount & "')"
Dim sqlcommand As New OleDbCommand
With sqlcommand
.CommandText = sqlQuery
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox("Save")
End Sub
End Class

Just add .Text() in your textboxes.
sqlQuery = "insert into voucher(VoucherName, Account_Code, Amount) values ('" & TxtVoucher & "', " & TxtAmount & ", '" & TxtAccount & "')"
To
sqlQuery = "insert into voucher(VoucherName, Account_Code, Amount) values ('" & TxtVoucher.Text() & "', " & TxtAmount.Text() & ", '" & TxtAccount.Text() & "')"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn.ConnectionString = connection
If conn.State = ConnectionState.Closed Then
conn.Open()
MsgBox("Open")
Else
MsgBox("Closed")
End If
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
conn.Open()
Dim sqlQuery As String
sqlQuery = "insert into voucher(VoucherName, Account_Code, Amount) values ('"+ TxtVoucher + "', " + TxtAmount + ", '" + TxtAccount + "')"
Dim sqlcommand As New OleDbCommand
With sqlcommand
.CommandText = sqlQuery
.Connection = conn
.ExecuteNonQuery()
conn.Close()
End With
MsgBox("Save")
End Sub

Related

Input string was not in a correct format & how to rename rowheader datagridview

please code solution and for rename row header I mean marking the color of the image file I attached
Note : for notes I use visual studio 2010
Public Class Form1
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
dbConnection()
Dim indexID As Integer
x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
indexID = Convert.ToInt32((DataGridView1.Rows(x).Cells(0).Value.ToString()))
sql = "UPDATE ITEM SET ITM= '" & txtFname.Text & "',ITC='" & txtLname.Text & "',QOH='" & cbGender.Text & "' WHERE ID=" & indexID & ""
cmd = New OleDbCommand(sql, conn)
cmd.ExecuteNonQuery()
MessageBox.Show("Successfully Updated...", "Update")
conn.Close()
'btnReadDatabase_Click(sender, e);
retrieveALL()
End Sub
End Class
object reference not set
Thanks
roy
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
dbConnection()
sql = "UPDATE ITEM SET ITM= '" & txtFname.Text & "',ITC='" & txtLname.Text & "',QOH='" & cbGender.Text & "' WHERE ITC='" & txtLname.Text & "'"
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
MessageBox.Show("Successfully Updated...", "Update")
con.Close()
PopulateDataGridView()
End Sub

vb message display

when I click on the register button I get the test "registration successful" even if I didn't enter anything on the column.
I need to display "registration successful" if it successfully inserted to the database.
if it is a failure I need it to display "registration fail" on the same label (lblsuccessfail)
Imports System.Data.SqlClient
Imports System.IO
Partial Class register
Inherits System.Web.UI.Page
Dim cn As New SqlConnection("Data Source=localhost\MSSQL2012;Initial Catalog=UserDetails;Integrated Security=True")
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnnew.Click
cn.Open()
Try
Dim Query As String
Query = "INSERT INTO FirstProject (fullname,username,password,address,gender)VALUES('" & txtfullname.Text & "', '" & txtusername.Text & "','" & txtpassword.Text & "','" & txtaddress.Text & "','" & cbogender.Text & "')"
cmd = New SqlCommand(Query, cn)
If cmd.ExecuteNonQuery() Then
lblsuccessfail.Text = lblsuccessfail.Text & " Registertration successful "
End If
Catch ex As Exception
lblsuccessfail.Text = lblsuccessfail.Text & " Registertration unsuccessful "
End Try
cn.Close()
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnback_Click(sender As Object, e As System.EventArgs) Handles btnback.Click
Response.Redirect("login.aspx")
End Sub
End Class
Imports System.Data.SqlClient
Imports System.IO
Partial Class register
Inherits System.Web.UI.Page
Dim cn As New SqlConnection("Data Source=localhost\MSSQL2012;Initial Catalog=UserDetails;Integrated Security=True")
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnnew.Click
Try
cn.Open()
cmd = New SqlCommand("INSERT INTO FirstProject (fullname,username,password,address,gender)VALUES('" & txtfullname.Text & "', '" & txtusername.Text & "','" & txtpassword.Text & "','" & txtaddress.Text & "','" & cbogender.Text & "')", cn)
If txtfullname.Text = Nothing OrElse txtusername.Text IsNot Nothing Or txtpassword.Text.Equals(Nothing) OrElse txtaddress.Text = String.Empty Or cbogender.Text.Equals(String.Empty) Then Throw New Exception()
If cmd.ExecuteNonQuery() Then lblsuccessfail.Text = lblsuccessfail.Text & " Registertration successful "
Catch ex As Exception
lblsuccessfail.Text = lblsuccessfail.Text & " Registertration unsuccessful "
Finally
cn.Close()
End Try
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnback_Click(sender As Object, e As System.EventArgs) Handles btnback.Click
Response.Redirect("login.aspx")
End Sub
End Class

Search and save data to database

I am using vb.net with OleDb database.
Following is my code, although i am to search database but not able to save after making changes.
It displays following error at Command.ExecuteNonQuery level :
"OleDbException was unhandled, Data type mismatch in criteria expression."
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Src_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Src.Click
Dim Connection1 As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Database2.accdb;" & "Persist Security Info=False;" & "Jet OLEDB:Database Password=" & "your pass" & ";")
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Try
Connection1.Open()
cmd = New OleDbCommand("SELECT * from Table1 WHERE ID=" & IDtxt.Text & "", Connection1)
dr = cmd.ExecuteReader
If dr.Read Then
Me.IDtxt.Text = dr("ID")
Me.Hbtxt.Text = dr("Hb")
Me.TCtxt.Text = dr("TC")
Me.DCtxt.Text = dr("DC")
dr.Close()
Else
MsgBox("No Record")
End If
Catch
End Try
Connection1.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Connection1 As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Database2.accdb;")
Connection1.Open()
Dim ID : ID = Me.IDtxt.Text
Dim Hb : Hb = Me.Hbtxt.Text
Dim TC : TC = Me.TCtxt.Text
Dim DC : DC = Me.DCtxt.Text
Dim command As New OleDbCommand("UPDATE Table1 SET Hb = '" & Hb & "',TC = '" & TC & "',DC = '" & DC & "' WHERE ID = '" & ID & "'", Connection1)
Command.ExecuteNonQuery()
Connection1.Close()
End Sub
use parameter in your code..like this..
Dim command As New OleDbCommand("UPDATE Table1 SET Hb = #hb,TC = #tc,DC = #DC WHERE ID = #id", Connection1)
Command.parameters.add(new sqlparameter("#hb",Hb))
Command.parameters.add(new sqlparameter("#tc",tc))
Command.parameters.add(new sqlparameter("#dc",dc))
Command.parameters.add(new sqlparameter("#id",id))
Command.ExecuteNonQuery()

Data shown as being inserted when it's not in Visual Studio 2010 and MS Access

I have created a database in MS Access 2007 and a front end in Visual Studio 2010. The code is as follows:
Imports System.Data.OleDb
Public Class Form3
Public connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Course_Data.accdb;persist security info=false"
Dim cn As New OleDbConnection
'Dim cn As New OleDbConnection(My.Settings.Course_DataConnectionString)
Private Sub Label14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label14.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim sqlquery As String = "insert into Participant_info values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & TextBox11.Text & "','" & TextBox12.Text & "','" & TextBox13.Text & "','" & TextBox14.Text & "','" & TextBox15.Text & "', '" & TextBox16.Text & "')"
Dim cmd As New OleDbCommand
With cmd
.CommandText = sqlquery
.Connection = cn
.ExecuteNonQuery()
End With
MsgBox("Details Inserted")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.ConnectionString = connstring
If cn.State = ConnectionState.Closed Then
MsgBox("open")
Else
MsgBox("close")
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
cn.ConnectionString = connstring
cn.Open()
TextBox17.Text = ("Connected")
End Sub
End Class
It shows a message as data being inserted, but the data is not there in the database.
Sometimes Data Source=|DataDirectory|\... is problematic when debugging. Please bear on mind that you'll have another database in \bin\debug at your project folder when you are debugging your code. Probably you're updating the records in this database instead the original one.
Try to set an absolute path and check if the records are being updated.
Also, your code is vulnerable to SQL Injection. You should build your OleDbCommand as follows:
Dim cmd As New OleDbCommand("insert into Participant_info values('?','?','?',······)", cn)
cmd.Parameters.Add(TextBox1.Text)
cmd.Parameters.Add(TextBox2.Text)
cmd.Parameters.Add(TextBox3.Text)
...
...
cmd.ExecuteNonQuery()
cn.close()

.net: how to evaluate this error

I am new to vb.net and I am trying to create a login form but on running this code i get error msg("ExecuteReader: Connection property has not been initialized.")
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim con As New System.Data.OleDb.OleDbConnection
Dim cmd As System.Data.OleDb.OleDbCommand
Dim da As System.Data.OleDb.OleDbDataAdapter
Dim sdr As System.Data.OleDb.OleDbDataReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dbprovider As String = "Provider=Microsoft.jet.OleDb.4.0;"
Dim dbsource As String = "Data Source=C:\Users\RAJKRI\Documents\Visual Studio 2008\Projects\Banking System.mdb"
con.ConnectionString = dbprovider & dbsource
con.Open()
cmd.Connection = con
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\RAJKRI\Documents\Visual Studio 2008\Projects\Banking System.accdb")
cmd = New OleDb.OleDbCommand("SELECT * FROM Login WHERE Empid = '" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "' , con")
sdr = cmd.ExecuteReader()
If (sdr.Read() = True) Then
Form2.Show()
TextBox1.Text = ""
TextBox2.Text = ""
Else
MessageBox.Show("Invalid Employee Id/Password")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class
You are appending your connection object to your query as text. Change
cmd = New OleDb.OleDbCommand("SELECT * FROM Login WHERE Empid = '" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "' , con")
to
cmd = New OleDb.OleDbCommand("SELECT * FROM Login WHERE Empid = '" & TextBox1.Text & "' AND password = '" & TextBox2.Text & "'" , con)
(the difference is at the end)