I have code to save the information in mydatatable into the database and also into my Audit file I am using the code below. however the first part of the code works great but the second part is not doing the job .Why is it that the same code is not working for the other database.it is not inserting the datatable into the Audit file
'**************************************************************************************************************************************************
'********************* SAVE PRODUCT RAW MATERIAL DETAILS IN THE GRIDVIEW ******************************************************
Dim myConn As OleDbConnection = frmLogIn.Conn
Dim oledbCmdBuilder As OleDbCommandBuilder
Dim changes As DataTable
Dim sql As String = "select * from ProductRawMaterial"
Dim oledbAdapter As OleDbDataAdapter = New OleDbDataAdapter(sql, myConn)
'Try
oledbCmdBuilder = New OleDbCommandBuilder(oledbAdapter)
changes = myTable.GetChanges()
oledbCmdBuilder.ConflictOption = ConflictOption.OverwriteChanges
If changes IsNot Nothing Then
oledbAdapter.Update(myTable)
End If
'myTable.AcceptChanges()
myConn.Close()
'Catch ex As Exception
' MsgBox(ex.ToString)
'End Try
'***************************************************************************************************************************************************
'********************************************************* SAVING TO AUDIT ****************************************************************
Dim myKonn As OleDbConnection = frmLogIn.Connn
Dim oledbCmdBuilderAudit As OleDbCommandBuilder
'Dim changes As DataTable
Dim sqlAudit As String = "select * from ProductRawMaterial"
Dim oledbAdapterAudit As OleDbDataAdapter = New OleDbDataAdapter(sqlAudit, myKonn)
'Try
oledbCmdBuilderAudit = New OleDbCommandBuilder(oledbAdapterAudit)
'changes = myTable.GetChanges()
oledbCmdBuilderAudit.ConflictOption = ConflictOption.OverwriteChanges
If changes IsNot Nothing Then
oledbAdapterAudit.Update(myTable)
End If
myTable.AcceptChanges()
myKonn.Close()
'Catch ex As Exception
' MsgBox(ex.ToString)
'End Try
Related
I am trying to use OleDB and SQLBulkCopy to pull data off an excel spreadsheet, into an SQL database. When I try and run my code, I get the following exception, triggered by
"bulkCopy.WriteToServer(objDR)" (which is also described in the question title): exception data
OleDB successfully reads the excel sheet which is then populated properly in the DataGridView I created within a WinForm for debugging purposes.
Below is a snippet of the relevant code.
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\damon\Everyone\sheet1erictest.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=Yes""")
ExcelConnection.Open()
Dim sheet As String = "Sheet5$"
Dim expr As String = "SELECT * FROM [" + sheet + "]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDbDataReader
Dim SQLconn As New SqlConnection()
Dim ConnString As String = "SERVER=SqlDEV;DATABASE=Freight;Integrated Security=True"
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
bulkCopy.DestinationTableName = "dbo.test"
Try
objDR = objCmdSelect.ExecuteReader
Dim dt = New DataTable()
dt.Load(objDR)
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = dt
DataGridView1.Refresh()
bulkCopy.WriteToServer(objDR)
objDR.Close()
SQLconn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
End Sub
Your reader object is being used by the DataTable, so comment out those lines and run them separately later with a new instance:
objDR = objCmdSelect.ExecuteReader
'Dim dt = New DataTable()
'dt.Load(objDR)
'DataGridView1.AutoGenerateColumns = True
'DataGridView1.DataSource = dt
'DataGridView1.Refresh()
bulkCopy.WriteToServer(objDR)
I'm having a trouble in viewing or displaying an image from the database (mysql) to datagriview
The table in my database that I'm trying to retrieve is named as sample with fields ID = Int(10), primary, auto increment and IMG = blob
Anyone who can help me with this? It will be so much appreciated
Sub getData()
Try
Dim Sql = "Select ID, IMG from sample"
connectionOn()
Dim cmd = New MySqlCommand(Sql, ConOn)
Dim dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
DataGridView1.Rows.Clear()
While dr.Read = True
Dim mybytearray As Byte() = dr.Item("IMG")
Dim myimage As Image
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(mybytearray)
myimage = System.Drawing.Image.FromStream(ms)
DataGridView1.Rows.Add(dr(0), myimage)
End While
ConOn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Below is my code saving the image in the database. But it doesn't save anything. I want to get the image from the datagrid then save it to the database
Try
connectionSync()
Dim a, b As String
Dim Sql = "INSERT INTO SAMPLE (ID, IMG)values(#a,#b)"
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
a = Me.DataGridView1.Rows(i).Cells(0).Value.ToString()
Dim cmd As New MySqlCommand(Sql, ConSync)
Dim memorystream1 As New MemoryStream()
Dim filename As String = DataGridView1.Rows(i).Cells(1).Value
Dim bitmaps As New Bitmap(filename)
bitmaps.Save(memorystream1, Imaging.ImageFormat.Jpeg)
Dim pic() As Byte = memorystream1.GetBuffer()
cmd.Parameters.AddWithValue("#a", a)
cmd.Parameters.AddWithValue("#b", bitmaps)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
ConSync.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Since MySql BLOB Type is used to stored SqlServer IMAGE Type I think you can adapt this code using MySql classes (i.e.: MySqlDataAdapter instead of SqlDataAdapter):
Try
Me.DataGridView1.DataSource = Nothing
Dim dgvID As New DataGridViewTextBoxColumn
dgvID.DataPropertyName = "ID"
Dim dgvIMG As New DataGridViewImageColumn
dgvIMG.DataPropertyName = "IMG"
Me.DataGridView1.Columns.Add(dgvID)
Me.DataGridView1.Columns.Add(dgvIMG)
connectionOn()
Dim cmdSample As SqlCommand = ConOn.CreateCommand
cmdSample.CommandText = "SELECT ID, " _
& "IMG " _
& "FROM Sample"
Dim dtSample As New DataTable
Dim daSample As New SqlDataAdapter(cmdSample)
daSample.Fill(dtSample)
Me.DataGridView1.DataSource = dtSample
ConOn.Close()
ConOn.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
I have a SQL Server 2008 express with a database and a table and using VB 2010 express.
I am trying to read from that table with sqldatareader, but I only one row in the datagridview with the column headers, no row with data.
What am I doing wrong? (I'm a newbe).
The connection string is :
Data Source=xxxxxxxxxx\SQLEXPRESS;Initial Catalog=Masteca_Inventory;Integrated Security=True
Dim searchStr As String = ""
Dim connetionString As String
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim sqlStr As String
Public bindingSource1 As New BindingSource()
connetionString = My.Settings.SQLconnSTR
sqlStr = "SELECT * FROM Piese WHERE " & searchStr 'searchStr is OK I fill it elsewhere
sqlCnn = New SqlConnection(connetionString)
Try
sqlCnn.Open()
sqlCmd = New SqlCommand(sqlStr, sqlCnn)
Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
Using sqlReader
Dim dTable As New DataTable
dTable.Load(sqlReader)
bindingSource1.DataSource = dTable
End Using
SearchReport.DataGridView1.DataSource = bindingSource1
'SearchReport is another form
sqlReader.Close()
sqlCmd.Dispose()
sqlCnn.Close()
SearchReport.Show()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
You are not reading the data as a group (you are fetching only one result).
You will need to adjust the code to use a While sqlReader.Read;
Example;
Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
While sqlReader.Read()
Try
'Do the work needed
rowResult += sqlReader(0) 'This will contain the result script
Catch ex As Exception
'Catch exception
End Try
End While
Something like that should work (I have not tested the code but the concept is the same).
PS - I strongly suggest you adjust your script to add a Where clause and / or the columns needed (Select * is not a "good practice")
Hope this helps.
i have a program needs to update data in dbf file. but it keeps appear error 'operator/operand type mismatch'. here is sample code :
Dim con As OleDbConnection = New OleDbConnection("Provider=vfpoledb;Data Source=C:\folder\paytran.dbf;Collating Sequence=machine;")
Try
Dim strSQL As String = "UPDATE paytran.dbf SET workhr = 20 WHERE empno = 102"
Dim cmd As OleDbCommand = New OleDbCommand(strSQL, con)
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
' Using DataAdapter object fill data from database into DataSet object
myDA.Fill(myDataSet, "MyTable")
' Binding DataSet to DataGridView
DGV.DataSource = myDataSet.Tables("MyTable").DefaultView
con.Close()
con = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Select Data")
Finally
If con IsNot Nothing Then
con.Close()
End If
End Try
please help me..
Its your connection string. The connection string should only have to point to the PATH where the data files are located, then all the SQL based commands will by default be able to see any .DBF tables IN that path (or forward if subpaths exists).
Data Source=C:\folder
instead of
Data Source=C:\folder\paytran.dbf
So, now if you have 30 tables in the "C:\folder", you can now query from ALL of them as needed.
You need to explicitly open and close the DBF. Try:
Dim strSQL As String = "Use paytran in 0 shared;UPDATE paytran SET workhr = 20 WHERE empno = 102;use in select('paytran')"
Here is the error:
http://screencast.com/t/ZDVhNmJiOTgt
Please help, here is my code:
what can I do to solve this error
Try
If MessageBox.Show("Save and update database?", _
"Confirmation", MessageBoxButtons.YesNo) = _
Windows.Forms.DialogResult.Yes Then
Dim idXs As Integer
Dim dSet As New DataSet
Dim conn As New OleDb.OleDbConnection
Dim strSQL As String
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\ACCESS DATABASE\search.mdb"
conn.Open()
strSQL = "Select * From GH"
Dim da As OleDbDataAdapter
da = New OleDb.OleDbDataAdapter(strSQL, conn)
da.Fill(dSet, "GH") 'fill dataset
'code for editing records
Dim cb As New OleDbCommandBuilder(da)
idXs = Form1.idX 'retrieve index from Form1
dSet.Tables("GH").Rows(idXs).Item(0) = TextBox1.Text
dSet.Tables("GH").Rows(idXs).Item(1) = TextBox2.Text
dSet.Tables("GH").Rows(idXs).Item(2) = TextBox3.Text
dSet.Tables("GH").Rows(idXs).Item(3) = TextBox4.Text
da.Update(dSet, "GH") 'update database
conn.Close() 'close connection
reloadMyMain() 'show new changes in form1 if any
Else
DSET.RejectChanges() 'cancel delete
End If
Catch ex As Exception
MsgBox(ex.ToString) 'show exception message
End Try
you need to check the table in the DB - one of the columns is either indexed and can only contain unique values, or has some other limitation.
you enter a data into that column that it can not hold.
Try to "UPDATE" the Data Manually directly on the table and you will see what is wrong...
It seems you just don't have the file F:\ACCESS DATABASE\search.mdb.
Check the path to your access database file.
The error message that you point to reads:
'F:\ACCESS DATABASE\search.mdb' is not a valid path.
Apparently you mistyped the path to the db in the conn.ConnectString = ... line.