[ASP.NET /VB.NET] Populate a Datagrid view without a database

I wanted to list out some data for a page that wasn’t being pulled from a database but I didn’t want to write out the table and all that. So I dropped a datagridview on to the webpage and wrote some code in the code behind.

ASP.NET Side of things

<asp:GridView ID="grd_Specs" CssClass="footable" runat="server" AutoGenerateColumns="False">
	<Columns>      
		<asp:BoundField DataField="item1" HeaderText="Pc Specs"   />
		<asp:BoundField DataField="itemstat" HeaderText="" />
	</Columns>
</asp:GridView>

 

 

VB.Net Code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PopulatePCGrid()
 
    End Sub
 
    Dim DT As New DataTable
    Dim DR As DataRow
 
    Private Sub PopulatePCGrid()
        Dim DS As New DataSet
 
        DT = New DataTable("items")
        DT.Columns.Add("item1")
        DT.Columns.Add("itemstat")
        DR = DT.NewRow
 
        AddRowToGrid("Operating System", My.Computer.Info.OSFullName.ToString)
        DR = DT.NewRow
        AddRowToGrid("Total Memory", ((My.Computer.Info.TotalPhysicalMemory / 1024) / 1024).ToString("N0") & " MB")
 
       grd_Specs.DataSource = DT
 
       grd_Specs.DataBind()
    End Sub
 
    Private Sub AddRowToGrid(ByVal description As String, ByVal data As Object)
        With DR
            .Item("item1") = description
            .Item("itemstat") = data
        End With
        DT.Rows.Add(DR)
    End Sub

 

I will explain this as best as I can.
We create two variables, 1 for the Data Table and 1 for the Data Row.
When we get into PopulatePCGrid we create a new data set and set up the data table to match the gridview layout. If these fields do not match you will get an error on page load. So make sure the fields you want exist in both the gridview and in the code behind.

I wrote a sub proceedure to add rows to the dataTable. This isn’t required but it’s best to break out code that you will be using over and over again and make them into functions /subs.

So we call the proceedure and pass in description and data. Data is an object simply because it could be anything. I didn’t want to limit it to a string or integer because that wouldn’t be true (and in fact on the actual page I wrote this for it wasn’t true).

We then add that row to the data table and go back to PopulatePCGrid to finish the rest of the code. We create a new Datarow and then continue to add more information to the Data table. After we finish that we bind the datagridview and we end up with something like this:

datagridviewExample

 

Posted in ASP.NET, Programming, VB.NET | Leave a comment

[VB.NET] MySQL Function to Insert/update/delete with parameters

I wrote this bit of code in a project that I am working on for taking screenshots and uploading them to a remote server.

Basically what this does is it accepts the following parameters:
SQLQuery as a string
Parameters and Values as a string array

I am sure there are probably better ways of doing this however this got the job done in a very little amount of code.

In order to use this you would need to include the MySql dll into your project and then refer to it.

Imports MySql.Data.MySqlClient
Dim connString As String = "server=server.com;database=db_Name;port=3306;user=db_user;password=db_password"
Public Sub SaveUpdateDelete(ByVal sql As String, ByVal parameters() As String, ByVal Values() As String)
	Dim con As MySqlConnection = New MySqlConnection(connString)
 
        con.Open()
        Dim cmd As MySqlCommand = New MySqlCommand(sql, con)
 
        For i = 0 To parameters.Count - 1
            cmd.Parameters.AddWithValue("@" & parameters(i).ToString, Values(i))
        Next
        cmd.CommandText = sql
        cmd.ExecuteNonQuery()
 
        con.Close()
End Sub

 

To make use of this function:

Private sub DoDBStuff
	Dim sql As String = "INSERT INTO db_table(userID,imageHash,notes,lastEditedDate) VALUES(@userID,@imageHash,@imageDescription,NOW())"
	Dim params() As String = {"userID", "imageHash", "imageDescription"}
	Dim Values() As String = {DataBase_UserID, DataBase_UserName & "/" & foldername & "/" & HashedFileName & "." & FileNameParts(FileNameParts.Count - 1).ToString, ""}
 
	SaveUpdateDelete(sql, params, Values)
End Sub
Posted in Programming, VB.NET | Leave a comment

[VB.NET] Reading and Writing to the Registry

This is a piece from my Final fantasy 11 Configurator that I made back when I used to play the game.

  1. Imports Microsoft.Win32

We’ll now make a registrykey variable.

  1. Dim FF_key As RegistryKey

Now we will make sure the location want it stored in that variable.

  1. FF_key = Registry.LocalMachine.OpenSubKey("\Software\Microsoft\", True)

Now to read in the Keys that are already saved by the game. In this part I called a few keys that I wanted information from, so in order to do that I first declare my variables:

  1. Dim key_windowed_mode As String
  2. Dim key_res_1 As String
  3. Dim key_res_2 As String
  4. Dim key_3D_res_1 As String
  5. Dim key_3D_res_2 As String
  6. Dim key_texture_compress As String
  7. Dim key_on_screen_map As String
  8. key_res_1 = FF_key.GetValue("0001")
  9. key_res_2 = FF_key.GetValue("0002")
  10. key_3D_res_1 = FF_key.GetValue("0003")
  11. key_3D_res_2 = FF_key.GetValue("0004")
  12. key_texture_compress = FF_key.GetValue("0018")
  13. key_on_screen_map = FF_key.GetValue("0019")

The (“numbers”) is the key name which holds the information that I wanted.
Now when it comes to writing it’s just as easy:

  1. FF_key.SetValue("0034", 1, RegistryValueKind.DWord

Valuekind.dword is just the datatype, you don’t always needs to do this but for this to work for me in this case I had to.

Posted in Programming, VB.NET | Leave a comment

[VB.NET][C#] Multi-thread / Crossthreading Example

Someone was asking how to do multi-threading in the shoutbox and my original example didn’t work because of cross-threads. This basically means I couldn’t set the value of a control within a thread since it’s considered a different process. Well I did manage to get it to work and I figured I’d post the code here. I will also include the source code both both the VB.net and C# versions below.

VB

  1. Imports System.Threading
  2. Public Class Form1
  3.  
  4. Dim Max As Double = 600000
  5. Dim i As Integer = 0
  6. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7. ProgressBar1.Maximum = Max
  8. Dim threadFill As Thread = New Thread(AddressOf Thread_FillProgressbar)
  9. threadFill.Start()
  10. End Sub
  11.  
  12. Private Sub Thread_FillProgressbar()
  13.  
  14. For i = 0 To Max
  15. AccessControl()
  16. Next
  17. End Sub
  18.  
  19. Private Sub AccessControl()
  20. If Me.InvokeRequired Then
  21. Me.Invoke(New MethodInvoker(AddressOf AccessControl))
  22. Else
  23.  
  24. ProgressBar1.Value = i
  25. ShowInTaskbar = True
  26. End If
  27. End Sub
  28. End Class

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. namespace CSharp_multithreading
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. int Max = 60000;
  20. int i = 0;
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. progressBar1.Maximum = Max;
  24. Thread threadFill = new Thread(Thread_FillProgressBar);
  25. threadFill.Start();
  26. }
  27.  
  28. private void Thread_FillProgressBar()
  29. {
  30. for (i = 0; i <= Max; i++)
  31. {
  32. accessControl();
  33. }
  34. }
  35.  
  36. private void accessControl()
  37. {
  38. if (InvokeRequired)
  39. {
  40.  
  41. Invoke(new MethodInvoker(accessControl));
  42.  
  43. }
  44. else
  45. {
  46. progressBar1.Value = i;
  47. ShowInTaskbar = true;
  48.  
  49. }
  50.  
  51. }
  52. }
  53. }

VB.NET Source

C# Source

Posted in C#, Programming, VB.NET | Leave a comment

VB.NET Detect Key Presses

This bit of code will allow you to detect key presses while the form is not the active window. I currently use this for macros and for my trainer programs for turning on cheats.

All you need to have is a timer on the form. You can either drag and drop one on the form or declare it in code and write the timer.Tick event.

In this example I check for 2 key presses.

  1. Dim altkey As Boolean
  2. Dim Num_1 As Boolean
  3. Dim Num_2 As Boolean
  4. Dim Num_3 As Boolean
  5.  
  6. Private Sub tmr_Keys_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_Keys.Tick
  7. altkey = GetAsyncKeyState(Keys.LMenu)
  8. Num_1 = GetAsyncKeyState(Keys.NumPad1)
  9. Num_2 = GetAsyncKeyState(Keys.NumPad2)
  10. Num_3 = GetAsyncKeyState(Keys.NumPad3)
  11. If altkey And Num_1 = True Then 'Infinite Health Player 1
  12. btn_P1InfiniteHealth_Click(sender, e)
  13. ElseIf altkey And Num_2 = True Then
  14. btn_P1InfiniteKi_Click(sender, e) ' Infinite Ki Player 1
  15. ElseIf altkey And Num_3 = True Then
  16.  
  17. End If
  18. End Sub
Posted in Programming, VB.NET | Leave a comment

VB.NET Setting an External Program’s Title window

I have been working on making some trainers for some games and I wanted a basic way of showing that a cheat was enabled.
I couldn’t figured out how to draw to the screen so I figured the title bar would be fine.

  1. Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hWnd As IntPtr, ByVal lpString As String) As Boolean
  2.  
  3. Public Sub SetTitleText(ByVal processName() As Process, ByVal textToSend As String)
  4.  
  5. For Each proc In processName
  6. SetWindowText(proc.MainWindowHandle, textToSend)
  7. Next
  8.  
  9. End Sub

The code above will allow you to set the title window to an external program.
An example on how to use it

  1. Dim notepad() As Process = Process.GetProcessesByName("notepad")
  2.  
  3. Private Sub btn_Notepad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Notepad.Click
  4. SetTitleText(notepad,"Insert some text here")
  5. End Sub
Posted in Programming, VB.NET | Leave a comment

VB.NET Using a Binary File as a resource

This was something I used in order to try and keep my programs down to 1 file. Instead of including a seperate file that handled the updating, I simply embedded it into the main program. To do this all I did was make it a resource and when I needed to use it I would write it out to the root directory that the program was in.

To add a file as a resource, right click your project and click properties.

Click on the Resources tab. On the top click on strings and select files. Drag and drop the file you want to include as the resource into this window.

Click save or save all.

Now when you decide you need to use that file you can write it out to a directory then run it.

  1. IO.File.WriteAllBytes("./" & "Updater.exe",My.Resources.Updater)
  2. Process.Start("Updater.exe")
Posted in Programming, VB.NET | Leave a comment

Final Fantasy XI Configurator v1.1

FFXIConfig

ChangeLog
-Added some new options
-MIP Mapping
-Environmental Animation
-Bump Mapping
-Font Quality
-Skip Intro Movie

Download

Posted in Final Fantasy Configurator, General | Leave a comment

Taskmanager Plus FileRemoval Update

This was a feature I have been planning for some time and finally got around to working on it. Basically it will have default folders that will be in the program. These folders will be where your temp files are located. All you would have to do from here is click the clean button next to the item and it will remove those files.

You are also able to add your own folders that you wish to be included in the program. So anytime you start up the application you will get the list of folders and the amount of space they are taking up.

The design in the video isn’t what I plan on having it look like on release but it’s functionality will be the same.

Posted in TaskManager | Leave a comment

Taskmanager Plus Update v 1.0.1

taskmanager
Version 1.0.1

-Added the ability to stop multiple processes at once. Simple control click the ones you want to stop and click End process.
-Forms have changed, new features will be added that deal with clean up on temp files and fixing other policies within windows

If you have the program already simply click file and check for updates. It will say an update was found click yes and then in the new windows click update. Or go to the download link in this post
Download

Posted in TaskManager | Leave a comment