Monday, August 30, 2010

VB.NET Export GridView to EXCEL

//FIRST WAY

If GridView1.Rows.Count.ToString + 1 < 65536 Then
GridView1.AllowPaging = "False"
bindgrid1()
Dim tw As New StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim frm As HtmlForm = New HtmlForm()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=" & txtfile.Text & ".xls") Response.Charset = "" EnableViewState = False Controls.Add(frm) frm.Controls.Add(GridView1) frm.RenderControl(hw) Response.Write(tw.ToString()) Response.End()
GridView1.AllowPaging = "True"
bindgrid1()
Else
lblerror.Text = "Too many rows - Export to Excel not possible"
End If

//SECOND WAY


Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

DataGrid1.RenderControl(oHtmlTextWriter)

Response.Write(oStringWriter.ToString())
Response.[End]()

End Sub

Friday, August 27, 2010

VB6.0 Table EXPORT to EXCEL

Private Sub Command3_Click()

Dim db As Database
Set db = OpenDatabase(App.Path & "\db1.mdb")
db.Execute "SELECT * INTO [Excel 8.0;DATABASE=d:\test.XLS].[atmmsttable] FROM [atmmstdatabase]"
Set db = Nothing
MsgBox "Saved XLS to D:\test.xls", vbInformation, "Complete"


End Sub

VB6.0 Changing Random Color

Private Sub Timer2_Timer()

Static A
A = A + 10: If A > 510 Then A = 0
//To change the effect, put other numbers insted of the 0 below.
//You can also replace places between the 'Abs(A - 256)' and one of thezeros below.
//To decrease the pace of the animation, increase the Timer intervalproperty.
cmdadditem.BackColor = RGB(Abs(A - 256), 100, 100)
cmdaddch.BackColor = RGB(Abs(A - 256), 50, 200)
cmdaddtocurrent.BackColor = RGB(Abs(A - 256), 150, 50)

End Sub

VB6.0 Remove Duplicates From ListBox

Private Sub Command6_Click()

Dim i As Long
Dim j As Long
With List1
For i = 0 To .ListCount - 1
For j = .ListCount To (i + 1) Step -1
If .List(j) = .List(i) Then
.RemoveItem j
End If
Next
Next
End With

Dim k As Long
Dim l As Long
With List2
For k = 0 To .ListCount - 1
For l = .ListCount To (k + 1) Step -1
If .List(l) = .List(k) Then
.RemoveItem l
End If
Next
Next
End With

End Sub

VB6.0 Fill ListBox with Column Values

Call cnOPEN //ConnectionString Function
rs.Open "select * from mainmaster", cn, adOpenDynamic, adLockOptimistic
rs.MoveFirst
Do While Not rs.EOF
List1.AddItem rs!mainpager
rs.MoveNext
Loop

VB6.0 MSAccess ConnectionString with Database Password

Public Sub cnOPEN()
mdbpassword = "k12i07s2002s"
mdbname = "bojpr2010.mdb"
If cn.State = 1 Then
cn.Close
End If
cn.Mode = adModeReadWrite
cn.Open "Provider=MSDataShape.1;Data Provider = Microsoft.Jet.OLEDB.3.51;Jet OLEDB:Database Password=" & mdbpassword & ";data source=" & App.Path & "\" & mdbname
End Sub

VB6.0 Work with FTP

//go to Project->Components->Select 'Microsoft Internet Transfer Controls 6.0(sp4)' and - 'Microsoft Internet Control'
//Drag Inet and webbrowser control

//Open FTP

WebBrowser1.Navigate "ftp://rpc:rpc123@sdmftp.com"
//where rpc and rpc123 are username and password


//UPLOAD to FTP

Inet1.Execute "ftp://rpc:rpc123@sdmftp.com", "put c:\npoh2010.zip npoh2010.zip "
Do While Inet1.StillExecuting: DoEvents: Loop
MsgBox "uploading Complete", vbInformation

//DOWNLOAD from FTP

Inet1.Execute "ftp://rpc:rpc123@sdmftp.com", "get dev.zip c:\dev.zip"
Do While Inet1.StillExecuting: DoEvents: Loop
MsgBox "Downloading Complete", vbInformation

//Open FTP with Shell Command

//declare this function

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

//open by default browser
ShellExecute 0, "open", "ftp://rpc:rpc123@sdmftp.com", vbNullString, vbNullString, 1
//open by selected browser
ShellExecute 0, "open", "C:\Program Files\opera\opera.exe", "ftp://rpc:rpc123@sdmftp.com", vbNullString, 1

VB6.0 Open Browser

//go to project->Components->select 'Microsoft internet controls'
// Drag webBrowser control



WebBrowser1.Navigate "http://www.google.com"

VB6.0 Only Digit in Textbox

Private Sub Text1_KeyPress(KeyAscii As Integer)

Dim str As String

str = "0123456789"
If KeyAscii > 26 Then
If InStr(str, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End If
End Sub

VB6.0 Open EXE File

//With Shell Command

Private Sub Command1_Click()
Dim Ret As Variant
Ret = Shell("D:\kapil\projects\newdatastore\Dstore.exe", vbNormalFocus)
End Sub

//Without Shell Command

Private Sub Command2_Click()
Dim oWord
On Error Resume Next

Set oWord = GetObject("Word.Application")
If oWord Is Nothing Then
Set oWord = CreateObject("Word.Application")
End If

If oWord Is Nothing Then
MsgBox "Could not start Word"
Else
oWord.Visible = True
End If
End Sub

VB6.0 Marquee



Private Sub Form_Paint()

Label1.Left = Frame1.Width
Timer1.Interval = 1

End Sub

Private Sub Timer1_Timer()

Label1.Left = Label1.Left - 25
If Label1.Left < Frame1.Left - Label1.Width Then Label1.Left = Frame1.Width

End Sub

//Timer Interval set to 1

VB.NET : Fill DropDownList With a Column Values From The Table



//When Connection string present in web.config file

If Not IsPostBack Then

Dim cn As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader

cn = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
cn.Open()
cmd.Connection = cn
cmd.CommandText = "SELECT DISTINCT name as 'named' FROM master"
dr = cmd.ExecuteReader()
While dr.Read
DropDownList1.Items.Add(dr.Item("named"))
End While
cn.Close()

end if

VB6 : ComboBox Duplicate Item Removed

Private Sub Combo1_Click()

Dim s As String
Dim i As Integer

s = Combo1.Text

For i = Combo1.ListCount - 1 To 0 Step -1
If Combo1.List(i) = s And Combo1.ListIndex <> i Then Combo1.RemoveItem i
Next i

End Sub

Stop Browsing

Log into the user account you want to restrict.
Go to "Internet Options" and click on the "Connections" tab. Click on the "LAN Settings" button and put a tick in the box next to "use a proxy server for your LAN". Type into the "Address" box:

127.0.0.1

Click "OK" then "OK" again.

This will make the users account you need to restrict unable to access the internet.


You can use a Group Policy setting to remove the "connections" tab in "internet options" to prevent the settings being changed back.

Click "Start" > "Run" then type:

gpedit.msc

Click Computer Configuration > Administrative Templates > Windows Components > Internet Explorer > Internet Control Panel > Disable the Connections page, and click Enabled, click Apply then OK.

This is easily reversable.