Saturday, August 23, 2014

DataReader

            conn = New OdbcConnection("dsn=poscafe;uid=root;")
            sqlStr = "select * from table_answer"
            conn.Open()
            comm = New OdbcCommand(sqlStr, conn)
            dr = comm.ExecuteReader
            comm.Dispose()
            If dr.Read Then
                MsgBox(dr("answer_id"))
            End If

vb.net (md5hash)

 Function getMD5Hash(ByVal strToHash As String) As String
        Dim md5Obj As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
        bytesToHash = md5Obj.ComputeHash(bytesToHash)
        Dim strResult As String = ""
        For Each b As Byte In bytesToHash
            strResult += b.ToString("x2")
        Next
        Return strResult
    End Function

vb.net (related database)

Imports System.Data.Odbc
Imports System.Data.DataRow
Imports System.Windows.Forms
Public Class Form1
    Public conn As OdbcConnection
    Public comm As OdbcCommand
    Public sqlStr As String
    Dim ods As New DataSet
    Dim odt As New DataTable
    Dim dr As OdbcDataReader
    Dim num As Integer = 0
    Dim cell As DataGridViewCell
    Public ans_id As Integer
    Private Sub connect1()
        conn = New OdbcConnection("dsn=poscafe;uid=root;")
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Me.WindowState = FormWindowState.Maximized
        Me.dgView.DefaultCellStyle.Font = New Font("Tahoma", 15)

        dgView.ColumnHeadersVisible = True

        ' Set the column header style.
        Dim columnHeaderStyle As New DataGridViewCellStyle()

        columnHeaderStyle.BackColor = Color.Beige
        columnHeaderStyle.Font = New Font("Tahoma", 15, FontStyle.Bold)
        dgView.ColumnHeadersDefaultCellStyle = columnHeaderStyle

        Try
            connect1()
            'conn = New OdbcConnection("dsn=poscafe;uid=root;")
            sqlStr = "select * from table_answer"
            conn.Open()
            comm = New OdbcCommand(sqlStr, conn)
            'dr = comm.ExecuteReader
            'If dr.Read Then
            '    MsgBox(dr("answer_id"))
            'End If
            Dim myDA As OdbcDataAdapter = New OdbcDataAdapter(comm)
            myDA.Fill(ods, "table_answer")
            odt = ods.Tables("table_answer")
            dgView.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            dgView.AllowUserToAddRows = False
            dgView.DataSource = odt
            Dim column As DataGridViewColumn = dgView.Columns(1)
            column.Width = 200
            dgView.Columns(0).HeaderText = "Code"
            dgView.Columns(1).HeaderText = "Question ID"
            dgView.ReadOnly = True
            conn.Close()
            ans_id = odt.Rows(num).Item(0)
            id.Text = ans_id
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Buttonnext_Click(sender As Object, e As EventArgs) Handles Buttonnext.Click
        dgView.Rows(num).Selected = False
        num += 1
        If num > (odt.Rows.Count - 1) Then
            num -= 1
            dgView.Rows(num).Selected = True
            Exit Sub
        End If
        dgView.Rows(num).Selected = True
        id.Text = odt.Rows(num).Item("answer_id").ToString
        ans_id = odt.Rows(num).Item(0)
        dgView.Columns(0).HeaderText = "Code"
    End Sub

    Private Sub Buttonprevious_Click(sender As Object, e As EventArgs) Handles Buttonprevious.Click
        dgView.Rows(num).Selected = False
        num -= 1
        If num < 0 Then
            num += 1
            dgView.Rows(num).Selected = True
            Exit Sub
        End If
        'TextBox1.Text = odt.Rows(num).Item("answer_id").ToString
        dgView.Rows(num).Selected = True
        id.Text = odt.Rows(num).Item("answer_id").ToString
        ans_id = odt.Rows(num).Item(0)
    End Sub

    Private Sub buttonsave_Click(sender As Object, e As EventArgs) Handles buttonsave.Click
        Try
            'comm.Connection = conn
            'connect1()
            conn.Open()
            Dim insertSQL As String = "INSERT INTO table_answer(question_id,answer,image) VALUES (?,?,?)"
            comm = New OdbcCommand(insertSQL, conn)
            comm.Parameters.AddWithValue("question_id", Textquestionid.Text)
            comm.Parameters.AddWithValue("answer", Textanswer.Text)
            comm.Parameters.AddWithValue("image", Textimage.Text)
            comm.ExecuteNonQuery()
            comm.Dispose()
            comm = Nothing
            conn.Close()
            odt.Clear()
            num += 1
            Call Alldata()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Alldata()
        Call connect1()
        sqlStr = "select * from table_answer"
        conn.Open()
        comm = New OdbcCommand(sqlStr, conn)
        'dr = comm.ExecuteReader
        'If dr.Read Then
        '    MsgBox(dr("answer_id"))
        'End If
        Dim myDA As OdbcDataAdapter = New OdbcDataAdapter(comm)
        myDA.Fill(ods, "table_answer")
        odt = ods.Tables("table_answer")
        dgView.DataSource = odt
        conn.Close()
        dgView.Rows(0).Selected = False
        dgView.Rows(num).Selected = True
        id.Text = odt.Rows(num).Item(0)
    End Sub

    Private Sub dgView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgView.CellClick
        'MsgBox(dgView.CurrentRow.Index)
        'MsgBox(dgView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
        'If IsNumeric(dgView.Rows(e.RowIndex).Cells(0).Value) Then
        'MsgBox(dgView.Rows(e.RowIndex).Cells(0).Value)
        Me.id.Text = dgView.Rows(e.RowIndex).Cells(0).Value
        dgView.Rows(num).Selected = False
        num = dgView.CurrentRow.Index
        ans_id = odt.Rows(num).Item(0)
        'End If
    End Sub

    Private Sub dgView_KeyUp(sender As Object, e As KeyEventArgs) Handles dgView.KeyUp
        'If (dgView.Rows.Count - 1) = dgView.CurrentRow.Index Then
        'MsgBox("no value in this row")
        'Else
        Select Case e.KeyCode
            Case Keys.Up
                'MsgBox(dgView.SelectedRows(0).Cells(0).Value)
                Me.id.Text = dgView.SelectedRows(0).Cells(0).Value
                ans_id = dgView.SelectedRows(0).Cells(0).Value
                num = dgView.CurrentRow.Index
            Case Keys.Down
                'MsgBox(dgView.SelectedRows(0).Cells(0).Value)
                Me.id.Text = dgView.SelectedRows(0).Cells(0).Value
                ans_id = dgView.SelectedRows(0).Cells(0).Value
                 num = dgView.CurrentRow.Index
        End Select
        'End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form2.Show()
    End Sub

    Private Sub update_Click(sender As Object, e As EventArgs) Handles update.Click
        Try
            conn.Open()
            Dim updateSQL As String = "update table_answer set question_id=?,answer=?,image=? where answer_id=?"
            comm = New OdbcCommand(updateSQL, conn)
            comm.Parameters.AddWithValue("question_id", Textquestionid.Text)
            comm.Parameters.AddWithValue("answer", Textanswer.Text)
            comm.Parameters.AddWithValue("image", Textimage.Text)
            comm.Parameters.AddWithValue("answer_id", id.Text)
            comm.ExecuteNonQuery()
            odt.Clear()
            Call Alldata()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        'comm.Connection = conn
        'connect1()
    End Sub
    Private Sub delete_Click(sender As Object, e As EventArgs) Handles delete.Click
        Dim rowaff As Integer
        Try
            conn.Open()
            Dim deleteSQL As String = "delete from table_answer where answer_id=?"
            comm = New OdbcCommand(deleteSQL, conn)
            comm.Parameters.AddWithValue("answer_id", id.Text)
            rowaff = comm.ExecuteNonQuery()
            If rowaff = 1 And num <> 0 Then
                num -= 1
            End If
            odt.Clear()
            Call Alldata()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

vb.net onpain(source, entire clone from source and path of clone from source)

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports System.IO
Public Class Form1
    Overloads Function CopyBitmap(source As Bitmap) As Bitmap
        Return New Bitmap(source)
    End Function
    Overloads Function CopyBitmap(source As Bitmap, part As Rectangle) As Bitmap
        Dim bmp As New Bitmap(part.Width, part.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.DrawImage(source, 0, 0, part, GraphicsUnit.Pixel)
        g.Dispose()
        Return bmp
    End Function
    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Dim arialFont As Font
        Dim blackBrush As Brush
        arialFont = New Font("Arial", 10, FontStyle.Regular)
        blackBrush = New SolidBrush(Color.Black)
        Dim sidesize As Integer = 300
        Dim third As Integer = CInt(sidesize / 3)
        Dim bm_sourceaa As New Bitmap(Bitmap.FromFile("C:\Users\Laptop\Desktop\troll\cute girl2.jpg"))
        Dim bm_source As New Bitmap(Bitmap.FromFile("C:\Users\Laptop\Desktop\troll\cute girl2.jpg"), _
            CInt(200), bm_sourceaa.Height * 200 / bm_sourceaa.Width)
        ' Copy entirely as a clone.
        Dim clone As Bitmap = CopyBitmap(bm_source)
        ' Copy the center part of the bitmap.
        Dim center As Bitmap =  CopyBitmap(bm_source, New Rectangle(0, 100, 100, 50))
        Dim DestPath As String = "C:\Users\Laptop\Desktop\rathana\"
        If Not Directory.Exists(DestPath) Then
            Directory.CreateDirectory(DestPath)
        End If
        clone.Save("C:\Users\Laptop\Desktop\rathana\2newbitmap.bmp", ImageFormat.Bmp)
        Dim y As Integer = 10
        e.Graphics.DrawString("source bitmap:", arialFont, blackBrush, 10, y)
        y += 20
        e.Graphics.DrawImage(bm_source, 10, y)
        y += bm_source.Height + 10
        e.Graphics.DrawString("clone bitmap:", arialFont, blackBrush, 10, y)
        y += 20
        e.Graphics.DrawImage(clone, 10, y)
        y += clone.Height + 10
        e.Graphics.DrawString("center part of bitmap:", arialFont, blackBrush, 10, y)
        y += 20
        e.Graphics.DrawImage(center, 10, y)
        y += center.Height + 10
        'Dispose graphic objects.
        arialFont.Dispose()
        blackBrush.Dispose()
    End Sub
End Class

vb.net (Add)

1. Browse to show image on form 

Private Sub Buttonaddimage_Click(sender As Object, e As EventArgs) Handles Buttonaddimage.Click
        Dim ofd As New OpenFileDialog
        If ofd.ShowDialog = DialogResult.OK Then
            If ofd.FileName <> String.Empty Then
                imgpath = ofd.FileName
                Dim bm_source As New Bitmap(Bitmap.FromFile(ofd.FileName), 200, 200)
                PictureBox1.Image = bm_source
            End If
        End If
    End Sub

2. Add Item include image

Private Sub additem_Click(sender As Object, e As EventArgs) Handles additem.Click
        Dim comm2 As New OdbcCommand
        Dim comm3 As New OdbcCommand
        Dim status As Byte
        Dim userid2 As Byte
        Form1.connect()
        Form1.conn.Open()
        comm2 = Form1.comm
        Dim itemgroupid As Integer
        comm2 = New OdbcCommand("INSERT INTO `tbl_item`(`igroup_id`, `item_code`, `item_name`, `qty`, `price_out`, `expire_date`, `image`, `status_id`, `created_date`, `created_by`) VALUES (?,?,?,?,?,?,?,?,?,?)", Form1.conn)
        Dim comm As New OdbcCommand
        Dim odt As New DataTable
        Dim ods As New DataSet
        comm3 = New OdbcCommand("select igroup_id from tbl_item_group where igroup_name='" & ComboBoxitemgroup.Text & "'", Form1.conn)
        Dim myDA As OdbcDataAdapter = New OdbcDataAdapter(comm3)
        myDA.Fill(ods, "tbl_item_group")
        odt = ods.Tables("tbl_item_group")
        itemgroupid = odt.Rows(0).Item(0)
        comm2.Parameters.AddWithValue("igroup_id", itemgroupid)
        comm2.Parameters.AddWithValue("item_code", Me.TextBoxitemcode.Text)
        comm2.Parameters.AddWithValue("item_name", Me.TextBox1.Text)
        comm2.Parameters.AddWithValue("qty", Me.TextBoxquantity.Text)
        comm2.Parameters.AddWithValue("price_out", Me.TextBoxsellout.Text)
        comm2.Parameters.AddWithValue("expire_date", CDate(Me.DateTimePicker1.Text))

        Dim bm_source As New Bitmap(Bitmap.FromFile(imgpath), 400, 400)

        Dim clone As Bitmap = CopyBitmap(bm_source)
        Dim timeStamp As String = DateTime.Now.ToString("yyyyMMddhhmmss")

        clone.Save("C:\Users\Laptop\Desktop\rathana\" & timeStamp & ".bmp", ImageFormat.Bmp)
        comm2.Parameters.AddWithValue("image", "C:\Users\Laptop\Desktop\rathana\" & timeStamp & ".bmp")

        If Comitemstatus.Text = "Active" And Comitemstatus.Text <> "" Then
            status = 1
        Else
            status = 0
        End If
        comm2.Parameters.AddWithValue("status_id", status)
        comm2.Parameters.AddWithValue("created_date", Now)
        userid2 = Form1.userid
        comm2.Parameters.AddWithValue("created_by", userid2)
        comm2.ExecuteNonQuery()
        Form1.conn.Close()
    End Sub

Wednesday, July 23, 2014