-
Notifications
You must be signed in to change notification settings - Fork 0
/
Login.cs
262 lines (212 loc) · 9.28 KB
/
Login.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace InstantChat
{
public partial class Login : Form
{
SqlConnection con;
bool validUser1 = false, validUser2;
int i;
public Login()
{
InitializeComponent();
}
protected override void OnClosing(CancelEventArgs e)
{
Application.Exit();
base.OnClosing(e);
}
private void btnNetworkSetting_Click(object sender, EventArgs e)
{
NetworkSetting frmNetworkSettings = new NetworkSetting();
frmNetworkSettings.ShowDialog();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUsername.Text == "")
{
MessageBox.Show("Please enter Login Name");
txtUsername.Focus();
return;
}
if (txtPassword.Text == "")
{
MessageBox.Show("Please enter Password");
txtPassword.Focus();
return;
}
if (cbType.Text == "")
{
MessageBox.Show("Please Select the Type");
cbType.Focus();
return;
}
if (File.Exists("NetSet.txt"))
{
FileStream fs = new FileStream("NetSet.txt", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
TempClass.hostIP = br.ReadString();
fs.Close();
br.Close();
/*This code will connect InstantChat project with Chat database,
created in Microsoft SQL Server 2005 having SQL authentication.
User has to change this connection code as per his compuer node.*/
TempClass.connectionString = ("Data Source=" + TempClass.hostIP + ";Initial Catalog=Chat;User ID=sa");
}
else
{
NetworkSetting serverAdd = new NetworkSetting();
serverAdd.Show();
}
if (TempClass.hostIP != "")
{
try
{
con = new System.Data.SqlClient.SqlConnection(TempClass.connectionString);
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select * From tbCreateAccount", con);
DataSet ds = new DataSet();
adp.Fill(ds, "tbCreateAccount");
if (ds.Tables.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
if ((dr["loginName"].ToString() == txtUsername.Text) && (dr["passwd"].ToString() == txtPassword.Text) && (dr["type"].ToString() == cbType.SelectedItem.ToString()) && (dr["type"].ToString() == "Administrator"))
{
i = 1;
break;
}
else if ((dr["loginName"].ToString() == txtUsername.Text) && (dr["passwd"].ToString() == txtPassword.Text) && (dr["type"].ToString() == cbType.SelectedItem.ToString()) && (dr["type"].ToString() == "User"))
{
i = 2;
break;
}
else
{
continue;
}
}
}
if (i == 1 || i == 2)
{
SqlDataAdapter adp2 = new SqlDataAdapter("Select * From tbAddContact", con);
DataSet ds2 = new DataSet();
adp2.Fill(ds2, "tbAddContact");
if (ds2.Tables.Count > 0)
{
foreach (DataRow dr2 in ds2.Tables[0].Rows)
{
if (dr2["loginName"].ToString() == txtUsername.Text)
{
validUser1 = true;
break;
}
else
{
continue;
}
}
}
SqlDataAdapter adp4 = new SqlDataAdapter("Select * From tbContactList", con);
DataSet ds4 = new DataSet();
adp4.Fill(ds4, "tbContactList");
if (ds4.Tables.Count > 0)
{
foreach (DataRow dr in ds4.Tables[0].Rows)
{
if ((dr["loginName"].ToString() == txtUsername.Text) && (dr["userStatus"].ToString() == "Offline"))
{
validUser2 = true;
break;
}
else
{
continue;
}
}
}
if (validUser2 == true)
{
SqlCommand SqlCommand1 = con.CreateCommand();
SqlCommand1.CommandText = "UPDATE tbContactList SET userStatus=@userStatus WHERE loginName=@loginname";
SqlCommand1.Parameters.Add("@userStatus", SqlDbType.VarChar, 10);
SqlCommand1.Parameters.Add("@loginname", SqlDbType.VarChar, 100);
SqlCommand1.Parameters[0].Value = "Online";
SqlCommand1.Parameters[1].Value = txtUsername.Text;
SqlCommand1.ExecuteNonQuery();
}
if (validUser1 == true)
{
MessageBox.Show("User already Logged In");
con.Close();
TempClass.username = txtUsername.Text;
if (i == 1)
{
AdminStatus cs = new AdminStatus();
TempClass.admin = cs;
this.Hide();
TempClass.admin.Show();
}
if (i == 2)
{
UserStatus cs = new UserStatus();
TempClass.user = cs;
this.Hide();
TempClass.user.Show();
}
}
if (validUser1 == false)
{
SqlDataAdapter adp3 = new SqlDataAdapter("select * from tbAddContact", con);
DataSet ds3 = new DataSet();
adp3.Fill(ds3, "tbAddContact");
int var = ds3.Tables["tbAddContact"].Rows.Count;
DataTable table = null;
table = ds3.Tables["tbAddContact"];
DataRow newRow = null;
newRow = table.NewRow();
newRow["loginName"] = txtUsername.Text;
table.Rows.Add(newRow);
SqlCommandBuilder commandBuilder = null;
commandBuilder = new SqlCommandBuilder(adp3);
adp3.InsertCommand = commandBuilder.GetInsertCommand();
adp3.Update(ds3, "tbAddContact");
con.Close();
TempClass.username = txtUsername.Text;
if (i == 1)
{
AdminStatus cs = new AdminStatus();
TempClass.admin = cs;
this.Hide();
TempClass.admin.Show();
}
if (i == 2)
{
UserStatus cs = new UserStatus();
TempClass.user = cs;
this.Hide();
TempClass.user.Show();
}
}
}
if (i == 0)
{
MessageBox.Show("Provided information does not match.\nPlease Check Your Data");
}
}
catch (Exception ex)
{
MessageBox.Show("Provided information does not match.\nPlease Check Your Data");
}
}
}
}
}