-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBus.cs
265 lines (248 loc) · 10.6 KB
/
Bus.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
263
264
265
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace BusReservationSystem.Classes
{
class Bus
{
//Connection String [for connecting with database]
private readonly string conStr = ConfigurationManager.ConnectionStrings["BTRS_Db"].ConnectionString;
public DataTable GetBuses()
{
DataTable dataTable = new DataTable();
try
{
using (SqlConnection con = new SqlConnection(conStr))
{
SqlDataAdapter da = new SqlDataAdapter();
//Select All Busses based on counter
string sql = "select b.[no], t.[type], b.[total_capacity], b.[avail_seat], bc.[date], bc.[time]"
+ "from [Bus] b "
+ "join [Bus_Type] t "
+ "on t.[id] = b.[type] "
+ "full outer join [Bus_Counter] bc "
+ "on b.[no] = bc.[bus_no] "
+ "where bc.[counter_name] = @currentCounter";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@currentCounter", LogInfo.user_counter);
da.SelectCommand = cmd;
da.Fill(dataTable);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
//Get List of Busses from Database & return as DataTable
return dataTable;
}
public DataTable GetBuses(string destination, int busType, DateTime busDate)
{
//Get List of Busses from Database & return as DataTable
//For searching Busses with requirments
DataTable dataTable = new DataTable();
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select b.[no], t.[type], b.[total_capacity], b.[avail_seat], bc.[date], bc.[time] "
+ "from [Bus] b "
+ "join [Bus_Type] t "
+ "on t.[id] = b.[type] "
+ "full outer join [Bus_Counter] bc "
+ "on b.[no] = bc.[bus_no] "
+ "where bc.[counter_name] = @currentCounter "
+ "and b.[type] = @busType "
+ "and bc.[date] = @busDate "
+ "and bc.[bus_no] = "
+ "any( "
+ "select [Bus_Counter].[bus_no] "
+ "from [Bus_Counter] "
+ "where counter_name like @counterName )";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@currentCounter", LogInfo.user_counter);
cmd.Parameters.AddWithValue("@counterName", destination + "%");
cmd.Parameters.AddWithValue("@busType", busType);
cmd.Parameters.AddWithValue("@busDate", busDate.ToShortDateString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dataTable);
}
return dataTable;
}
public bool DelBus(string busNo)
{
bool status = false;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "Delete from [Bus] where [no] = @busNo";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@busNo", busNo);
con.Open();
if (cmd.ExecuteNonQuery() > 0)
status = true;
}
return status;
}
public DataTable GetBusType()
{
//Get Bus Type
DataTable data = new DataTable();
using (SqlConnection sqlCon = new SqlConnection(conStr))
{
string sql = "select * from [Bus_Type]";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
//cmd.Parameters.AddWithValue("@busNo", busNo);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(data);
}
return data;
}
public DataTable GetPassenger(string busNo,string counter, int ticket)
{
DataTable data = new DataTable();
using (SqlConnection sqlCon = new SqlConnection(conStr))
{
string sql = "select * from [Passenger] where bus_no = @busNo and counter = @counter and ticket_no = @ticket";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
cmd.Parameters.AddWithValue("@busNo", busNo);
cmd.Parameters.AddWithValue("@counter", counter);
cmd.Parameters.AddWithValue("@ticket", ticket);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(data);
}
return data;
}
public DataTable GetPessangers(string busNo)
{
//Get Pessanger List for speacific bus
DataTable data = new DataTable();
using (SqlConnection sqlCon = new SqlConnection(conStr))
{
string sql = "select * from [Passenger] where bus_no = @busNo";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
cmd.Parameters.AddWithValue("@busNo", busNo);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(data);
}
return data;
}
public int CountPessanger(string busNo)
{
int count;
using (SqlConnection sqlCon = new SqlConnection(conStr))
{
DataTable data = new DataTable();
string sql = "select count(*) as [counts] from [Passenger] where bus_no = @busNo";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
cmd.Parameters.AddWithValue("@busNo", busNo);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(data);
count = Int32.Parse(data.Rows[0][0].ToString());
}
return count;
}
public bool SetPassenger(Passenger p)
{
bool b;
using (SqlConnection sqlCon = new SqlConnection(conStr))
{
string sql = "Insert into [Passenger] values "
+ "( "
+ p.ticket_no + ", '"
+ p.full_name + "', '"
+ p.phone_no + "', '"
+ p.bus_no + "', '"
+ p.start_loc + "', '"
+ p.end_loc + "', "
+ p.seat_length + ", '"
+ p.counter + "', "
+ p.GetPrice() + " )";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
sqlCon.Open();
if (cmd.ExecuteNonQuery() > 0)
{
string sqlUpdate = "Update [Bus] set [avail_seat] = "
+ (GetAvailSeat(p.bus_no) - p.seat_length)
+ "where [no] = '" + p.bus_no+"'";
SqlCommand cmd2 = new SqlCommand(sqlUpdate, sqlCon);
cmd2.ExecuteNonQuery();
b = true;
}
else
b = false;
}
return b;
}
public bool DeletePassenger(Passenger p)
{
bool b;
using (SqlConnection sqlCon = new SqlConnection(conStr))
{
//Deleting Passenger Info
string sql = "Delete from [Passenger] where [bus_no] = @busNo and [ticket_no] = @ticketNo and [counter] = @counter";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
cmd.Parameters.AddWithValue("@busNo", p.bus_no);
cmd.Parameters.AddWithValue("@counter", p.counter);
cmd.Parameters.AddWithValue("@ticketNo", p.ticket_no);
sqlCon.Open();
if (cmd.ExecuteNonQuery() > 0)
{
//Updating Avail Seat
string sqlUpdate = "Update [Bus] set [avail_seat] = "
+ (GetAvailSeat(p.bus_no) + p.seat_length)
+ "where [no] = '" + p.bus_no + "'";
SqlCommand cmd2 = new SqlCommand(sqlUpdate, sqlCon);
cmd2.ExecuteNonQuery();
b = true;
}
else
b = false;
}
return b;
}
public DateTime GetTime(string busNo, string counterName, DateTime? date)
{
DateTime date1, time1 ;
using (SqlConnection sqlConnection = new SqlConnection(conStr))
{
string sql = "select [date], [time] from [Bus_Counter] where [counter_name] = @counterName and [bus_no] = @busNo";
SqlCommand cmd = new SqlCommand(sql, sqlConnection);
cmd.Parameters.AddWithValue("@counterName", counterName);
cmd.Parameters.AddWithValue("@busNo", busNo);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable data = new DataTable();
adapter.Fill(data);
date1 = DateTime.Parse(data.Rows[0][0].ToString());
time1 = DateTime.Parse(data.Rows[0][1].ToString());
}
return date1.Add(time1.TimeOfDay);
}
public DataTable GetConters(string search)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "SELECT [counter_name] as [name] FROM [Bus_Counter] where [Bus_Counter].[bus_no] = @searchText";
SqlCommand command = new SqlCommand(sql, con);
command.Parameters.AddWithValue("@searchText", search);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dt);
}
return dt;
}
public int GetAvailSeat(string busNo)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select [avail_seat] from [Bus] where [no] = '" +busNo+"'";
SqlCommand command = new SqlCommand(sql, con);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dt);
}
return int.Parse(dt.Rows[0][0].ToString());
}
}
}