-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddBook.cs
231 lines (218 loc) · 8.91 KB
/
AddBook.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;
namespace Library_Management_Syatem
{
public partial class AddBook : Form
{
DataTable dt1 = new DataTable();
SqlCommand cmd;
SqlDataAdapter ad;
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-9CRFK3A;Initial Catalog=Library_Management_System;Integrated Security=True");
DataTable dt = new DataTable();
Thread th;
public AddBook()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
th = new Thread(openhomeform);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
public void openhomeform()
{
Application.Run(new Home());
}
private void updatecopies()
{
int copy = int.Parse(dt1.Rows[0]["Copies"].ToString()) + int.Parse(textBoxaddbookcopies.Text);
con.Open();
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Update Book_Table set Copies=" + copy + " where Book_ISBN='" + textBoxaddbookisbn.Text + "'";
cmd.ExecuteNonQuery();
MessageBox.Show("Book Copies Updated Successfully!");
con.Close();
}
private void insertupdateintoactivitylog()
{
con.Open();
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into ActivityLog_Table values(Default,'More Copies of Book with ISBN(" + textBoxaddbookisbn.Text + ") is Added by Vendor (" + textBoxvendorname.Text + ").')";
cmd.ExecuteNonQuery();
con.Close();
}
private void btnaddbook_Click(object sender, EventArgs e)
{
if (textBoxaddbookisbn.Text == "")
{
MessageBox.Show("Please Enter Book ISBN");
}
else if (textBoxaddbooktitle.Text == "")
{
MessageBox.Show("Please Enter Book Title");
}
else if (textBoxaddbookauthor1.Text == "")
{
MessageBox.Show("Please Enter Author Name");
}
else if (textBoxaddbookpages.Text == "")
{
MessageBox.Show("Please Enter Number of Pages");
}
else if (textBoxaddbookprice.Text == "")
{
MessageBox.Show("Please Enter Book Price");
}
else if (textBoxaddbookcopies.Text == "")
{
MessageBox.Show("Please Enter Number of Copies");
}
else if (textBoxaddbookisbn.Text.Length > 50 || textBoxaddbookisbn.Text.Length < 3)
{
MessageBox.Show("ISBN length must be between 3-50");
}
else if (textBoxaddbooktitle.Text.Length > 50)
{
MessageBox.Show("Title length must be <50");
}
else if (textBoxaddbookauthor1.Text.Length > 50)
{
MessageBox.Show("Author Name length must be <50");
}
else if (int.Parse(textBoxaddbookcopies.Text) <= 2)
{
MessageBox.Show("Copies must be > 2");
}
else
{
Check();
if (dt1.Rows.Count == 1)
{
DialogResult dr=MessageBox.Show("ISBN is allocated to another Book!\n\nDo You Want to Increase its Copies?","Alert",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
updatecopies();
insertupdateintoactivitylog();
this.Close();
th = new Thread(openaddbookform);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
else
{
this.Close();
th = new Thread(openaddbookform);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
}
else
{
con.Open();
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Book_Table values('" + textBoxaddbookisbn.Text + "','" + textBoxaddbooktitle.Text + "','" + textBoxaddbookauthor1.Text + "'," + int.Parse(textBoxaddbookpages.Text) + "," + int.Parse(textBoxaddbookprice.Text) + "," + int.Parse(textBoxaddbookcopies.Text) + ")";
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Book Added Successfully");
insertintoSupplyTable();
insertintoactivitylog();
this.Close();
th = new Thread(openaddbookform);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
}
}
private void insertintoactivitylog()
{
con.Open();
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into ActivityLog_Table values(Default,'New Book with ISBN(" + textBoxaddbookisbn.Text + ") is Added by Vendor ("+textBoxvendorname.Text+").')";
cmd.ExecuteNonQuery();
con.Close();
}
private void Check()
{
con.Open();
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Book_Table where Book_ISBN='" + textBoxaddbookisbn.Text + "'";
cmd.ExecuteNonQuery();
ad = new SqlDataAdapter(cmd);
ad.Fill(dt1);
con.Close();
}
private void insertintoSupplyTable()
{
con.Open();
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into SupplyBook_Table values(" + int.Parse(textBoxvendorid.Text) + ",'" + textBoxaddbookisbn.Text + "',Default," + int.Parse(textBoxaddbookcopies.Text) + "," + (int.Parse(textBoxaddbookcopies.Text) * int.Parse(textBoxaddbookprice.Text)) + ")";
cmd.ExecuteNonQuery();
con.Close();
}
private void openaddbookform()
{
Application.Run(new AddBook());
}
private void btnvendorsearch_Click(object sender, EventArgs e)
{
con.Open();
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * From Vendor_Table where Vendor_ID='" + int.Parse(textBoxvendorid.Text) + "'";
cmd.ExecuteNonQuery();
ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count == 0)
{
DialogResult dr=MessageBox.Show("Vendor not exists\nDo you want to add new Vendor","Vendor",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (dr == DialogResult.No)
{
this.Close();
th = new Thread(openaddbookform);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
else
{
AddVendor v = new AddVendor();
v.Show();
}
}
else
{
btnaddbook.Visible = true;
btnvendorsearch.Visible = false;
textBoxvendorid.Visible = false;
labelvendorid.Visible = false;
textBoxvendorname.Visible = true;
textBoxvendoraddress.Visible = true;
textBoxvendorphoneno.Visible = true;
labelvendorname.Visible = true;
labelvendoraddress.Visible = true;
labelvendorphoneno.Visible = true;
textBoxvendorid.Text = dt.Rows[0]["Vendor_ID"].ToString();
textBoxvendorname.Text = dt.Rows[0]["Vendor_Name"].ToString();
textBoxvendoraddress.Text = dt.Rows[0]["Vendor_Address"].ToString();
textBoxvendorphoneno.Text = dt.Rows[0]["Phone_Number"].ToString();
}
con.Close();
}
}
}