Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Imports System.Data
Imports System.Data.Odbc
Imports System.IO


Public Class Form1


    Dim OdbcConnection As Odbc.OdbcConnection
    Dim OdbcCommand As Odbc.OdbcCommand
    Dim sqlString As String
    Public des As String

    Dim connection As String = "DRIVER={MySQL ODBC 3.51 Driver};" & "SERVER=192.168.1.6;" & "DATABASE=ztest;" & "UID=MYBS;" & "PASSWORD=MYBSMYBS;" & "OPTION=3;"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ZtestDataSet.stk_group' table. You can move, or remove it, as needed.
        Me.Stk_groupTableAdapter1.Fill(Me.ZtestDataSet.stk_group)

        TestConnection()
        atxt.Select()
        atxt.Focus()
    End Sub

    Public Sub TestConnection()
        'Try

        Dim odbcconnection As String = "DRIVER={MySQL ODBC 3.51 Driver};" & "SERVER=192.168.1.6;" & "DATABASE=ztest;" & "UID=MYBS;" & "PASSWORD=MYBSMYBS;" & "OPTION=3;"
        Dim connStr As New OdbcConnection(odbcconnection)
        Dim selectSql As String = "SELECT description FROM stk_group WHERE itemgroup = '" & cb1.SelectedValue.ToString & "'"
        Dim comm As New OdbcCommand(selectSql, connStr)
        Dim dr As OdbcDataAdapter
        connStr.Open()
        dr = comm.ExecuteReader
        'While dr.Read()
        'des = dr.Item(0).ToString
        ' End While

        connStr.Close()
        'dr.Close()
        'MsgBox("Connection is ok.")

        'Catch ex As Exception
        'MsgBox("Failed to connect to data source")
        ' End Try
    End Sub



    

    Private Sub WriteToFile(ByVal input As String, ByVal path As String, ByVal append As Boolean)


        Using writer As New StreamWriter(path, append)
            writer.WriteLine(input)

        End Using
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'file location  
        Dim group, amount, final, price As String
        Dim a() As Byte
        Dim testPath As String = "D:\Barcode\textfile\tmp.txt"
        Dim receiptPath As String = "D:\Barcode\textfile\receipt.txt"
        Dim printPagePath As String = "D:\Barcode\textfile\barcode4.txt"
        Dim ibarcodePath As String = "D:\Barcode\textfile\barcode2.txt"
        Dim itextPath As String = "D:\Barcode\textfile\barcode3.txt"
        Dim ipricePath As String = "D:\Barcode\textfile\barcode.txt"
        Dim textPath As String = "D:\Barcode\textfile\text.txt"
        Dim desPath As String = "D:\Barcode\textfile\Description.txt"
        Dim page As Integer
        page = ctxt1.Text

        'combine this code into string code (13digit number )
        group = cb1.SelectedValue.ToString
        amount = String.Format("{0:0000000}", CInt(CDbl(atxt.Text * 100)))
        final = group + "" + amount + "0"
        WriteToFile(final, testPath, True)
        price = Format(CDbl(atxt.Text), "f")

        'read this xy file to fill the barcode size 
        Dim xy, xy2, xy3, xy4, xy5 As String
        Dim reader As New System.IO.StreamReader("D:\Barcode\textfile\xy.txt")
        xy = reader.ReadLine()
        xy2 = reader.ReadLine()
        xy3 = reader.ReadLine()
        xy4 = reader.ReadLine()
        xy5 = reader.ReadLine()
        reader.Close()

        'copy all content txt file to receipt.txt
        Dim ibarcodeString As String = "BARCODE " + xy + ",""128"",50,0,0,2,1,""" + final + """"
        WriteToFile(ibarcodeString, ibarcodePath, False)


        Dim desString As String = "TEXT " + xy5 + ",""1"",0,1,1,""" + des + """"
        WriteToFile(desString, desPath, False)

        Dim textString As String = "TEXT " + xy4 + ",""1"",0,1,1,""" + texttxt.Text + """"
        WriteToFile(textString, textPath, False)

        Dim itextString As String = "TEXT " + xy2 + ",""2"",0,1,1,""" + final + """"
        WriteToFile(itextString, itextPath, False)

        Dim ipriceString As String = "TEXT " + xy3 + ",""3"",0,1,1,""RM " + price + """"
        WriteToFile(ipriceString, ipricePath, False)

        Dim iprintString As String = "PRINT " + ctxt1.Text + ",1"
        WriteToFile(iprintString, printPagePath, False)

        a = System.IO.File.ReadAllBytes("D:\Barcode\textfile\barcode1.txt")
        System.IO.File.WriteAllBytes(receiptPath, a)
        WriteToFile(ibarcodeString, receiptPath, True)
        WriteToFile(desString, receiptPath, True)
        WriteToFile(textString, receiptPath, True)
        WriteToFile(itextString, receiptPath, True)
        WriteToFile(ipriceString, receiptPath, True)
        WriteToFile(iprintString, receiptPath, True)
        'until this session end is the txt file to reicept.txt  
        'MessageBox.Show("Record stored successful")

        'shell profile is to use cmd to run bat file 
        Shell("D:\Barcode\PrintBarcode.bat", vbNormalFocus)

    End Sub


End Class
Posted
Comments
Member 10711343 30-Mar-14 22:27pm    
i got error from
Dim connStr As New OdbcConnection(odbcconnection)
Dim selectSql As String = "SELECT description FROM stk_group WHERE itemgroup = '" & cb1.SelectedValue.ToString & "'"

Dim comm As New OdbcCommand(selectSql, connStr)
Dim dr As OdbcDataAdapter
connStr.Open()
dr = comm.ExecuteReader
'While dr.Read()
'des = dr.Item(0).ToString
' End While
Richard MacCutchan 31-Mar-14 4:33am    
What error? And what is the actual content of your database query string?
Shahan Ayyub 3-Apr-14 14:54pm    
Please make sure you are not putting username and password or your live connection. It would be accessible to others and you might be in trouble.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900