1
Answer

Passing data from C# to ComPort2 (USb to RS232)

I have a card dispensing device in which I want to be triggered by a C# program.
 
The Card Dispensing Device has a command to trigger the card-out function.
 
 
 
Now when I checked the 42 is a hex and the H is just a hex determinator of some sort.
 
They gave me an exe which do the sending of data but I want to repliacte it and create my own.
 
In the exe that they gave when I press card out it shows a command like this one
 
-->[0x01] [0x42]
 
the first [] is for the device the second [] is the I think ascii hex of the command.
 
Here's the code I have so far
  1. using System.IO.Ports;  
  2. namespace Card_Reader  
  3. {  
  4. public partial class Form1 : Form  
  5. {  
  6. public Form1()  
  7. {  
  8. InitializeComponent();  
  9. }  
  10. private void button1_Click(object sender, EventArgs e)  
  11. {  
  12. using (SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8))  
  13. {  
  14. byte[] bytesToSend = new byte[1] { 0x2A };  
  15. port.Open();  
  16. port.Write(bytesToSend, 0, 1);  
  17. }  
  18. }  
I did not include all closing brackets.
Answers (1)
3
Nainil
NA 660 0 15y

.NET:   
   string
userString = textBox1.Text;
   string replacedString = userString.Replace("'", "''");
 
SQL:
   replacedString = Replace(userString,"'","''")
 
Hope this helps.
Accepted
1
Nainil
NA 660 0 15y

Hi Giorgio,
    Whenever you have single quotes, replace them with 2 single quotes. That should work.
0
Gustavo
NA 1.8k 386.6k 15y

Nainil:
 
Yes, thank you.
0
Gustavo
NA 1.8k 386.6k 15y

Nainil:
 
Can you give me a sample code of the 2 types?
 
0
Nainil
NA 660 0 15y

Giorgio,
   You can either use .NET string.Replace() method to replace any single quotes with 2 single qoutes or you can use T-SQL's Replace function. Since the value being inserted is passed in as a SqlParameter.Value property, you can run the Replace function through it and ensure that any string being passed to the stored procedure is clean. Hope this helps.
0
Gustavo
NA 1.8k 386.6k 15y

Nainil:
 
Yes, if it was a string. But it will be some text that the user entered. Is there a way to do it automatically when I put it into the sql command to save it?