Received Data from Serial Port not save properly Access Database

sqadree

New member
Joined
Feb 22, 2018
Messages
3
Programming Experience
Beginner
hi all i am new to C#
i am developed Testing utility
currently i received string from Device .

D1U1802200000000000001000000S009000570101640048500851202880033800265003140015000536000280001400048004420013100098002530

and try to save in Access Database .

My problem is that my data is not save in access single record . save data in multipule recode .


C#:
namespace MYabc21_Final
{
    public partial class TestingUtilityForm : TemplateForm
    {
        OleDbConnection connection = new OleDbConnection();

        // String DataOut;
        String DataIN;
        public TestingUtilityForm()
        {
            InitializeComponent();
            
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Interfacing.accdb";
        }

        private void TestingUtilityForm_Load(object sender, EventArgs e)
        {
            connection.Open();
            connectionStatusLabel.Text = "Connection Establish";
            connection.Close();

            // Create Function to get Serial Port 
            string[] ports = SerialPort.GetPortNames();
            comPortsComboBox.Items.AddRange(ports);            
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = comPortsComboBox.Text;
                serialPort1.BaudRate = Convert.ToInt32(baudComboBox.Text);
                serialPort1.DataBits = Convert.ToInt32(dataBitsComboBox.Text);
                serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBitsComboBox.Text);
                serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), parityComboBox.Text);
                serialPort1.Open();
                progressBar1.Value = 100;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                progressBar1.Value = 0;
            }
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            DataIN = serialPort1.ReadExisting();
           // this.Invoke(new EventHandler(ShowData));
            
            try
            {
               string databasedata = DataIN;
               connection.Open();
               OleDbCommand command = new OleDbCommand();
               command.Connection = connection;
               command.CommandText = "insert into tblData(MYdata) values('"+databasedata+"')";
               command.ExecuteNonQuery();
               DataIN = null;
               connection.Close();
             
            }
            catch (Exception ex)
            {
                MessageBox.Show("error"+ex);
            }
        }

        private void ShowData(object sender, EventArgs e)
        {
            int dataINLength = DataIN.Length;
            dataInLengthLabel.Text = String.Format("{0:00}", DataIN.Length);

          INdataTextBox.Clear();
        INdataTextBox.Text += DataIN;
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            INdataTextBox.Text = "";
            dataInLengthLabel.Text = "";
        }

        private void INdataTextBox_TextChanged(object sender, EventArgs e)
        {
           int DataOutLength = INdataTextBox.TextLength;
          dataInLengthLabel.Text = String.Format("{0:00}", DataOutLength);
        }

        private void showDataButton_Click(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "select * from tblData";
                command.CommandText = query;

                OleDbDataAdapter da = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;

                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error" + ex);
            }
        }
    }
    }
 

Attachments

  • DataProblem.PNG
    DataProblem.PNG
    15 KB · Views: 125
Have you debugged your code? If not, you should do that before posting. Set a breakpoint and step through your code line by line. You will then be able to see exactly how your code behaves and how that differs form what you expect. If you still can't solve the issue, at least you'll be able to provide us with all the relevant information and you'll also be able to work out what code is relevant and post only that.

On the subject of code, please don't post code snippets containing huge wads of whitespace. It just makes the code harder to read. There's rarely a good reason to have more than one blank line between any two lines of code and there's never a reason for you to post empty methods or lines of code commented out. It's just lazy for you to expect us to all to read that because you didn't want the time to remove what's irrelevant to the problem.
 
Hi jmcilhinney thanks for your replay set the breakpoint and step through my code. my data is transfer successfully . when remove the breakpoint and run program show error . i think update database through separate thread.
kindly advise me how to create custom thread . whenever received data from serial port data automatically safe in database .


--------------------------------------------
rivate void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Read Data from Serail Port
DataIN += serialPort1.ReadExisting();
// Received Data value in string DataIN and store to database
try
{
string databasedata = DataIN;
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into tblData(MYdata) values('"+databasedata+"')";
command.ExecuteNonQuery();
DataIN = null;
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("error"+ex);
}
 
when remove the breakpoint and run program show error

What error? The exception contains lots of information intended to help diagnose the issue, most notably the error message. If you want us to help diagnose the issue then logic dictates that you pass that information on to us.
 
There is no error show Try Catch not show any error . code run successfully . My Question is why my data not save in shape of single Record. data received from serial port save in multi record.
multi Record
datastring2.PNG

single record
datastring.PNG
 
So, are you saying, without actually saying, that the data all ends up in a single record when you use a breakpoint and step through the code but ends up in multiple records when you don't break in the debugger? If so, where did you place the breakpoint? If it was before the call ReadExisting then the obvious conclusion is that the existing data is not the entire data unless you pause before that call. If you place the breakpoint after that call then it should be obvious if that is the case. If you're only getting part of the data at a time then you have to wait until you have all the data for a record before adding a record rather than adding a record every time you receive data.
 
Back
Top Bottom