A generic error occurred in GDI+ when update - vb.net

When I update a form I have this error only when I don't update(change) the image.
When I update with a new image I don't have this error.
Try
Dim mstream As New System.IO.MemoryStream()
PictureBox1.Image = New Bitmap(Image.FromStream(mstream))
Dim arrImage() As Byte = mstream.GetBuffer()
mstream.Close()
MysqlConn.Close()
MysqlConn.Open()
COMMAND.Connection = MysqlConn
COMMAND.CommandText = "update product set
id_maker=#Value1,
foto=#foto
where id = '" & TextBox1.Text & "'"
COMMAND.Parameters.AddWithValue("#Value1", If(String.IsNullOrEmpty(ComboBox1.Text), DBNull.Value, ComboBox1.Text))
COMMAND.Parameters.AddWithValue("#foto", arrImage)
READER = COMMAND.ExecuteReader
MessageBox.Show("Datos Guardados")
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
MysqlConn.Close()

I Try this and work.
Dim mstream As New System.IO.MemoryStream()
'PictureBox1.Image = New Bitmap(Image.FromStream(mstream))
PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()

Related

How to add Header Columns to PDF Sql, Vb.net

How to add Header Columns('ID, Name, Class, Date'. When I'm exporting to pdf from database pdf documnet not showing header columns how to add header columns to pdf. Thank you...
Dim table As New PdfPTable(5)
table.TotalWidth = 516.0F
table.LockedWidth = False
Dim widths As Single() = New Single() {1.0F, 2.0F, 3.0F, 4.0F, 5.0F}
table.SetWidths(widths)
table.HorizontalAlignment = 0
table.SpacingBefore = 20.0F
table.SpacingAfter = 20.0F
Dim connect As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\nagaraju\Documents\Attendance.mdf;Integrated Security=True;Connect Timeout=30"
Using conn As New SqlConnection(connect)
Dim pdfDoc As New Document()
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream("C:\Users\nagaraju\Documents\pdf\" & TextBox1.Text & ".pdf", FileMode.Create))
pdfDoc.Open()
Dim img As Image = Image.GetInstance(My.Resources.Header, System.Drawing.Imaging.ImageFormat.Jpeg)
pdfDoc.Add(img)
img.ScalePercent(50.0F)
img.ScaleToFit(250.0F, 250.0F)
Dim query As String = "SELECT ID,Name,Class,Date,Intime FROM stuattrecordAMPM where ID=#ID and Date between #Date1-1 and #Date2"
Dim cmd As New SqlCommand(query, conn)
cmd.Parameters.Add("#ID", SqlDbType.Int)
cmd.Parameters("#ID").Value = TextBox1.Text
cmd.Parameters.Add("#Date1", SqlDbType.DateTime).Value = DateTimePicker1.Value
cmd.Parameters.Add("#Date2", SqlDbType.DateTime).Value = DateTimePicker2.Value
Try
conn.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
table.AddCell(rdr(0).ToString())
table.AddCell(rdr(1).ToString())
table.AddCell(rdr(2).ToString())
table.AddCell(rdr(3).ToString())
table.AddCell(rdr(4).ToString())
End While
End Using
Catch ex As Exception
End Try
pdfDoc.Add(table)
pdfDoc.Close()
End Using

sqlite read pdf file from database

I'm a little stuck with this...
I have a code to insert a PDF file into a blob as bytes:
Dim ofd As New OpenFileDialog
With ofd
.InitialDirectory = Application.StartupPath
.Filter = "PDF Files|*.pdf"
.FileName = Nothing
.ShowDialog()
End With
MsgBox(ofd.FileName)
Dim filePath As String = ofd.FileName
Dim filename As String = Path.GetFileName(filePath)
Dim fs As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
br.Close()
fs.Close()
Dim con As New SQLiteConnection(db)
Dim cmd As New SQLiteCommand
Try
con.Open()
cmd = con.CreateCommand
cmd.CommandText = "UPDATE Employee SET File = #File WHERE ID = 20"
cmd.Parameters.Add("#File", SqlDbType.Binary).Value = bytes
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
cmd.Dispose()
End Try
This works, but now I need to reverse it and load the PDF file (I don't want to save the PDF back into the computer, all I need is to view it...)
This is what I have to retrieve the PDF file:
Dim con As New SQLiteConnection(db)
Dim cmd As New SQLiteCommand
Dim File As New DataTable
Try
con.Open()
cmd = con.CreateCommand
Dim CommandText As String = "SELECT File FROM Employee WHERE ID = 20"
Dim adapter As New SQLiteDataAdapter(CommandText, con)
adapter.Fill(File)
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
cmd.Dispose()
End Try
But it doesn't work...
Anyone have another way to do it?
So this is how I fixed it...
Try
con.Open()
cmd = con.CreateCommand
cmd.CommandText = "SELECT File FROM Employee WHERE ID = 20"
Dim bytes As Byte() = cmd.ExecuteScalar()
Dim fs As FileStream = New FileStream("d:\test.pdf", FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
cmd.Dispose()
End Try
Thanks for the help

odbc reader for csv in vb.net

I'm writing a program that should handle million of datasets in csv in a short time, my idea was to use odbc because of performance reasons, therefore i read all the data with odbc and save it in memory, thereafter i add parameters and insert it in sql db, here is my code so far:
Using connection As New OdbcConnection("jdbc:odbc:Driver={Microsoft Text Driver (*.txt; *.csv)};" & filePath & "Extensions=csv;Persist Security Info=False;")
Dim reader As OdbcDataReader
Dim i As Integer
Dim r As SeekZeilen
Dim TextFileTable As DataTable = Nothing
Dim line As String = reader.Read()
Me.ParseString(line)
Dim memStream As New MemoryStream(Encoding.Default.GetBytes(line))
Using TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(memStream)
TextFileReader.TextFieldType = FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(";")
r.erste_Zeile = TextFileReader.ReadFields()
If TextFileTable Is Nothing Then
TextFileTable = New DataTable("TextFileTable")
For i = 0 To r.erste_Zeile.Length - 1
Dim Column As New DataColumn(r.erste_Zeile(i))
Column.ReadOnly = True
TextFileTable.Columns.Add(Column)
Next
End If
DataGridView1.DataSource = TextFileTable
End Using
While reader.HasRows
line = reader.Read()
Me.ParseString(line)
memStream = New MemoryStream(Encoding.Default.GetBytes(line))
Using TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(memStream)
TextFileReader.TextFieldType = FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(";")
DataGridView1.DataSource = TextFileTable
Try
r._Rest = TextFileReader.ReadFields()
ReplaceChars(r._Rest)
If Not r._Rest Is Nothing Then
Dim oSQL As New DBUmgebung.cdb.SQL()
oSQL.init()
AddParameters(oSQL, r)
oSQL.ausfuehrenSQL(DBUmgebung.cdb.KSQLCommand.INSERT, _table, "")
Dim dtRow As DataRow = TextFileTable.NewRow
For i = 0 To r._Rest.Length - 1
dtRow(i) = r._Rest(i).ToString()
Next
TextFileTable.Rows.Add(dtRow)
DataGridView1.Refresh()
Application.DoEvents()
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Error! " & ex.Message & _
"")
Catch sqlEx As SqlException
MessageBox.Show(sqlEx.Message)
rtbSql.Focus()
Exit For
Catch ex As Exception
MessageBox.Show(ex.Message)
rtbSql.Focus()
Exit For
End Try
End Using
End While
reader.Close()
End Using
the problem is that i get null pointer exception for a unknown reason, does anyone have idea what i did wrong? is it probably because my odbc reader is not properly initialized?
Try this. This will read the csv file as all text into a datatable. Once in the Datatable you could then insert the records into SQL. You can always adjust this to handle multiple csv files.
Friend Shared Function GetExcelFile(ByVal strFileName As String, ByVal strPath As String) As DataTable
Try
Dim dt As New DataTable
Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=""Text;HDR=Yes;FMT=Delimited\"""
Dim conn As New OleDb.OleDbConnection(ConStr)
Dim da As New OleDb.OleDbDataAdapter("Select * from " & strFileName, conn)
da.Fill(dt)
Return dt
Catch ex As Exception
Return Nothing
End Try
End Function

specified cast is not available

can someone help me in this code please
is says specified cast is not available
MysqlConn = New SqlConnection
MysqlConn.ConnectionString =
"Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
Dim READER As SqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "SELECT [firstname] FROM md_1103763.dbo.customer where firstname='" & ComboBox1.Text & "'"
COMMAND = New SqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
TextBox1.Text = READER.GetInt32("customerID")
TextBox2.Text = READER.GetString("firstname")
TextBox3.Text = READER.GetString("surname")
TextBox4.Text = READER.GetString("contactnumber")
TextBox5.Text = READER.GetString("emailaddress")
End While
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
MysqlConn = New SqlConnection
MysqlConn.ConnectionString =
"Data Source=SABAHALI-SHEIKH;Initial Catalog=md_1103763;Integrated Security=True"
Dim READER As SqlDataReader
Try
MysqlConn.Open()
Dim Query As String
Query = "SELECT [customerID], [firstname], [surname], [contactnumber], [emailaddress] FROM md_1103763.dbo.customer where firstname='" & ComboBox1.Text & "'"
COMMAND = New SqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
TextBox1.Text = READER.GetInt32(READER.GetOrdinal("customerID"))
TextBox2.Text = READER.GetString(READER.GetOrdinal("firstname"))
TextBox3.Text = READER.GetString(READER.GetOrdinal("surname"))
TextBox4.Text = READER.GetString(READER.GetOrdinal("contactnumber"))
TextBox5.Text = READER.GetString(READER.GetOrdinal("emailaddress"))
End While
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
you Must Select all columns in table
and
Try
MysqlConn.Open()
Dim Query As String = Nothing
Query = "SELECT * FROM md_1103763.dbo.customer where firstname='" + ComboBox1.Text & "'"
COMMAND() = New SqlCommand(Query, MysqlConn)
READER = COMMAND().ExecuteReader
While READER.Read
TextBox1.Text = READER("customerID").ToString()
TextBox2.Text = READER("firstname").ToString()
TextBox3.Text = READER("surname").ToString()
TextBox4.Text = READER("contactnumber").ToString()
TextBox5.Text = READER("emailaddress").ToString()
End While
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try

How do I Iterate Through DataTable and Decrypt a field?

Good-day,
I need help with iterating through a DataTable (dbTable) and decrypting a particular field (ccNumber) before assigning its items to a Listview control (ListViewRecords).
I've already used the decryption code below elsewhere in my project on a Textbox with success, but can't figure out how to do it with the DataTable. Your assistance would be greatly appreciated, thanks.
HERE'S THE DECRYPTION CODE:
Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider
Dim Hash As New System.Security.Cryptography.MD5CryptoServiceProvider
Try
DES.Key = Hash.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(My.Settings.Key))
DES.Mode = System.Security.Cryptography.CipherMode.ECB
Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = DES.CreateDecryptor
Dim Buffer As Byte() = Convert.FromBase64String(TextBoxCard.Text)
TextBoxCard.Text = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
MessageBox.Show("The following error(s) have occurred: " & ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
HERE'S THE CODE TO QUERY THE DB AND FILL THE LISTVIEW:
Private Sub loadRecords()
'FOR MySQL DATABASE USE
Dim dbConn As New MySqlConnection
Dim dbTable As New DataTable
Dim dbQuery As String = ""
Dim dbCmd As New MySqlCommand
Dim dbAdapter As New MySqlDataAdapter
dbTable.Clear()
Try
If dbConn.State = ConnectionState.Closed Then
dbConn.ConnectionString = String.Format("Server={0};Port={1};Uid={2};Password={3};Database=accounting", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password)
dbConn.Open()
End If
dbQuery = "SELECT *" & _
"FROM cc_master INNER JOIN customer ON customer.accountNumber = cc_master.customer_accountNumber " & _
"ORDER BY nameCOMPANY ASC"
With dbCmd
.CommandText = dbQuery
.Connection = dbConn
End With
With dbAdapter
.SelectCommand = dbCmd
.Fill(dbTable)
End With
ListViewRecords.Items.Clear()
For i = 0 To dbTable.Rows.Count - 1
With ListViewRecords
.Items.Add(dbTable.Rows(i)("ccID"))
With .Items(.Items.Count - 1).SubItems
.Add(dbTable.Rows(i)("nameCOMPANY"))
.Add(dbTable.Rows(i)("ccNumber"))
.Add(dbTable.Rows(i)("ccExpireMonth"))
.Add(dbTable.Rows(i)("ccExpireYear"))
.Add(dbTable.Rows(i)("ccType"))
.Add(dbTable.Rows(i)("ccAuthorizedUseStart"))
.Add(dbTable.Rows(i)("ccAuthorizedUseEnd"))
.Add(dbTable.Rows(i)("ccLocation"))
.Add(dbTable.Rows(i)("cardholderSalutation"))
.Add(dbTable.Rows(i)("cardholderLastname"))
.Add(dbTable.Rows(i)("cardholderFirstname"))
.Add(dbTable.Rows(i)("ccZipcode"))
End With
End With
Next
Catch ex As MySqlException
MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
End Try
dbConn.Close()
End Sub
Put the decryption thing in a function:
Function Decrypt(ByVal ToDecrypt) as String
Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider
Dim Hash As New System.Security.Cryptography.MD5CryptoServiceProvider
Try
DES.Key = Hash.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(My.Settings.Key))
DES.Mode = System.Security.Cryptography.CipherMode.ECB
Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = DES.CreateDecryptor
Dim Buffer As Byte() = Convert.FromBase64String(ToDecrypt)
return System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Catch ex As Exception
return "Whatever failed message you want"
End Try
End Function
Then loop through your table like this and change the value:
for i as Integer = 0 to dbTable.Rows.Count - 1
dbTable.Rows(i)("ccNumber")) = Decrypt(dbTable.Rows(i)("ccNumber"))
This should work if I'm not totally off.