Results 1 to 10 of 14

Hybrid View

  1. #1
    Senior Member
    Join Date
    Jan 2011
    Location
    Navi Mumbai
    Posts
    30

    Question

    i tried the sample program but its not working at my end, it compiles properly and runs but when i run the client its not working
    http://www.mydemos.com/SimpleDBFeed/index.html
    UNABLE TO CONNECT message is coming.

    i have created the folder SimpleDBFeed and copied the .exe, dll's
    and i have created adapter.xml in Adapter/SimpleDBFeed folder

    its not working
    check the attached sample which i have created
    Attached Files Attached Files

  2. #2
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    1,091
    We couldn't find any issue in the Lightstreamer-related code (apart from the loop with no wait).
    I just suggest you should move any initialization code from the Data Adapter constructor to the Init method, but this is not an issue.
    You should also ensure that the connection to the DB is open upon subsequent invocations of the Run method, but this should not be a problem at the current stage.

    Can you confirm that your concerns are only on DB-related stuff?
    Unfortunately, we are not familiar with that.

    Where do you get the "UNABLE TO CONNECT" message?
    The Data Adapter is supposed to connect in its constructor/Init method, hence, the message could be issued before you ever open the page.
    Can you confirm that?

    When you run your .NET project from Visual Studio, can you see the constructor and the Init method being invoked?
    And after you open the page, can you see the Subscribe method being invoked?

  3. #3
    Senior Member
    Join Date
    Jan 2011
    Location
    Navi Mumbai
    Posts
    30
    i have modified the code and executed, when i run the application from visual studio its properly without any errors. this is the flow of my code 1st my dataadapter constructor execute
    server.Start() method will execute in the DataAdapterLauncher class next init() method from MyDataAdapter class is called there i opened the connection and next its executes SetListener() method after that it execute console.Writeline method next to server.Start() method will be called, this is the i wrote for testing check.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Lightstreamer.Interfaces.Data;
    using System.Collections;
    using System.Data;
    using System.Threading;
    using System.Data.SqlClient;
    
    namespace DataAdapterDemo
    {
        public class MyDataAdapter : IDataProvider, IExternalFeedListener
        {
            private IItemEventListener listener;
            private volatile bool go;
            
            private DataTable dt;
            public SqlConnection conn;
            private SqlCommand comm;
    
            private IDictionary subscribedItems;
    
            public MyDataAdapter()
            {
                //dt = new DataTable();
                //subscribedItems = new Hashtable();
            }
    
            public void Init(IDictionary parameters, string configFile)
            {
                try
                {
                    conn = new SqlConnection("Data Source=(local);Database=Northwind; Integrated Security=true;");
                    comm = new SqlCommand("Select * from Orders", conn);
                    conn.Open();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex);
                    throw new DataProviderException(ex.Message.ToString());
                }
            }
            public void SetListener(IItemEventListener eventListener)
            {
                listener = eventListener;
            }
            public void Subscribe(string itemName)
            {
                if (itemName.Equals("item"))
                {
                    Thread t = new Thread(new ThreadStart(Run));
                    t.Start();
                }
            }
    
            public void Unsubscribe(string itemName)
            { 
                if (itemName.Equals("item"))
                {
                    go = false;
                }
            }
    
            public bool IsSnapshotAvailable(string itemName)
            {
                if (!itemName.StartsWith("item"))
                    throw new SubscriptionException("Unexpected item: " + itemName);
    
                return true;
            }
    
            public void Run()
            {
                go = true;
                try
                {
                    while (go)
                    {
    
                        SqlDataReader dr = comm.ExecuteReader();
                        while (dr.Read())
                        {
                            IDictionary eventData = new Hashtable();
                            eventData.Add("OrderID", dr[0]);
                            eventData.Add("UnitPrice", dr[1]);
                            eventData.Add("Quantity", dr[2]);
                            eventData.Add("Discount", dr[3]);
    
                            listener.Update("item", eventData, false);
                        }
    
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex); }
                try { Thread.Sleep(1000); }
                catch (ThreadInterruptedException ex) { Console.WriteLine(ex); }
                finally { conn.Close(); }
            }
    
            public void onEvent(string itemName, IDictionary currentValues, bool isSnapshot)
            {
                lock (subscribedItems)
                {
                    if (!subscribedItems.Contains(itemName)) return;
    
                    bool started = (bool)subscribedItems[itemName];
                    if (!started)
                    {
                        if (!isSnapshot)
                            return;
    
                        subscribedItems[itemName] = true;
                    }
                    else
                    {
                        if (isSnapshot)
                        {
                            isSnapshot = false;
                        }
                    }
                }
    
                if (listener != null)
                    listener.Update(itemName, currentValues, isSnapshot);
            }
        }
    }
    public interface IExternalFeedListener
        {
            void onEvent(string itemName, IDictionary currentValues, bool isSnapshot);
        }
    DataAdapterLauncher.cs code
    Code:
    public class DataAdapterLauncher
        {
            public static void Main(string[] args)
            {
                string host = ConfigurationManager.AppSettings[0].ToString();
                int reqRepPort = Convert.ToInt32(ConfigurationManager.AppSettings[1].ToString());
                int notifPort = Convert.ToInt32(ConfigurationManager.AppSettings[2].ToString());
                try
                {
                    DataProviderServer server = new DataProviderServer();
                    server.Adapter = new MyDataAdapter();
    
                    TcpClient reqrepSocket = new TcpClient(host,reqRepPort);
                    server.RequestStream = reqrepSocket.GetStream();
                    server.ReplyStream = reqrepSocket.GetStream();
    
                    TcpClient notifSocket = new TcpClient(host, notifPort);
                    server.NotifyStream = notifSocket.GetStream();
    
                    server.Start();
    
                    Console.WriteLine("Remote Adapter connected to LightStreamer Server...");
                    Console.WriteLine("Ready to publish data...");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not connect to Lightstreamer Server.");
                    Console.WriteLine("Make sure Lightstreamer Server is started before this Adapter.");
                    Console.WriteLine(ex);
                }
            }
        }
    when i call client code then i get the "UNABLE TO CONNECT" message

  4. #4
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    1,091
    You should have your Subscribe, Unsubscribe and Run methods also log something.
    If you find out that, after you run the client, Subscribe and Run get called and then "UNABLE TO CONNECT" appears, then the problem should be entirely in the DB access part (in fact, the reported message is not originated by Lightstreamer code).

    In that case, we could not help you.
    You should setup a simple .NET application not involving Lightstreamer in any way and try to log the DB table contents from there at regular intervals.
    Once succeeded, we could help you porting the code in a Data Adapter for Lightstreamer.

  5. #5
    Senior Member
    Join Date
    Jan 2011
    Location
    Navi Mumbai
    Posts
    30

    Question how to display newly added record in webclient

    Now i am able to display the data in webclient and if i change value in db that automatically reflecting in webclient. In INIT method i am retrieving all the record in 1shot and storing in dataTable, and then showing in webclient, if change any value from backend its also reflecting the page with updated value, now if i added new record instead of modifying/updating existing record that should also be subscribed in webclient.

    What are the changes that need to done and where exactly i have to write the code in which event.

  6. #6
    Administrator
    Join Date
    Jul 2006
    Location
    Milan
    Posts
    1,091
    Until now, you have been associating your subscription to a table containing a single record.
    You could see that in a more general way, by associating your subscription to the record in your table that is identified by a predetermined key (and the key value should be in correspondance with the item name).

    Now, in order to subscribe to multiple lines, you should use multiple items and setup multiple lines on your page for displaying the results.
    Otherwise, if you want to subscribe to the entire table contents, you could leverage COMMAND mode.
    Please see this thread, which explores the former case, first.
    Do you think you will need COMMAND mode?

  7. #7
    Senior Member
    Join Date
    Jan 2011
    Location
    Navi Mumbai
    Posts
    30

    Question

    Thanks for your reply Dario, this is code which i am using for retrieve the updated value, which i have written in ExternalFeedProducer class.

    Code:
    public void ComputeNewValues() {
                lock (this) {
                    try
                    {
                        conn = new SqlConnection("Data Source=(local);Database=TestDB;Integrated Security=true;");
    
                        comm = new SqlCommand("Select TotalQuantity, UnitPrice from Products WHERE ProductID=" + _productID + ";", conn);
                        conn.Open();
    
                        SqlDataReader dr = comm.ExecuteReader();
                        while (dr.Read())
                        {
                            int totQty = Convert.ToInt32(dr[0]);
                            double unitP = Convert.ToDouble(dr[1]);
                            if (_totalQuantity != totQty)
                                _totalQuantity = totQty;
                            if (_unitPrice != unitP)
                                _unitPrice = unitP;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message.ToString());
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
    Actually i have a table in SQL SERVER with multiple records [say 10 records with multiple columns], when i run the DataAdapter class in INIT method i am retrieving all the records from SQL SERVER and storing in DataTable.

    Now the DataTable contains 10records stored internally, when i called the webclient i am getting all the records displayed in the page [total 10 records are displaying].

    If i change any value in the DataBase [SQL SERVER table], its reflecting in the web client. My code work perfectly for only those records which is retrieved in INIT method when i run the DataAdapter. BUT if i add extra record into the database SQL SERVER [say i have added 11th record], now my table record count will be 11 instead of 10 and in my webclient its already showing 10records.

    My Issue: when i add new record in db it should also reflect/show in the webclient, that mean whenever any new record is added in DB that should be reflected in my PAGE.

    If i restart DataAdapter and access my webpage then its showing 11 records, instead of restarting dataadapter to check the updated record my dataadapter should automatically check if any new records is added in the database if added then it should display in my web page.

    Hope you got the point what exactly i need. If you want more clarification then let me know i will explain with code and screen shots

 

 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT +1. The time now is 10:02 AM.