none
Not displaying continues data of networkstream in DataGridView RRS feed

  • Question

  • reading the data from device through TcpClient class and method is below.

    once i made the connection using Ipaddress and port through ethernet connection able to read the bytes of data. My read method reading networkstream data of particular number of bytes and trimming the string as my need and updating to a database and trying to show the updated data in datagridview.It will not displaying any data in datagridview.

    here is the below code i made.where i went wrong?


            private void ReceiveMethod()
            {// coding part of receiving changed.
                try
                {
                    string IPStr = textIP.Text.Trim();
                    string portStr = textPort.Text.Trim(); 
                    int port = Convert.ToInt32(portStr);
                    NetworkStream ns;
                    int bytesRead = 0;
                    byte[] buffer = new byte[9];
                               
                    try
                    {
                        
                        IPAddress ipAddress = System.Net.IPAddress.Parse(IPStr);
                        //create server's tcp listener for incoming connection
                        client = new TcpClient();
                        client.Connect(IPStr, port);
                        while (true)
                       {
                            ns = client.GetStream();
                            ns.Read(buffer, (int)bytesRead, buffer.Length - (int)bytesRead);
     
                            ASCIIEncoding encoder = new ASCIIEncoding();
                             msg = encoder.GetString(buffer);
                             //MessageBox.Show(msg);
                            GetData(msg);                        
                            
                        }
                        client.Close();         
                        
                    }
    
    
    GetData(string data)
    {
    
    I'm trimming the data value according to my need
    and the updateing one filed of db and trying to populate table data in datagridview ..
    }
    
    
    

    But am not able to view the data in datagridview.some times only one time can show ...no data most of the times...any suggestion  highly appreciate, please let me know where i went wrong.


    • Moved by Lisa Zhu Thursday, March 28, 2013 7:35 AM network related
    Monday, March 25, 2013 6:15 PM

All replies

  • I don't see your code do any datagridview binding?

    chanmm


    chanmm

    Tuesday, March 26, 2013 2:35 AM
  • ok here is my GetData(string data) method.

                data1 = data
                data1 = data1.Substring(1);
                data1 = data1.Remove(5);
                //MessageBox.Show(data);
                SqlConnection con = new SqlConnection("connectionstring");
                con.Open();
                SqlDataReader sda;
                SqlCommand cmd = new SqlCommand("Select CardID from tablename where userID='"+data1+"'", con);
                sda=cmd.ExecuteReader();
                if (sda.HasRows)
                {
                    DateTime datetime;
                      datetime =DateTime.Now;
                    dataGridView1.Visible = true;
                    int count = new Regex(data1).Matches(data1).Count;
                    if (IsOdd(count))
                    {
                        string Cmd2 = "Update tablename set timetoentry='" + datetime.ToString("yyyy-MM-dd hh:mm:ss")+ "'where UserID='" + data + "';";
                    }
                    else
                    {
                        string Cmd2 = "Update tablename set timetoexit='" + datetime.ToString("yyyy-MM-dd hh:mm:ss") + "'where userID='" + data + "';";
                    }
                    string cmd1 = "select * from tablename where userID='" + data + "'";
                    SqlDataAdapter sdap = new SqlDataAdapter(cmd1, con);
                    DataSet dataset = new DataSet();
                    sda.Close();
                    sdap.Fill(dataset,"tablename");
                    dataGridView1.DataSource = dataset.Tables[0];                            


    Tuesday, March 26, 2013 2:58 AM
  • verify that your datagridview itemtemplate/row is mapping/showing proper field name

    it will show up blank if you map to wrong fields.

    and try dataGridView1.DataSource = dataset.Tables[0].Rows;

    Tuesday, March 26, 2013 4:08 AM
  • Hi kida.altantis,

    I think there is nothing wrong with your datagridview binding, please try to execute cm1 "select * from tablename where userID='yourUserID' " to check whether there is some data in your table.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 27, 2013 5:23 AM
  • If i remove while (true) in the above code...it will show the details in datagridview. because am reading the data from network stream continuously. Now i created a delegate for viewing the data indatagridview. So that i can view the updated data for every 1min.

    when i run the above program,it takes around 1 or 2 mins delay to show the data for the first time in

     while (true)
                       {
                            ns = client.GetStream();
                            ns.Read(buffer, (int)bytesRead, buffer.Length - (int)bytesRead);
     
                            ASCIIEncoding encoder = new ASCIIEncoding();
                             msg = encoder.GetString(buffer);
                             //MessageBox.Show(msg);
                            GetData(msg);                        
                            
                        }

    text box to see the value of buffer.But when ever i check in hyper terminal as soon as i connect it gives data continuously for every 2 seconds.I guess the below code is the only problem not working properly. Is there any way that i read data for every 2 seconds and can display the change in datagridview too? I am new to socket programming .if it there any better way to do it.please let me know.

    thanks




    Thursday, March 28, 2013 2:43 AM
  • Hi kida.altantis,

    Since you need some help with socket programming, I would move this thread to Windows
    Communication Foundation, Serialization, and Networking
    forum for you to get better support.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 28, 2013 7:35 AM