Skip to content

Commit 701d259

Browse files
author
Jayson Ragasa
committed
more model clean up going on | fixed that database locking issue
1 parent 4c07972 commit 701d259

File tree

14 files changed

+561
-484
lines changed

14 files changed

+561
-484
lines changed

Database/Database.cs renamed to Database/Controllers/Database.cs

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public Database()
2323

2424
this._connection.ConnectionString = this._connectionString;
2525

26-
if (!System.IO.File.Exists(db_name))
27-
{
28-
SQLiteConnection.CreateFile(db_name);
29-
_connection.Open();
30-
_connection.Close();
31-
}
26+
//if (!System.IO.File.Exists(db_name))
27+
//{
28+
// SQLiteConnection.CreateFile(db_name);
29+
// _connection.Open();
30+
// _connection.Close();
31+
//}
3232
}
3333

3434
public void Delete(bool all)
@@ -70,13 +70,41 @@ public string ExecuteQuery(string sql_query, SQLiteParameter[] parameters, out S
7070
{
7171
string res = string.Empty;
7272

73-
if (_connection.State == ConnectionState.Open)
74-
{
75-
CloseConnection();
76-
}
73+
reader = null;
74+
75+
//using(SQLiteConnection conn = new SQLiteConnection(this._connectionString))
76+
//{
77+
// conn.Open();
78+
// using (SQLiteCommand comm = conn.CreateCommand())
79+
// {
80+
// comm.CommandText = sql_query;
81+
82+
// if(parameters != null)
83+
// {
84+
// comm.Parameters.AddRange(parameters);
85+
// }
86+
87+
// try
88+
// {
89+
// this._command = comm;
90+
91+
// reader = this._command.ExecuteReader(CommandBehavior.CloseConnection);
92+
// }
93+
// catch (Exception ex)
94+
// {
95+
// res = CreateExceptionMessage(ex);
96+
// }
97+
// }
98+
//}
7799

78100
reader = null;
79101

102+
//if (_connection.State == ConnectionState.Open)
103+
//{
104+
// CloseConnection();
105+
//}
106+
107+
_connection = new SQLiteConnection(this._connectionString);
80108
_connection.Open();
81109
_command = _connection.CreateCommand();
82110
_command.CommandText = sql_query;
@@ -103,11 +131,35 @@ public string ExecuteNonQuery(string sql_query, SQLiteParameter[] parameters)
103131
{
104132
string res = string.Empty;
105133

106-
if (_connection.State == ConnectionState.Open)
107-
{
108-
CloseConnection();
109-
}
110-
134+
//using (SQLiteConnection conn = new SQLiteConnection(this._connectionString))
135+
//{
136+
// conn.Open();
137+
// using (SQLiteCommand comm = conn.CreateCommand())
138+
// {
139+
// comm.CommandText = sql_query;
140+
141+
// if (parameters != null)
142+
// {
143+
// comm.Parameters.AddRange(parameters);
144+
// }
145+
146+
// try
147+
// {
148+
// comm.ExecuteNonQuery();
149+
// }
150+
// catch (Exception ex)
151+
// {
152+
// res = CreateExceptionMessage(ex);
153+
// }
154+
// }
155+
//}
156+
157+
//if (_connection.State == ConnectionState.Open)
158+
//{
159+
// CloseConnection();
160+
//}
161+
162+
_connection = new SQLiteConnection(this._connectionString);
111163
_connection.Open();
112164
_command = _connection.CreateCommand();
113165
_command.CommandText = sql_query;
@@ -126,15 +178,22 @@ public string ExecuteNonQuery(string sql_query, SQLiteParameter[] parameters)
126178
System.Diagnostics.Debug.WriteLine("ERROR! " + ex.Message + "\r\n" + ex.StackTrace);
127179
res = CreateExceptionMessage(ex);
128180
}
129-
181+
130182
_connection.Close();
131183

132184
return res;
133185
}
134186

135187
public void CloseConnection()
136188
{
137-
_connection.Close();
189+
_command.Dispose();
190+
191+
if (_connection != null)
192+
{
193+
_connection.Close();
194+
_connection.Dispose();
195+
_connection = null;
196+
}
138197
}
139198

140199
private string CreateExceptionMessage(Exception ex)

Database/Groups.cs renamed to Database/Controllers/Groups.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,11 @@ public int GetIDByGroupName(string name)
188188

189189
if (result == string.Empty)
190190
{
191-
reader.Read();
192-
ret = int.Parse(reader["groupid"].ToString());
191+
if (reader.HasRows)
192+
{
193+
reader.Read();
194+
ret = int.Parse(reader["groupid"].ToString());
195+
}
193196
}
194197
else
195198
{
File renamed without changes.

Database/Database.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@
7272
<Reference Include="System.Windows.Forms" />
7373
</ItemGroup>
7474
<ItemGroup>
75-
<Compile Include="Database.cs" />
76-
<Compile Include="Groups.cs" />
75+
<Compile Include="Controllers\Database.cs" />
76+
<Compile Include="Controllers\Groups.cs" />
7777
<Compile Include="Models\Model_GroupDetails.cs" />
7878
<Compile Include="Models\Model_ServerDetails.cs" />
7979
<Compile Include="Properties\AssemblyInfo.cs" />
80-
<Compile Include="RijndaelHelper.cs" />
81-
<Compile Include="Servers.cs" />
80+
<Compile Include="Helpers\RijndaelHelper.cs" />
81+
<Compile Include="Controllers\Servers.cs" />
8282
<Compile Include="SettingsException.cs" />
8383
</ItemGroup>
8484
<ItemGroup>
File renamed without changes.

Database/SettingsException.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
42

53
namespace Database
64
{

RemoteDesktopClient/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<configuration>
33
<appSettings>
44
<add key="DatbaseFilepath" value="AppData\servers.s3db"/>
5-
<add key="connection" value="Data Source={0};Version=3;New=False;Compress=True;"/>
5+
<add key="connection" value="Data Source={0};Version=3;New=True;Compress=True;"/>
66
</appSettings>
77
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>
0 Bytes
Binary file not shown.

RemoteDesktopClient/Class Modules/Common Helpers/GlobalHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public static void PopulateGroupsDropDown(ComboBox cb, string selected_text)
7373
{
7474
cb.SelectedIndex = cb.FindStringExact(selected_text);
7575
}
76+
77+
dbGroups.CloseConnection();
7678
}
7779
}
7880
}

RemoteDesktopClient/Controls/NitificationContextMenu.cs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,44 @@ private void AddMenuItems()
2626
{
2727
this.Items.Clear();
2828

29-
GlobalHelper.dbGroups.GetGroupsWithServerCount();
30-
ArrayList groups = GlobalHelper.dbGroups.ArrayListGroups;
31-
ArrayList servers = GlobalHelper.dbServers.ArrayListServers;
29+
//GlobalHelper.dbGroups.GetGroupsWithServerCount();
30+
//ArrayList groups = GlobalHelper.dbGroups.ArrayListGroups;
31+
//ArrayList servers = GlobalHelper.dbServers.ArrayListServers;
3232

33-
ToolStripMenuItem[] menuItemGroups = new ToolStripMenuItem[groups.Count];
33+
//ToolStripMenuItem[] menuItemGroups = new ToolStripMenuItem[groups.Count];
3434

35-
int cnt = 0;
36-
foreach (Model_GroupDetails gd in groups)
37-
{
38-
//if (gd.ServerCount == 0) { continue; }
35+
//int cnt = 0;
36+
//foreach (Model_GroupDetails gd in groups)
37+
//{
38+
// //if (gd.ServerCount == 0) { continue; }
3939

40-
ToolStripMenuItem[] menuItemServers = new ToolStripMenuItem[gd.ServerCount];
41-
int scnt = 0;
42-
foreach (Model_ServerDetails sd in servers)
43-
{
44-
if (gd.GroupID == sd.GroupID)
45-
{
46-
System.Diagnostics.Debug.WriteLine(sd.ServerName);
47-
menuItemServers[scnt] = new ToolStripMenuItem(sd.ServerName, Properties.Resources.mstscax_dll_I345e_0409_16, new EventHandler(Servers_Clicked));
48-
menuItemServers[scnt].Tag = sd;
49-
menuItemServers[scnt].Name = "menuItem" + sd.ServerName.Replace(" ", "_");
50-
scnt++;
51-
}
52-
}
40+
// ToolStripMenuItem[] menuItemServers = new ToolStripMenuItem[gd.ServerCount];
41+
// int scnt = 0;
42+
// foreach (Model_ServerDetails sd in servers)
43+
// {
44+
// if (gd.GroupID == sd.GroupID)
45+
// {
46+
// System.Diagnostics.Debug.WriteLine(sd.ServerName);
47+
// menuItemServers[scnt] = new ToolStripMenuItem(sd.ServerName, Properties.Resources.mstscax_dll_I345e_0409_16, new EventHandler(Servers_Clicked));
48+
// menuItemServers[scnt].Tag = sd;
49+
// menuItemServers[scnt].Name = "menuItem" + sd.ServerName.Replace(" ", "_");
50+
// scnt++;
51+
// }
52+
// }
5353

54-
menuItemGroups[cnt] = new ToolStripMenuItem(gd.GroupName, Properties.Resources.manage_groups_16, menuItemServers);
55-
cnt++;
56-
}
54+
// menuItemGroups[cnt] = new ToolStripMenuItem(gd.GroupName, Properties.Resources.manage_groups_16, menuItemServers);
55+
// cnt++;
56+
//}
5757

58-
this.Items.AddRange(new ToolStripItem[] {
59-
new ToolStripMenuItem("Servers", null, menuItemGroups),
60-
new ToolStripMenuItem("Disconnect All", Properties.Resources.disconnect_all_16, new EventHandler(DisconnectAll_Clicked)),
61-
new ToolStripSeparator(),
62-
new ToolStripMenuItem("Configuration", Properties.Resources.filemgmt_dll_I00ec_0409_16, new EventHandler(Configuration_Clicked)),
63-
new ToolStripSeparator(),
64-
new ToolStripMenuItem("Lock", Properties.Resources.LogOff_16, new EventHandler(Lock_Clicked)),
65-
new ToolStripMenuItem("Exit", Properties.Resources.Shutdown_16, new EventHandler(Exit_Clicked)),
66-
});
58+
//this.Items.AddRange(new ToolStripItem[] {
59+
// new ToolStripMenuItem("Servers", null, menuItemGroups),
60+
// new ToolStripMenuItem("Disconnect All", Properties.Resources.disconnect_all_16, new EventHandler(DisconnectAll_Clicked)),
61+
// new ToolStripSeparator(),
62+
// new ToolStripMenuItem("Configuration", Properties.Resources.filemgmt_dll_I00ec_0409_16, new EventHandler(Configuration_Clicked)),
63+
// new ToolStripSeparator(),
64+
// new ToolStripMenuItem("Lock", Properties.Resources.LogOff_16, new EventHandler(Lock_Clicked)),
65+
// new ToolStripMenuItem("Exit", Properties.Resources.Shutdown_16, new EventHandler(Exit_Clicked)),
66+
//});
6767
}
6868

6969
public void UpdateMenuItems()

RemoteDesktopClient/Forms/ServerSettingsWindow.Designer.cs

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RemoteDesktopClient/Forms/ServerSettingsWindow.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Database.Models;
22
using System;
3+
using System.Diagnostics;
34
using System.Drawing;
5+
using System.Runtime.InteropServices;
46
using System.Windows.Forms;
57
using TextboxRequiredWrappers;
68

@@ -173,19 +175,32 @@ void btnSave_Click(object sender, EventArgs e)
173175
return;
174176
}
175177

176-
Model_ServerDetails sd = new Model_ServerDetails();
177-
sd.GroupID = GlobalHelper.dbGroups.GetIDByGroupName(ddGroup.Text);
178-
sd.ServerName = txServername.Text;
179-
sd.Server = txComputer.Text;
180-
sd.Domain = txDomain.Text;
181-
sd.Port = int.Parse(txPort.Text == string.Empty ? "0" : txPort.Text);
182-
sd.Username = txUsername.Text;
183-
sd.Password = txPassword.Text;
184-
sd.Description = txDescription.Text;
185-
sd.ColorDepth = (int)lblColorDepth.Tag;
186-
sd.DesktopWidth = int.Parse(txWidth.Text);
187-
sd.DesktopHeight = int.Parse(txHeight.Text);
188-
sd.Fullscreen = cbFullscreen.Checked;
178+
int groupId = 0;
179+
180+
groupId = GlobalHelper.dbGroups.GetIDByGroupName(ddGroup.Text);
181+
GlobalHelper.dbGroups.CloseConnection();
182+
183+
if(groupId == 0)
184+
{
185+
MessageBox.Show("Something went wrong while saving to database", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
186+
return;
187+
}
188+
189+
Model_ServerDetails sd = new Model_ServerDetails()
190+
{
191+
GroupID = groupId,
192+
ServerName = txServername.Text,
193+
Server = txComputer.Text,
194+
Domain = txDomain.Text,
195+
Port = int.Parse(txPort.Text == string.Empty ? "0" : txPort.Text),
196+
Username = txUsername.Text,
197+
Password = txPassword.Text,
198+
Description = txDescription.Text,
199+
ColorDepth = (int)lblColorDepth.Tag,
200+
DesktopWidth = int.Parse(txWidth.Text),
201+
DesktopHeight = int.Parse(txHeight.Text),
202+
Fullscreen = cbFullscreen.Checked
203+
};
189204

190205
try
191206
{

0 commit comments

Comments
 (0)