Hi Everyone, I hope I don't get flamed for asking this (I just started using Visual Studio for the first time for this project), but let me tell you what the situation is: I'm writing software for a company I work for, basically, we have the HandHeld Products Dolphin 7600. It comes with software called ScanWedge which captures the code from the scanner into the clipboard and automatically pastes it and presses return (I think). I'm writing a program that's supposed to scan a bunch of barcodes and give a final count of how many of each type were scanned. Right now, I have the database created, and everything is all set up. I have a textbox that's waiting on input from the ScanWedge program so that it can run a SQL query on the database to find out the count and then just +1 to it. Right now, the code for the textbox looks like this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
SqlCeConnection cn = new SqlCeConnection(
@"Data Source=\My Documents\BIS.sdf");
string query = "'Select PaperCount FROM Papers('" + e + "') ";
SqlCeCommand run = new SqlCeCommand(query, cn);
try
{
run = new SqlCeCommand(query, cn);
run.CommandType = CommandType.Text;
cn.Open();
run.ExecuteNonQuery();
}
catch (SqlCeException scee)
{
for (int curNdx = 0; curNdx < scee.Errors.Count; ++curNdx)
{
MessageBox.Show("Error:" + scee.Errors[curNdx].ToString() + "\n");
}
Thread.Sleep(5000);
}
}
}
Code Block
The problem so far, among others, is that when I scan something, it only accepts the first character of what I scan. It also throws the error "Error ystem.Data.SqlServer.SqlCeError"
Any Ideas?
Thanks,
Omri | | Pash91 Sunday, December 23, 2007 2:56 AM | When you double click on a control, the event handler that is most commonly used is automatically generated which is why it came up with the textchanged event. However, like I said before, what you want to use is the "keypress" event. If you select your textbox in the designer, then look at the "Properties" grid that contains all the editable options for that control, you will see a little lightning bolt icon, press that and you will see all of the available events that you can listen for including the "keypress" event I mentioned before. Look here for an example of a hash map in .NET : http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx An example using a hash map would be the following. This would keep track of the count of the particular barcodes scanned in:
Code Block
Dim hshBarcodes as New Dictionary(Of String, Integer)()
if (Not hshBarcodes.ContainsKey(txtBarcode.Text)) Then hshBarcodes.Add(txtBarcode.Text, 1) Else hshBarcodes(txtBarcode.Text) += 1 End If Everytime a new barcode is scanned in you should execute that "If...Else.." block. Also the "hshBarcodes" should be a member of the class so that you can continue to persist the object outside of the "keypress" event method. Where "txtBarcode" is the textbox that contains the barcode that you scanned in. So then when you want to get the count of a certain barcode, all you would have to do is:
Code Block
Dim iCount as Integer = hshBarcodes("BarcodeValue") | | ScubaSteve20001 Wednesday, December 26, 2007 2:56 PM | Actually, the SQL Query is:
string query = "'Select PaperCount FROM '" + e + "' ";
| | Pash91 Sunday, December 23, 2007 3:05 AM | Why do you put these code in the TextChanged event handler functionï¼?/font> | | Zhi-Xin Ye Tuesday, December 25, 2007 9:51 AM | It looks like you may have at least a few errors. First off you are searching for the papercount after the text changes in the textbox. This is going to occur whenever a new character is added or removed to the textbox which I do not think is the behavior that you want. To fix this I would put the code in the "KeyPress" event method of the textbox. Then I would wait until the return key is pressed which you can figure out via the argument passed by the "KeyPress" event, then do your calculations or sql queries or whatever needs to be done after a full barcode has been scanned. Another error I see is that in the sql query you are trying to append the value of the EventArgs object to the sql query which I can't imagine would return the results that you want. Those are the immediately obvious problems that I see. Also, I'm not sure on the scope of the problem you're trying to solve since you didn't get too detailed about it. However, if you are just trying to keep track of the count of items scanned, without any persistance between application launches, then you could just use a hash map ( aka in VB.NET, a Dictionary(Of String,Integer)) to store the barcode value, and their respective count. No additional logic would be needed to look up the values since it is a hash map, and internally it is a constant time operation (which means it should be a very quick search). So everytime you scan a new barcode, you would look the value up in the hash map, if it already exists you would increment its current cout, if it doesn't exist you would add the barcode and set its initial count to 1. Write back with more details, or questions if you have any.
| | ScubaSteve20001 Tuesday, December 25, 2007 11:28 PM | First off, let me say thank you for taking the time to answer my question. I am totally new to VB, .NET, and C#, so I am really learning this as I go.
As for the "textchanged" method, I double clicked the box to get the code for it, and that's what came up so I assumed that's where I start writing in.
As to the scope of my problem. Basically what I want to do is have a count of what was scanned. I generated the barcodes so that their name is pretty obvious when it comes time to understanding which one is which. In this case, a database is not necessary, but I found it to be my only viable option as I have never heard of a hash map. Basically what I want to do is have all the papers get scanned, and a text document be generated for printing out the final count of all the barcodes (this can just be the "text scanned" as it would be pretty obvious which item it refers to). I know I can write the code to send it to the scanner, there is actually some sample code available, but I figured this would be a good temporary solution (basically saving it as a text document, opening it in wordpad on the device and sending it to the bluetooth printer.
The reason I pass the scanned argument to the SQL query is so that it can lookup the table in the database. The table names are exactly what the barcodes come up as so I thought this would be an easy way to go about it.
I will start looking for the "keypress" method and trying to figure out how to implement it. Any ideas on this project, and especially any help is much appreciated.
Thank you
| | Pash91 Wednesday, December 26, 2007 2:23 PM | When you double click on a control, the event handler that is most commonly used is automatically generated which is why it came up with the textchanged event. However, like I said before, what you want to use is the "keypress" event. If you select your textbox in the designer, then look at the "Properties" grid that contains all the editable options for that control, you will see a little lightning bolt icon, press that and you will see all of the available events that you can listen for including the "keypress" event I mentioned before. Look here for an example of a hash map in .NET : http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx An example using a hash map would be the following. This would keep track of the count of the particular barcodes scanned in:
Code Block
Dim hshBarcodes as New Dictionary(Of String, Integer)()
if (Not hshBarcodes.ContainsKey(txtBarcode.Text)) Then hshBarcodes.Add(txtBarcode.Text, 1) Else hshBarcodes(txtBarcode.Text) += 1 End If Everytime a new barcode is scanned in you should execute that "If...Else.." block. Also the "hshBarcodes" should be a member of the class so that you can continue to persist the object outside of the "keypress" event method. Where "txtBarcode" is the textbox that contains the barcode that you scanned in. So then when you want to get the count of a certain barcode, all you would have to do is:
Code Block
Dim iCount as Integer = hshBarcodes("BarcodeValue") | | ScubaSteve20001 Wednesday, December 26, 2007 2:56 PM | A little progress has been made. I managed to get the keypress handler to work. It was able to enter the entire Barcode . Now however, It tells me that there is a SQL error: "Error ystem.Data.SqlServerCe.SqlCeError: There was an error parsing the query. [Token line number = 1, Token line offset = 3, Token 3, Token in error = Select PaperCount FROM ]"
I assume that this has to do with my text not being passed correct to the query I set up. Here is my code at the moment:
Code Block
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
SqlCeConnection cn = new SqlCeConnection(
@"Data Source=\My Documents\BIS.sdf");
string query = "'Select PaperCount FROM '" + e + "' ";
SqlCeCommand run = new SqlCeCommand(query, cn);
try
{
run = new SqlCeCommand(query, cn);
run.CommandType = CommandType.Text;
cn.Open();
run.ExecuteNonQuery();
}
catch (SqlCeException scee)
{
for (int curNdx = 0; curNdx < scee.Errors.Count; ++curNdx)
{
MessageBox.Show("Error:" + scee.Errors[curNdx].ToString() + "\n");
}
Thread.Sleep(5000);
}
}
Ideas? | | Pash91 Wednesday, December 26, 2007 3:24 PM | Actually, If I'm not mistaken, the problem is that it's only sending the enter key to the SQL because of the eventargs. I need to have it read the textbefore the enter key | | Pash91 Wednesday, December 26, 2007 3:27 PM | Sorry, I didn't see your reply to my post until now. I will look into doing this in C#. Should I abandon my database method? Is this that much simpler?
| | Pash91 Wednesday, December 26, 2007 3:39 PM | I changed the code to this:
Code Block
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyData == Keys.Enter) { SqlCeConnection cn = new SqlCeConnection( @"Data Source=\My Documents\BIS.sdf"); string table = textBox1.Text; string query = "'Select PaperCount FROM '" + table + "' "; SqlCeCommand run = new SqlCeCommand(query, cn); try { run = new SqlCeCommand(query, cn); run.CommandType = CommandType.Text; cn.Open(); run.ExecuteNonQuery(); } catch (SqlCeException scee) { for (int curNdx = 0; curNdx < scee.Errors.Count; ++curNdx) { MessageBox.Show("Error:" + scee.Errors[curNdx].ToString() + "\n"); } Thread.Sleep(5000); }
} } To debug, I set up a MessageBox to show the data taken from the textbox. It does indeed read the Barcode correctly, however, I'm still getting the same SQL error as earlier and after the FROM it only shows whitespace, not the argument. I'll keep looking | | Pash91 Wednesday, December 26, 2007 4:50 PM | I think it's practically there. The Dictionary is definitely the way to go; however, I am having 1 problem. My code is as follows:
Code Block
using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlServerCe; using System.Threading;
namespace BarCode { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
Dictionary<String, int> Papers = new Dictionary<String, int>();
public bool ContainsKey(TKey key);
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyData == Keys.Enter) { string name = textBox1.Text; if (!Papers.ContainsKey(textBox1.Text)) { Papers.Add(textBox1.Text,1); } else { Papers[textBox1.Text]+= 1; } MessageBox.Show(textBox1.Text + " added. Count is now" + Papers[textBox1.Text]); } } } }
The problem is that I get the error at build say "The type or namespace name 'TKey' cound not be found (are you missing a using directive or an assembly reference)" I can't figure out what to add to get this to work. Thanks | | Pash91 Wednesday, December 26, 2007 6:16 PM | It magically started working. Thanks a lot for all the help
| | Pash91 Wednesday, December 26, 2007 7:01 PM | Everything looks good except this line:
public bool ContainsKey(TKey key);
Why is that there? You should remove that line. Other than that everything looks good.
| | ScubaSteve20001 Wednesday, December 26, 2007 7:01 PM | It magically started working. Thanks a lot for all the help
| | Pash91 Wednesday, December 26, 2007 7:36 PM | It wasn't working so I was following along with MSDN.
Everything works now. I have a few little thigns left to do, but the majority of the work is accomplished. Now, I just hope Handheld products can figure out why the printer doesn't appear in Windows Notepad.
Thanks again.
| | Pash91 Wednesday, December 26, 2007 7:37 PM |
|