BestChange - exchange monitor, earn as an affiliate!

Showing posts with label code snippet. Show all posts
Showing posts with label code snippet. Show all posts

Thursday, July 19, 2012

Encryption


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Public Function SimpleCrypt(ByVal Text As String) As String
' Encrypts/decrypts the passed string using 
' a simple ASCII value-swapping algorithm
Dim strTempChar As String, i As Integer
For i = 1 To Len(Text)
  If Asc(Mid$(Text, i, 1)) < 128 Then
    strTempChar = _
CType(Asc(Mid$(Text, i, 1)) + 128, String)
  ElseIf Asc(Mid$(Text, i, 1)) > 128 Then
    strTempChar = _
CType(Asc(Mid$(Text, i, 1)) - 128, String)
  End If
  Mid$(Text, i, 1) = _
      Chr(CType(strTempChar, Integer))
Next i
Return Text
End Function


Tuesday, June 26, 2012

Get values of selected row in Datagrid

1
2
3
4
5
6
    Protected Sub gvPhonebook_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles gvPhonebook.SelectedIndexChanged
Dim i As Integer = gvPhonebook.SelectedRow.RowIndex
txtGSMNo.Text = gvPhonebook.Rows(i).DataItem("gsmno")
txtName.Text = gvPhonebook.Rows(i).DataItem("name")
ddlTeams.Text = gvPhonebook.Rows(i).DataItem("team")
End Sub

Read Excel Cells and Rows

 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
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim oExcel As Object
oExcel = CreateObject("Excel.Application")
Dim wbk As Object

wbk = oExcel.Workbooks.Open(Filename:="C:\Users\Dev1\Downloads\11-21-2011\Quest Technology Report - InVoice Account Daily_13-12-01-2011.xls", UpdateLinks:=False, ReadOnly:=False)
Dim firstletter As Integer = 65 '65 =A 90 Z
Dim secondletter As Integer = 0


' "Zipp Technologies LTD" last


objConn.open()
'For i As Integer = 5089 To 5937
For i As Integer = 4 To 102

With wbk.ActiveSheet

'For j = 2 To 5
firstletter = firstletter + 1

'If Len((.Range(Chr(firstletter) & "1").value)) > 0 Then

ListBox1.Items.Add(.Cells(i, 2).Value & "-" & formatDate(.Cells(i, 3).Value))

Dim CountSQl As String = "select * from invoice where accountno = '" & RTrim(.Cells(i, 2).Value) & "' and dateissue = '" & formatDate(.Cells(i, 3).Value) & "' and productcode = '" & .Cells(i, 9).Value & "'"

Dim cmd As OdbcCommand = New OdbcCommand(CountSQl, objConn)
Dim reader As OdbcDataReader
reader = cmd.ExecuteReader()


If reader.Read() Then
txtAccountNo.Text &= (.Cells(i, 2).Value)
'insertInvoice(.Cells(i, 2).Value, formatDate(.Cells(i, 3).Value), .Cells(i, 4).Value, .Cells(i, 5).Value, .Cells(i, 7).Value, .Cells(i, 8).Value, .Cells(i, 9).Value, .Cells(i, 11).Value)
Else
insertInvoice(RTrim(.Cells(i, 2).Value), formatDate(.Cells(i, 3).Value), .Cells(i, 4).Value, .Cells(i, 5).Value, .Cells(i, 7).Value, .Cells(i, 8).Value, .Cells(i, 9).Value, .Cells(i, 10).Value, .Cells(i, 11).Value)

End If




reader.Close()


'insertSimorder(.Cells(i, 1).Value, .Cells(i, 2).Value, .Cells(i, 3).Value, .Cells(i, 4).Value, .Cells(i, 5).Value, .Cells(i, 6).Value, .Cells(i, 7).Value, .Cells(i, 8).Value, .Cells(i, 9).Value, .Cells(i, 10).Value, .Cells(i, 11).Value, .Cells(i, 12).Value, .Cells(i, 13).Value, .Cells(i, 14).Value, "0000-00-00", .Cells(i, 16).Value, formatDate(.Cells(i, 17).Value), .Cells(i, 18).Value, .Cells(i, 19).Value)

'updateAcctMgr(.Range(Chr(firstletter) & "29").value(), searchClientID(.Range(Chr(firstletter) & "1").value()).Tables(0).Rows(0).Item("carrierDetailsID"))
'insertSimrequest(.Cells(i, 2).Value, .Cells(i, 3).Value, .Cells(i, 4).Value, .Cells(i, 5).Value, .Cells(i, 6).Value, .Cells(i, 7).Value, .Cells(i, 8).Value, .Cells(i, 9).Value, .Cells(i, 10).Value, .Cells(i, 13).Value, formatDate(.Cells(i, 19).Value), formatDate(.Cells(i, 21).Value), .Cells(i, 22).Value, .Cells(i, 23).Value, .Cells(i, 14).Value)
'insertSimrequest(.Cells(i, 2).Value, .Cells(i, 3).Value, .Cells(i, 4).Value, .Cells(i, 5).Value, .Cells(i, 6).Value, .Cells(i, 7).Value, .Cells(i, 8).Value, .Cells(i, 9).Value, .Cells(i, 10).Value, .Cells(i, 13).Value, formatDate(.Cells(i, 19).Value), formatDate(.Cells(i, 21).Value), .Cells(i, 22).Value, .Cells(i, 23).Value, .Cells(i, 15).Value)
'insertSimrequest(.Cells(i, 2).Value, .Cells(i, 3).Value, .Cells(i, 4).Value, .Cells(i, 5).Value, .Cells(i, 6).Value, .Cells(i, 7).Value, .Cells(i, 8).Value, .Cells(i, 9).Value, .Cells(i, 10).Value, .Cells(i, 13).Value, formatDate(.Cells(i, 19).Value), formatDate(.Cells(i, 21).Value), .Cells(i, 22).Value, .Cells(i, 23).Value, .Cells(i, 16).Value)


'End If


If firstletter = 90 Then
firstletter = 65
If secondletter = 0 Then secondletter = 65

If secondletter > 0 Then
secondletter = secondletter + 1
End If

End If


'Next

End With



Next
oExcel.Workbooks.close()
objConn.close()


MsgBox("done")



End Sub

Formatting Date

1
2
3
4
5
6
7
8
    Public Function formatDate(ByVal strDate As String) As String
' Dim now As DateTime = Convert.ToDateTime(strDate)
Dim newDate As String

newDate = Format(CDate(strDate), "yyyy-MM-dd HH:mm:ss")

Return newDate
End Function

Database Connection

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
     Public Function MyDataSet(ByVal strSQL As String) As DataSet
Dim ds As New DataSet
Dim mycmd As MySqlCommand = New MySqlCommand(strSQL, objConn)
Dim ad As MySqlDataAdapter = New MySqlDataAdapter
ad.SelectCommand = mycmd
objConn.Open()
ad.Fill(ds, "a")
objConn.Close()
Return ds
End Function

Public Function MsDataset(ByVal strSQL As String, ByVal strConn As String) As DataSet
Dim ds As New DataSet
Dim newStrConn As New SqlConnection(strConn)
Dim sqlcmd As SqlCommand = New SqlCommand(strSQL, newStrConn)
Dim ad As SqlDataAdapter = New SqlDataAdapter
ad.SelectCommand = sqlcmd
newStrConn.Open()
ad.Fill(ds, "a")
newStrConn.Close()
Return ds
End Function

Thursday, August 11, 2011

Table Schema

  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
Imports System.Data
Imports System.Data.Odbc
Imports System.IO

Public Class Table_Schema

Dim objConn As OdbcConnection
Dim objCmd As OdbcCommand

Private Sub Table_Schema_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim strConnString As String
strConnString = "Driver={MySQL ODBC 5.1 Driver};database=information_schema;option=3;server=5.46.208.197;port=3306;uid=app.user;pwd=user.1275"
objConn = New OdbcConnection(strConnString)
objConn.Open()
'insertClientName("grace")

' MsgBox(searchClientID("012 Smile").Tables(0).Rows.Count)
End Sub

'***insert client name***
Public Function insertClientName(ByVal clientname As String) As DataSet
Dim sql As String = "INSERT INTO carrierdetails (clientName) values ('" & clientname & "')"
Return sqldataset(sql)
End Function

Public Function sqldataset(ByVal sqlstr As String) As DataSet
Dim ds As New DataSet
Dim ssql As String = (sqlstr)
Dim cmd As OdbcCommand = New OdbcCommand(ssql, objConn)
Dim ad As OdbcDataAdapter = New OdbcDataAdapter
ad.SelectCommand = cmd
'objConn.Open()
ad.Fill(ds, "a")
objConn.Close()
Return ds
End Function

'***seach client ID***
Public Function searchClientID(ByVal clientname As String) As DataSet
Dim sql As String = "SELECT carrierDetailsID FROM carrierDetails WHERE clientname = '" & clientname & "'"
Return sqldataset(sql)
End Function


Private Sub btnMagic_Click(sender As System.Object, e As System.EventArgs) Handles btnMagic.Click
Dim table As String = txtTable.Text

txtInsert.Text = insertQuery(table)
txtUpdate.Text = updateQuery(table)
txtFunction.Text = functionQuery(table)


End Sub

Public Function insertQuery(ByVal table As String) As String
'*********INSERT*********'
Dim f As String = "("
Dim v As String = "("
Dim sql As String = "select column_name from columns where table_name='" & table & "'"
Dim insert As String = "INSERT INTO " & table & " "
Dim thisCommand As New OdbcCommand(sql, objConn)


Dim thisReader As OdbcDataReader = thisCommand.ExecuteReader()

While thisReader.Read
f = f & thisReader.GetString(0) & ", "
v = v & "'" & Chr(34) & " & " & thisReader.GetString(0) & " & " & Chr(34) & "', "
End While

thisReader.Close()

f = f.Remove(f.LastIndexOf(","), 1)
v = v.Remove(v.LastIndexOf(","), 1)
f = f & ")"
v = v & ")"

Dim fullinsert As String
fullinsert = insert & f & " values " & v

Return fullinsert

End Function

Public Function updateQuery(ByVal table As String) As String
'********UPDATE********'
Dim f As String

Dim sql As String = "select column_name from columns where table_name='" & table & "'"
Dim update As String = "UPDATE " & table & " SET "
Dim thisCommand As New OdbcCommand(sql, objConn)

Dim thisReader As OdbcDataReader = thisCommand.ExecuteReader()

While thisReader.Read
f = f & thisReader.GetString(0) & " = '" & Chr(34) & " & " & thisReader.GetString(0) & " & " & Chr(34) & "', "
End While

thisReader.Close()

f = f.Remove(f.LastIndexOf(","), 1)

Dim fullupdate As String
fullupdate = update & f & " WHERE "
Return fullupdate

End Function

Public Function functionQuery(ByVal table As String) As String
Dim f As String = "("

Dim sql As String = "select column_name from columns where table_name='" & table & "'"
Dim sqlfunction As String = "Public Function " & table
Dim thisCommand As New OdbcCommand(sql, objConn)

Dim thisReader As OdbcDataReader = thisCommand.ExecuteReader()

While thisReader.Read
f = f & "ByVal " & thisReader.GetString(0) & " As String, "
End While

thisReader.Close()

f = f.Remove(f.LastIndexOf(","), 1)

Dim fullfunction As String
fullfunction = sqlfunction & f & ") As DataSet"

Return fullfunction

End Function

End Class