1 Imports System.Security.Cryptography
2 Imports System
3 Imports System.IO
4 Imports System.Net
5 Imports System.Text
6 Imports System.Web
7 Imports System.Data.SqlClient
8 Imports Excel = Microsoft.Office.Interop.Excel
9 Imports System.Net.Mail
10 Module ModFunc
11     Public Function CheckForInternetConnection() As Boolean
12         Try
13             Using client = New WebClient()
14                 Using stream = client.OpenRead(
"http://www.google.com")
15                     Return True
16                 End Using
17             End Using
18         Catch
19             Return False
20         End Try
21     End Function
22     Public Sub GetServerList(ByVal cmbServers As ComboBox)
23         Dim Server As String = String.Empty
24         Dim instance As System.Data.Sql.SqlDataSourceEnumerator = System.Data.Sql.SqlDataSourceEnumerator.Instance
25         Dim table As System.Data.DataTable = instance.GetDataSources()
26         For Each row As System.Data.DataRow In table.Rows
27             Server = String.Empty
28             Server = row(
"ServerName")
29             If row(
"InstanceName").ToString.Length > 0 Then
30                 Server = Server &
"\" & row("InstanceName")
31             End If
32             cmbServers.Items.Add(Server)
33         Next
34         cmbServers.SelectedIndex = cmbServers.FindStringExact(Environment.MachineName)
35     End Sub
36
37     Public Function IsConnectionAvailable() As Boolean
38         Dim objUrl As New System.Uri("
http://www.google.com")
39         Dim objWebReq As System.Net.WebRequest
40         objWebReq = System.Net.WebRequest.Create(objUrl)
41         Dim objresp As System.Net.WebResponse
42
43         Try
44             objresp = objWebReq.GetResponse
45             objresp.Close()
46             objresp = Nothing
47             Return True
48
49         Catch ex As Exception
50             objresp = Nothing
51             objWebReq = Nothing
52             Return False
53         End Try
54     End Function
55     Sub SMSFunc(ByVal st1 As String, ByVal st2 As String, ByVal st3 As String)
56         st3 = st3.Replace("
@MobileNo", st1).Replace("@Message", st2)
57         Dim request As HttpWebRequest
58         Dim response As HttpWebResponse = Nothing
59         Dim myUri As New Uri(st3)
60         request = DirectCast(WebRequest.Create(myUri), HttpWebRequest)
61         response = DirectCast(request.GetResponse(), HttpWebResponse)
62     End Sub
63     Sub LogFunc(ByVal st1 As String, ByVal st2 As String)
64         con = New SqlConnection(cs)
65         con.Open()
66         Dim cb As String = "
insert into Logs(UserID,Date,Operation) VALUES (@d1,@d2,@d3)"
67         cmd = New SqlCommand(cb)
68         cmd.Connection = con
69         cmd.Parameters.AddWithValue("
@d1", st1)
70         cmd.Parameters.AddWithValue("
@d2", System.DateTime.Now)
71         cmd.Parameters.AddWithValue("
@d3", st2)
72         cmd.ExecuteReader()
73         con.Close()
74     End Sub
75     Public Function Encrypt(password As String) As String
76         Dim strmsg As String = String.Empty
77         Dim encode As Byte() = New Byte(password.Length -
1) {}
78         encode = Encoding.UTF8.GetBytes(password)
79         strmsg = Convert.ToBase64String(encode)
80         Return strmsg
81     End Function
82
83     Public Function Decrypt(encryptpwd As String) As String
84         Dim decryptpwd As String = String.Empty
85         Dim encodepwd As New UTF8Encoding()
86         Dim Decode As Decoder = encodepwd.GetDecoder()
87         Dim todecode_byte As Byte() = Convert.FromBase64String(encryptpwd)
88         Dim charCount As Integer = Decode.GetCharCount(todecode_byte,
0, todecode_byte.Length)
89         Dim decoded_char As Char() = New Char(charCount -
1) {}
90         Decode.GetChars(todecode_byte,
0, todecode_byte.Length, decoded_char, 0)
91         decryptpwd = New [String](decoded_char)
92         Return decryptpwd
93     End Function
94     Sub ExportExcel(ByVal st As Object)
95         Dim rowsTotal, colsTotal As Short
96         Dim I, j, iC As Short
97         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
98         Dim xlApp As New Excel.Application
99         Try
100             Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
101             Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(
1), Excel.Worksheet)
102             xlApp.Visible = True
103
104             rowsTotal = st.RowCount
105             colsTotal = st.Columns.Count -
1
106             With excelWorksheet
107                 .Cells.Select()
108                 .Cells.Delete()
109                 For iC =
0 To colsTotal
110                     .Cells(
1, iC + 1).Value = st.Columns(iC).HeaderText
111                 Next
112                 For I =
0 To rowsTotal - 1
113                     For j =
0 To colsTotal
114                         .Cells(I +
2, j + 1).value = st.Rows(I).Cells(j).Value
115                     Next j
116                 Next I
117                 .Rows("
1:1").Font.FontStyle = "Bold"
118                 .Rows("
1:1").Font.Size = 12
119
120                 .Cells.Columns.AutoFit()
121                 .Cells.Select()
122                 .Cells.EntireColumn.AutoFit()
123                 .Cells(
1, 1).Select()
124             End With
125         Catch ex As Exception
126             MessageBox.Show(ex.Message, "
Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
127         Finally
128             'RELEASE ALLOACTED RESOURCES
129             System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
130             xlApp = Nothing
131         End Try
132     End Sub
133     Public Sub SendMail(ByVal s1 As String, ByVal s2 As String, ByVal s3 As String, ByVal s4 As String, ByVal s5 As String, ByVal s6 As String, ByVal s7 As Integer, ByVal s8 As String, ByVal s9 As String)
134         Dim msg As New MailMessage()
135         Try
136             msg.From = New MailAddress(s1)
137             msg.[To].Add(s2)
138             msg.Body = s3
139             msg.Attachments.Add(New Attachment(s4))
140             msg.IsBodyHtml = True
141             msg.Subject = s5
142             Dim smt As New SmtpClient(s6)
143             smt.Port = s7
144             smt.Credentials = New NetworkCredential(s8, s9)
145             smt.EnableSsl = True
146             smt.Send(msg)
147         Catch ex As Exception
148             MessageBox.Show(ex.Message, "
Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
149         End Try
150     End Sub
151     Sub LedgerSave(ByVal a As DateTime, ByVal b As String, ByVal c As String, ByVal d As String, ByVal e As Decimal, ByVal f As Decimal, ByVal g As String)
152         con = New SqlConnection(cs)
153         con.Open()
154         Dim cb As String = "
insert into LedgerBook(Date, Name, LedgerNo, Label,Debit,Credit,PartyID) Values (@d1,@d2,@d3,@d4,@d5,@d6,@d7)"
155         cmd = New SqlCommand(cb)
156         cmd.Parameters.AddWithValue("
@d1", a)
157         cmd.Parameters.AddWithValue("
@d2", b)
158         cmd.Parameters.AddWithValue("
@d3", c)
159         cmd.Parameters.AddWithValue("
@d4", d)
160         cmd.Parameters.AddWithValue("
@d5", e)
161         cmd.Parameters.AddWithValue("
@d6", f)
162         cmd.Parameters.AddWithValue("
@d7", g)
163         cmd.Connection = con
164         cmd.ExecuteReader()
165         con.Close()
166     End Sub
167     Sub LedgerDelete(ByVal a As String, ByVal b As String)
168         con = New SqlConnection(cs)
169         con.Open()
170         Dim cq As String = "
delete from LedgerBook where LedgerNo=@d1 and Label=@d2"
171         cmd = New SqlCommand(cq)
172         cmd.Parameters.AddWithValue("
@d1", a)
173         cmd.Parameters.AddWithValue("
@d2", b)
174         cmd.Connection = con
175         cmd.ExecuteReader()
176         con.Close()
177     End Sub
178     Sub LedgerUpdate(ByVal a As DateTime, ByVal b As String, ByVal e As Decimal, ByVal f As Decimal, ByVal g As String, ByVal h As String, ByVal i As String)
179         con = New SqlConnection(cs)
180         con.Open()
181         Dim cb As String = "
Update LedgerBook set Date=@d1, Name=@d2,Debit=@d3,Credit=@d4,PartyID=@d5 where LedgerNo=@d6 and Label=@d7"
182         cmd = New SqlCommand(cb)
183         cmd.Parameters.AddWithValue("
@d1", a)
184         cmd.Parameters.AddWithValue("
@d2", b)
185         cmd.Parameters.AddWithValue("
@d3", e)
186         cmd.Parameters.AddWithValue("
@d4", f)
187         cmd.Parameters.AddWithValue("
@d5", g)
188         cmd.Parameters.AddWithValue("
@d6", h)
189         cmd.Parameters.AddWithValue("
@d7", i)
190         cmd.Connection = con
191         cmd.ExecuteReader()
192         con.Close()
193     End Sub
194     Public Function HandleRegistry() As Boolean
195         Dim firstRunDate As Date
196         Dim st As Date = My.Computer.Registry.GetValue("
HKEY_LOCAL_MACHINE\SOFTWARE\SchoolERP1", "Set", Nothing)
197         firstRunDate = st
198         If firstRunDate = Nothing Then
199             firstRunDate = System.DateTime.Today.Date
200             My.Computer.Registry.SetValue("
HKEY_LOCAL_MACHINE\SOFTWARE\SchoolERP1", "Set", firstRunDate)
201         ElseIf (Now - firstRunDate).Days >
7 Then
202             Return False
203         End If
204         Return True
205     End Function
206
207 End Module


Gõ tìm kiếm nhanh...