Results 1 to 13 of 13

Thread: help with cash drawer kick open with stored drawer codes in database

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    help with cash drawer kick open with stored drawer codes in database

    hello, hopefully someone could help me with my issue. this is a point of sale software and it is a touch screen. the issue im having is that when i tap the drawer open button on the screen the cash drawer is not popping open, instead it prints a small receipt with the drawercode that i have stored on the database. however if i embed the drawer code into the sourcecode like this : DrawerCode = Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25) it pops open the drawer perfectly but the issue remains that not everyone who uses the point of sale software is also using the exact make and model of hardware which is why i decided to store drawer codes in the database.
    Code:
    Sub OpenCashDrawer()
            Try
                con = New SqlConnection(cs)
                con.Open()
                cmd = con.CreateCommand()
                cmd.CommandText = "SELECT RTRIM(CashDrawer) from POSPrinterSetting where CashDrawer='Enabled'"
                rdr = cmd.ExecuteReader()
                If Not rdr.Read() Then
                    frmCustomDialog11.ShowDialog()
                    Exit Sub
                End If
                If (rdr IsNot Nothing) Then
                    rdr.Close()
                End If
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
                'Modify DrawerCode to your receipt printer open drawer code
                con = New SqlConnection(cs)
                con.Open()
                cmd = con.CreateCommand()
                cmd.CommandText = "SELECT RTRIM(CDCode) from POSPrinterSetting where CashDrawer='Enabled' and TillID=@d1"
                cmd.Parameters.AddWithValue("@d1", lblTillID.Text)
                rdr = cmd.ExecuteReader()
                If rdr.Read() Then
                    DrawerCode = rdr(0).ToString
                End If
                If (rdr IsNot Nothing) Then
                    rdr.Close()
                End If
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
                'Modify PrinterName to your receipt printer name
                con = New SqlConnection(cs)
                con.Open()
                cmd = con.CreateCommand()
                cmd.CommandText = "SELECT RTRIM(PrinterName) from POSPrinterSetting where TillID=@d1 and IsEnabled='Yes'"
                cmd.Parameters.AddWithValue("@d1", lblTillID.Text)
                rdr = cmd.ExecuteReader()
                If rdr.Read() Then
                    s4 = rdr.GetValue(0)
                End If
                If (rdr IsNot Nothing) Then
                    rdr.Close()
                End If
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
                Dim PrinterName As String = s4
                RawPrinter.PrintRaw(PrinterName, DrawerCode)
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    Last edited by iced8out; Apr 20th, 2021 at 06:42 PM. Reason: add more detail

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with cash drawer kick open with stored drawer codes in database

    First things first, don't use a data reader. If you're retrieving a single value from a database then call ExecuteScalar, not ExecuteReader.

    As for the issue, I'm wondering whether it has something to do with the text encoding. I would suggest that you retrieve that data from the database and then compare it to your hard-coded value. The next step depends on whether they are equal or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: help with cash drawer kick open with stored drawer codes in database

    thanks jmcilhinney for your reply. i went ahead changed it to ExecuteScalar as you suggested. i also compared the code thats prints on the receipt and its exactly as the hard-coded value. yet when i hard code with the same exact drawer code value in to the source code its kicks the drawer instantly without issue. what could you suggest i do to have it kick open the drawer with the drawer code value coming from the database. thank u

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with cash drawer kick open with stored drawer codes in database

    They're obviously not the same or using them would produce the same result. How did you compare them? Did you just look at them? the fact that two things look the same is not proof that they are the same. Did you actually use code to compare the two Strings for equality? Did you compare each Char in the Strings?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: help with cash drawer kick open with stored drawer codes in database

    i copied and pasted the code stored in the database and pasted it into the source and it worked. now when i did it the other way where the value comes from the database its prints a receipt with the exact drawer code but doesnt kick open the drawer.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with cash drawer kick open with stored drawer codes in database

    That's not what I said to do. If you're not going to listen when the help you asked for is provided then I'll waste no more time providing it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: help with cash drawer kick open with stored drawer codes in database

    Quote Originally Posted by iced8out View Post
    i copied and pasted the code stored in the database and pasted it into the source and it worked. now when i did it the other way where the value comes from the database its prints a receipt with the exact drawer code but doesnt kick open the drawer.
    try compare again with code
    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'Dim str1 As String = "TestString"
            'Dim str2 As String = "teststring"
    
            Dim str1 As String = "Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25)"
            Dim str2 As String = "Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25) "
    
            If String.Compare(str1, str2, StringComparison.Ordinal) = 0 Then
                MessageBox.Show("Are Equal")
            Else
                MessageBox.Show("Are Not Equal")
            End If
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: help with cash drawer kick open with stored drawer codes in database

    Quote Originally Posted by iced8out View Post
    i copied and pasted the code stored in the database and pasted it into the source and it worked. now when i did it the other way where the value comes from the database its prints a receipt with the exact drawer code but doesnt kick open the drawer.
    Wait... do you literally have "Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25)" in the database? If so, that's your problem. That's not what should be there. What you should have in the database is the characters that are returned by that code, not the code itself.\


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: help with cash drawer kick open with stored drawer codes in database

    Quote Originally Posted by ChrisE View Post
    try compare again with code
    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'Dim str1 As String = "TestString"
            'Dim str2 As String = "teststring"
    
            Dim str1 As String = "Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25)"
            Dim str2 As String = "Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25) "
    
            If String.Compare(str1, str2, StringComparison.Ordinal) = 0 Then
                MessageBox.Show("Are Equal")
            Else
                MessageBox.Show("Are Not Equal")
            End If
        End Sub
    hi chris thank you for your reply, i ran your code and i got the message that " not equal"

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: help with cash drawer kick open with stored drawer codes in database

    Quote Originally Posted by techgnome View Post
    Wait... do you literally have "Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25)" in the database? If so, that's your problem. That's not what should be there. What you should have in the database is the characters that are returned by that code, not the code itself.\


    -tg
    hi techgnome, thank you for your reply. yes i have the code literally "Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25)" in the database because i dont how to store it in the database any other way. if you could give me a better way to do it i would be greatly appreciate it. ive tried removing the "chr" and putting it in like this : (27)(7)(11)(55)(25) but same result, ive tried also : Chr(27) Chr(7) Chr(11) Chr(55) Chr(25) but also no luck.

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: help with cash drawer kick open with stored drawer codes in database

    Quote Originally Posted by iced8out View Post
    hi techgnome, thank you for your reply. yes i have the code literally "Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25)" in the database because i dont how to store it in the database any other way. if you could give me a better way to do it i would be greatly appreciate it. ive tried removing the "chr" and putting it in like this : (27)(7)(11)(55)(25) but same result, ive tried also : Chr(27) Chr(7) Chr(11) Chr(55) Chr(25) but also no luck.
    Imagine your password to a website is 200, should the system accept a password of "Two Hundred"? That should demonstrate why what you are trying isn't going to work.

    I'm not motivated enough to fire up my box that has SQL on it to try it, but I'm going to assume that storing the non-printable low ascii characters as-is won't work.

    If this were me, I'd probably store it as:

    Code:
    27,7,11,55,25
    in SQL, retrieve it in VB, split it into an array, and then use Chr() on the array values to build the string. That being said, there's likely more elegant ways to do it than that.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with cash drawer kick open with stored drawer codes in database

    Good grief! What Chr does is create a Char value from an ASCII code and your code here:
    vb.net Code:
    1. DrawerCode = Chr(27) & Chr(7) & Chr(11) & Chr(55) & Chr(25)
    creates a String containing those characters. It's that String that you should be saving to the database. I'm sure that you're quite capable of working out how to save a String to a database. Alternatively, you could convert the String to a Byte array using Encoding.ASCII.GetBytes and then store that as binary data in the database, calling Encoding.ASCII.GetString in your application. That extra step seems a bit pointless though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help with cash drawer kick open with stored drawer codes in database

    Quote Originally Posted by iced8out View Post
    hi chris thank you for your reply, i ran your code and i got the message that " not equal"
    Chr(80) = “P”

    But

    “Chr(80)” <> “P”

    Why??? Check the quotation marks. Anything contained within double quotation marks is a String literal

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width