Abhishek Modak
asked on
Passing data between different windows application forms
I have developed two forms, first form for scanning Bluetooth devices and second form to browse and select a folder. I want to pass data of device name and it's Bluetooth address from first form to second form and store it in database. I tried this but the database takes entries only from one form at a time and neglects the data entered in other form. Plz provide a solution to my query.
IMG_20180124_141234034.jpg
IMG_20180124_141234034.jpg
IMG_20180124_141234034.jpg
IMG_20180124_141234034.jpg
since you got database, why not using it as the medium to store the data and share between your forms?
you already have the blootooth form as a parent dialog which invokes the folder browse dialog as a modal dialog. so you could make a constructor of the folder browser dialog where you pass the needed data as arguments. after ok returned, you would get the data to store in database from the folder browse form 'Fbd', which is still a valid object as long as it doesn't go out of scope. then store the combined data to the SQL 'con' there.
Sara
Sara
ASKER
When I try to open and connect to a database connection from Bluetooth form it stores the device name and device address to database,then I close the database connection from Bluetooth form and start connection in folder browse form.But the folder path entry doesn't get stored in database.
ASKER
Also I tried to open a database connection first from folder browser form and then I have created public string variables in Bluetooth form to store device name and device address .Then I created an object of Bluetooth form in folder browse form and tried to store these information along with the folder path,but then also only folder path gets saved,no entry gets stored for device name and address
how these 2 forms were being opened? is your program multi-threaded ?
ASKER
I have created four forms in total and each form is linked to another form using objects. Like after I get all information in Bluetooth form ,the form gets hidden the using an object for folder browse form I display the folder browse form and then select a particular folder and then hide it to open another form for entering password
Are these forms all in the same Visual Studio solution? If so: https://www.experts-exchange.com/articles/4322/How-to-Pass-Data-Between-Forms-in-NET.html
If not, then you need something like .NET remoting, a self-hosted web service in each app, or the Win API.
If not, then you need something like .NET remoting, a self-hosted web service in each app, or the Win API.
from your code image (can you repost it as codes instead?) it seems that your Bluetooth form bt was not opened modally?
ASKER
How to open bluetooth form bt modally?
ASKER
bluetooth form code:
using System;
using System.Collections.Generic ;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
namespace Folder_Security
{
public partial class Bluetooth_App_Form : Form
{
List<string> items;
List<string> paired_items;
public String Dev_Address, Dev_Name;
public Bluetooth_App_Form()
{
items = new List<string>();
paired_items = new List<string>();
InitializeComponent();
}
private void btn_Scan_Devices_Click(obj ect sender, EventArgs e)
{
startScan();
}
private void startScan()
{
Discovered_Devices_List.Da taSource = null;
Discovered_Devices_List.It ems.Clear( );
items.Clear();
Thread bluetoothScanThread = new Thread(new ThreadStart(scan));
bluetoothScanThread.Start( );
}
BluetoothDeviceInfo[] devices;
private void scan()
{
updateUI("Starting Scan..");
BluetoothClient client = new BluetoothClient();
devices = client.DiscoverDevicesInRa nge();
updateUI("Scan completed");
updateUI(devices.Length + " devices discovered");
foreach (BluetoothDeviceInfo d in devices)
{
items.Add(d.DeviceName);
//trial
Dev_Name = d.DeviceName;
Dev_Address = d.DeviceAddress + "";
//select where deviceaddress!=d.deviceadd ress
}
updateDeviceList();
}
Guid mUUID = new Guid("2DA0CC0E-1927-4CE8-A 6BF-1B7DAF 4BB9D4");
private void updateUI(string message)
{
Func<int> del = delegate ()
{
tbOutput.AppendText(messag e + System.Environment.NewLine );
return 0;
};
Invoke(del);
}
private void updateDeviceList()
{
Func<int> del = delegate ()
{
Discovered_Devices_List.Da taSource = items;
Paired_Devices_List.DataSo urce = paired_items;
return 0;
};
Invoke(del);
}
BluetoothDeviceInfo deviceInfo;
private void Discovered_Devices_List_Do ublelick(o bject sender, EventArgs e)
{
deviceInfo = devices.ElementAt(Discover ed_Devices _List.Sele ctedIndex) ;
updateUI(deviceInfo.Device Name + " was selected , attempting connect");
if (pairDevice())
{
updateUI("device paired..");
updateUI("starting connect thread");
Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectT hread));
bluetoothClientThread.Star t();
}
else
{
updateUI("Pair failed");
}
}
private void ClientConnectThread()
{
BluetoothClient client = new BluetoothClient();
updateUI("attempting connect..");
client.BeginConnect(device Info.Devic eAddress, mUUID, new AsyncCallback(BluetoothCli entConnect Callback), client);
}
private void BluetoothClientConnectCall back(IAsyn cResult result)
{
if (result.IsCompleted)
{
updateUI("Device is now connected");
}
}
string myPin = "";
private bool pairDevice()
{
if (!deviceInfo.Authenticated )
{
if (!BluetoothSecurity.PairRe quest(devi ceInfo.Dev iceAddress , myPin))
{
return false;
}
}
return true;
}
private void btn_View_Paired_Devices_Cl ick(object sender, EventArgs e)
{
startScan_paired();
}
private void startScan_paired()
{
Paired_Devices_List.DataSo urce = null;
Paired_Devices_List.Items. Clear();
paired_items.Clear();
Thread bluetoothScanThread = new Thread(new ThreadStart(paired_scan));
bluetoothScanThread.Start( );
}
BluetoothDeviceInfo[] paired_devices;
private void paired_scan()
{
updateUI("Retreving paired device list...");
BluetoothClient client = new BluetoothClient();
paired_devices = client.DiscoverDevicesInRa nge();
updateUI("Retrevied paired device list.");
foreach (BluetoothDeviceInfo d in paired_devices)
{
if (d.Remembered == true)
paired_items.Add(d.DeviceN ame);
Dev_Name = d.DeviceName;
Dev_Address = d.DeviceAddress + "";
}
updateDeviceList();
}
private void Paired_Devices_List_Select edIndexCha nged(objec t sender, EventArgs e)
{
if (Paired_Devices_List.Selec tedIndex >= 0)
{
btn_Proceed.Enabled = true;
}
else
{
btn_Proceed.Enabled = false;
}
}
private void btn_Proceed_Click(object sender, EventArgs e)
{
this.Hide();
AddFilesForEncryption file1 = new AddFilesForEncryption();
file1.Show();
}
}
}
using System;
using System.Collections.Generic
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
namespace Folder_Security
{
public partial class Bluetooth_App_Form : Form
{
List<string> items;
List<string> paired_items;
public String Dev_Address, Dev_Name;
public Bluetooth_App_Form()
{
items = new List<string>();
paired_items = new List<string>();
InitializeComponent();
}
private void btn_Scan_Devices_Click(obj
{
startScan();
}
private void startScan()
{
Discovered_Devices_List.Da
Discovered_Devices_List.It
items.Clear();
Thread bluetoothScanThread = new Thread(new ThreadStart(scan));
bluetoothScanThread.Start(
}
BluetoothDeviceInfo[] devices;
private void scan()
{
updateUI("Starting Scan..");
BluetoothClient client = new BluetoothClient();
devices = client.DiscoverDevicesInRa
updateUI("Scan completed");
updateUI(devices.Length + " devices discovered");
foreach (BluetoothDeviceInfo d in devices)
{
items.Add(d.DeviceName);
//trial
Dev_Name = d.DeviceName;
Dev_Address = d.DeviceAddress + "";
//select where deviceaddress!=d.deviceadd
}
updateDeviceList();
}
Guid mUUID = new Guid("2DA0CC0E-1927-4CE8-A
private void updateUI(string message)
{
Func<int> del = delegate ()
{
tbOutput.AppendText(messag
return 0;
};
Invoke(del);
}
private void updateDeviceList()
{
Func<int> del = delegate ()
{
Discovered_Devices_List.Da
Paired_Devices_List.DataSo
return 0;
};
Invoke(del);
}
BluetoothDeviceInfo deviceInfo;
private void Discovered_Devices_List_Do
{
deviceInfo = devices.ElementAt(Discover
updateUI(deviceInfo.Device
if (pairDevice())
{
updateUI("device paired..");
updateUI("starting connect thread");
Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectT
bluetoothClientThread.Star
}
else
{
updateUI("Pair failed");
}
}
private void ClientConnectThread()
{
BluetoothClient client = new BluetoothClient();
updateUI("attempting connect..");
client.BeginConnect(device
}
private void BluetoothClientConnectCall
{
if (result.IsCompleted)
{
updateUI("Device is now connected");
}
}
string myPin = "";
private bool pairDevice()
{
if (!deviceInfo.Authenticated
{
if (!BluetoothSecurity.PairRe
{
return false;
}
}
return true;
}
private void btn_View_Paired_Devices_Cl
{
startScan_paired();
}
private void startScan_paired()
{
Paired_Devices_List.DataSo
Paired_Devices_List.Items.
paired_items.Clear();
Thread bluetoothScanThread = new Thread(new ThreadStart(paired_scan));
bluetoothScanThread.Start(
}
BluetoothDeviceInfo[] paired_devices;
private void paired_scan()
{
updateUI("Retreving paired device list...");
BluetoothClient client = new BluetoothClient();
paired_devices = client.DiscoverDevicesInRa
updateUI("Retrevied paired device list.");
foreach (BluetoothDeviceInfo d in paired_devices)
{
if (d.Remembered == true)
paired_items.Add(d.DeviceN
Dev_Name = d.DeviceName;
Dev_Address = d.DeviceAddress + "";
}
updateDeviceList();
}
private void Paired_Devices_List_Select
{
if (Paired_Devices_List.Selec
{
btn_Proceed.Enabled = true;
}
else
{
btn_Proceed.Enabled = false;
}
}
private void btn_Proceed_Click(object sender, EventArgs e)
{
this.Hide();
AddFilesForEncryption file1 = new AddFilesForEncryption();
file1.Show();
}
}
}
ASKER
Folder Browse code:
using System;
using System.Collections.Generic ;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Folder_Security
{
public partial class AddFilesForEncryption : Form
{
public AddFilesForEncryption()
{
InitializeComponent();
}
private void btn_Browse_Click(object sender, EventArgs e)
{
Bluetooth_App_Form bt = new Bluetooth_App_Form();
FolderBrowserDialog Fbd = new FolderBrowserDialog();
SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\HP\Documents\Visu al Studio 2015\Projects\Folder_Secur ity\Folder _Security\ Database1. mdf; Integrated Security = True");
con.Open();
if (Fbd.ShowDialog() == DialogResult.OK)
{
txt_File_Path.Text = Fbd.SelectedPath;
MessageBox.Show("Your Folder has been selected for encryption");
SqlCommand cmd = new SqlCommand("INSERT INTO Devices(DeviceAddress,Devi ceName,Fil e_Path,Pas sword) VALUES('"+bt.Dev_Address+" ','"+bt.De v_Name+"', '" + txt_File_Path.Text + "','dsgg')", con);
cmd.ExecuteNonQuery();
btn_Proceed.Enabled = true;
}
else
{
MessageBox.Show("Please select a folder to proceed");
btn_Proceed.Enabled = false;
}
con.Close();
}
private void btn_Open_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process .Start(txt _File_Path .Text);
}
catch
{ }
}
private void btn_Proceed_Click(object sender, EventArgs e)
{
this.Hide();
SetPwd pwd = new SetPwd();
pwd.Show();
}
}
}
using System;
using System.Collections.Generic
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Folder_Security
{
public partial class AddFilesForEncryption : Form
{
public AddFilesForEncryption()
{
InitializeComponent();
}
private void btn_Browse_Click(object sender, EventArgs e)
{
Bluetooth_App_Form bt = new Bluetooth_App_Form();
FolderBrowserDialog Fbd = new FolderBrowserDialog();
SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\HP\Documents\Visu
con.Open();
if (Fbd.ShowDialog() == DialogResult.OK)
{
txt_File_Path.Text = Fbd.SelectedPath;
MessageBox.Show("Your Folder has been selected for encryption");
SqlCommand cmd = new SqlCommand("INSERT INTO Devices(DeviceAddress,Devi
cmd.ExecuteNonQuery();
btn_Proceed.Enabled = true;
}
else
{
MessageBox.Show("Please select a folder to proceed");
btn_Proceed.Enabled = false;
}
con.Close();
}
private void btn_Open_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process
}
catch
{ }
}
private void btn_Proceed_Click(object sender, EventArgs e)
{
this.Hide();
SetPwd pwd = new SetPwd();
pwd.Show();
}
}
}
ASKER
Now i have provided the codes for both the forms. Now can someone please suggest me the exact commands to be inserted in both the forms so that the entries from both the forms get inserted and stored in the database 'devices' after the execution of both the forms!
Don't forget that your forms are just objects, so you can pass data between them the same as you can with any objects. You could either add a public property to the AddFilesForEncryption form and set that to the relevant info, or you could override / overload the AddFilesForEncryption constructor and pass in the values when you creae the form. @sarabande has already alluded to this.
In your AddFilesForEncryption form, overload the constructor to receive the relevant data. Add in a couple of private fields to store the info:
In your AddFilesForEncryption form, overload the constructor to receive the relevant data. Add in a couple of private fields to store the info:
private string devName;
private string devAddress;
public AddFilesForEncryption(string devName, string devAddress)
{
InitializeComponent();
this.devName = devName;
this.devAddress = devAddress;
}
Now in the Proceed method on your BlueTooth form, just pass the values in the ctor:AddFilesForEncryption file1 = new AddFilesForEncryption(Dev_Name, Dev_Address);
file1.Show();
The values passed in will be available in your AddFileForEncryption form as the private fields devName and devAddress, so you can use them in your Database query.
ASKER
@chris stanyon ,your solution worked.
ASKER
@chris stanyon ,your solution worked.
ASKER
Can someone please tell me how can I check using an SQL query whether an already scanned device address is already stored in the database? If it's stored then insert query should not execute , otherwise if no entry of the scanned device is found in database then only the insert query should run.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have made device address in Bluetooth form as the primary key and I would like to check the SQL exception for insert query using either devAddress or devName
ASKER
How to check whether a paired Bluetooth device is in range or not according to the code provided in my Bluetooth form?
These are 2 different questions to the original so they should be raised as new questions in the relevant forums. This question was in regard to passing data between forms which has already been answered.
ASKER
Thanks for the assistance.