Create the user interface for the Customers form.
using System.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SUP101WindowsMobile;
using Sybase.Mobile;
using Sybase.Persistence;
using Sybase.Collections;
namespace SUP101WindowsMobile
{
public delegate void DelegateAddString(String s);
public delegate void DelegateRefreshItem(long i);
public partial class Customers : Form
{
private const String USERNAME = "supAdmin";
private const String PASSWORD = "s3pAdmin";
private const String HOST = "...";
private const int PORT = 5001;
private const int TIMEOUT = 600;
private System.Collections.Generic.Dictionary<string, long> IdToSK = new System.Collections.Generic.Dictionary<string, long>();
internal static System.Collections.Generic.Dictionary<long, int> SKToIndex = new System.Collections.Generic.Dictionary<long, int>();
DelegateAddString m_DelegateAddString;
DelegateRefreshItem m_refreshItem;
public Customers()
{
InitializeComponent();
m_DelegateAddString = new DelegateAddString(this.AddString);
m_refreshItem = new DelegateRefreshItem(this.RefreshListItem);
}
internal void RefreshListItem(long sk)
{
int index = SKToIndex[sk];
listView1.BeginUpdate();
ListViewItem item = listView1.Items[index];
String id = item.Text;
Customer thisCustomer = Customer.FindByPrimaryKey(Int32.Parse(id));
item.SubItems[1].Text = thisCustomer.Fname;
item.SubItems[2].Text = thisCustomer.Lname;
listView1.EndUpdate();
}
private void AddString(String s)
{
textBox1.Text += s + "\r\n";
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox1.Refresh();
}
private void InitializeApplication_Click(object sender, EventArgs e)
{
Sybase.Mobile.Application app = Sybase.Mobile.Application.GetInstance();
app.ApplicationIdentifier = "SUP101";
MyCallbackHandler.textBox1 = textBox1;
SUP101DB.RegisterCallbackHandler(new MyCallbackHandler());
SUP101DB.SetApplication(app);
SUP101DB.GetSynchronizationProfile().ServerName = HOST;
ConnectionProperties connProps = app.ConnectionProperties;
LoginCredentials loginCredentials = new LoginCredentials(USERNAME, PASSWORD);
connProps.LoginCredentials = loginCredentials;
connProps.ServerName = HOST;
connProps.PortNumber = PORT;
if (app.RegistrationStatus != RegistrationStatus.REGISTERED)
{
AddString("Application registering ... ");
app.RegisterApplication(TIMEOUT);
AddString("Application registered");
}
else
{
AddString("Connecting to server ...");
app.StartConnection(TIMEOUT);
AddString("Connected to server");
}
if (!SUP101DB.IsSynchronized("default"))
{
SUP101DB.DisableChangeLog();
AddString("Package synchronizing ...");
SUP101DB.Synchronize(); // Initial Synchronize
ISynchronizationGroup sg = SUP101DB.GetSynchronizationGroup("default");
sg.EnableSIS = true;
sg.Save();
SUP101DB.Synchronize();
AddString("Package synchronized");
}
SUP101DB.EnableChangeLog();
AddListView();
}
private void LoadData_Click(object sender, EventArgs e)
{
if (Sybase.Mobile.Application.GetInstance().RegistrationStatus == RegistrationStatus.REGISTERED)
{
Cursor.Current = Cursors.WaitCursor;
AddString("Loading data from database ...");
AddDataToListView();
Cursor.Current = Cursors.Default;
}
else
{
AddString("Application is not initialized!");
}
}
private void AddListView()
{
this.listView1.Clear();
listView1.Columns.Add("Id", listView1.Width / 4, HorizontalAlignment.Left);
listView1.Columns.Add("First Name", listView1.Width / 3, HorizontalAlignment.Center);
listView1.Columns.Add("Last Name", listView1.Width / 3, HorizontalAlignment.Right);
listView1.View = View.Details;
listView1.FullRowSelect = true;
}
private void AddDataToListView()
{
this.listView1.Clear();
listView1.Columns.Add("Id", listView1.Width / 4,
HorizontalAlignment.Left);
listView1.Columns.Add("First Name", listView1.Width /
3, HorizontalAlignment.Center);
listView1.Columns.Add("Last Name", listView1.Width / 3,
HorizontalAlignment.Right);
listView1.View = View.Details;
listView1.FullRowSelect = true;
Query query = new Query();
query.Select("x.fname, x.lname, x.surrogateKey, x.id");
query.From("Customer", "x");
query.OrderBy("id", SortOrder.ASCENDING);
int index = 0;
QueryResultSet rs = SUP101DB.ExecuteQuery(query);
while (rs.Next())
{
String fname = rs.GetString(1);
String lname = rs.GetString(2);
long sk = rs.GetLong(3);
String id = rs.GetString(4);
ListViewItem item = new ListViewItem(id);
item.SubItems.Add(fname);
item.SubItems.Add(lname);
listView1.Items.Add(item);
IdToSK.Add(id, sk);
SKToIndex.Add(sk, index++);
}
}
private void update_Click(object sender, EventArgs e)
{
if (listView1.FocusedItem != null)
{
Program.setCustomer(listView1.FocusedItem.Text);
Program.getForm2().Visible = true;
Program.getForm1().Visible = false;
}
else
MessageBox.Show("Please select a row");
}
private void Send_Click(object sender, EventArgs e)
{
Customer.SubmitPendingOperations();
}
public class MyCallbackHandler : Sybase.Persistence.DefaultCallbackHandler
{
public static TextBox textBox1 = new TextBox();
private void invokeDelegate(long i)
{
Customers f = Program.getForm1();
f.Invoke(f.m_refreshItem, new Object[] { i });
}
public override SynchronizationAction OnSynchronize(GenericList<ISynchronizationGroup> groups, SynchronizationContext context)
{
if (context.Status == SynchronizationStatus.FINISHING || context.Status == SynchronizationStatus.ASYNC_REPLAY_UPLOADED)
{
Query query = new Query();
GenericList<IChangeLog> changeLogs = SUP101DB.GetChangeLogs(query);
foreach (IChangeLog changeLog in changeLogs)
{
if (changeLog.EntityType == EntityType.Customer)
{
invokeDelegate(changeLog.SurrogateKey);
}
}
}
return SynchronizationAction.CONTINUE;
}
}
}
}