Skip to content

Instantly share code, notes, and snippets.

@Phuseos
Phuseos / Site.Master.cs
Created May 24, 2016 10:45
String matching in current URL (C#)
protected void ahrefOut_Click(object sender, EventArgs e)
{//Logout button
//Get the current absolute URL of the ContentPlaceHolder
string AbsoluteURL = BodyContent.Page.Request.Url.AbsoluteUri;
//Set the string to search for in the URL
string strCheck = "Test";
//If the URL contains (part of) the string to check redirect to special login page
@Phuseos
Phuseos / mdlPublicCleanForm.bas
Created May 24, 2016 11:30
Clear control values on current active form (VBA)
Option Compare Database
Function CleanForm()
'Call as CleanForm(), will clear current text & comboboxes on active form.
For Each ctl In Form.Controls 'Loop through controls of the form
Select Case ctl.ControlType
Case acTextBox, acComboBox 'Select Textboxes and Comboboxes (DropDownLists)
ctl.value = "" 'Set the value to ""
@Phuseos
Phuseos / ButtonBlocker.cs
Created May 25, 2016 12:25
Set all buttons on page to not enabled (C#)
protected void ButtonBlocker(Control Parent)
{ //Set all buttons on page to not enabled
//Call by using ButtonBlocker(Page);
foreach (Control c in Parent.Controls) //Loop through the controls on the page
{
Button bt = c as Button; if (bt != null)
{
bt.Enabled = false; //Set the buttons to not enabled.
//c.Visible = false; //Uncomment to make the buttons invisible.
}
@Phuseos
Phuseos / ExportToExcel.vb
Last active May 26, 2016 06:53
Export a table to Excel with make up (MS Access / VBA)
Private Sub ExportSR()
1 Dim db As DAO.Database
2 Dim rs As DAO.Recordset
3 Dim oApp As Excel.Application
4 Dim i As Integer
5 Dim oWbk As Object
6 Dim oWbs As Object
7 Set db = CurrentDb()
@Phuseos
Phuseos / CoalesNull.cs
Created May 26, 2016 14:18
Null coalescing operator '??' (C#)
protected void CoalesNull(Object sender, EventArgs e) {
string MessageNoNull = "Hello World!";
string ResultNoNull = MessageNoNull ?? "Null!";
Console.WriteLine(ResultNoNull); //Prints "Hello Word!"
string MessageNull = null;
string ResultNull = MessageNull ?? "Null!";
@Phuseos
Phuseos / LingQSample.cs
Created May 27, 2016 06:21
Simple LinQ sample - string aggregation (C#)
protected void btnLINQ_Click(object sender, EventArgs e)
{
string[] WordsToUse = { "Hello ", "World", "!" }; //Array of words to use
var Res = WordsToUse.Aggregate((current, next) => current + next); //Aggregate words using LINQ
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('" + Res + "');", true); //Print text using JavaScript (don't use in production)
}
@Phuseos
Phuseos / ReadPDF.vb
Last active August 19, 2017 02:53
Select and read PDF files (VBA / MS Access)
sub ReadPDF()
'NOTE: Adobe Acrobat Pro XI or higher has to be installed and the following references have to be active:
'Adobe Acrobat 10.0 Type Library (acrobat.tlb) Usually found in: C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\
'Acrobat Access 3.0 Type Library (Accessibility.api) Usually found in: C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\plug_ins\
'To check for active references, see Tools -> References OR use the Immediate window (ctrl + G) and type "for each r in references : debug.Print r.name, r.fullpath : next r" (minus "" quotes)
Dim AcroApp As Acrobat.CAcroApp 'We're going to use the Acrobat 'app', so point to that
Dim theForm As Acrobat.CAcroPDDoc 'Define that we're going to use a Acrobat Form (known as PDF)
@Phuseos
Phuseos / NullCheck.cs
Last active May 27, 2016 11:51
Difference null checking with If and the ?? operator in action (C#)
protected void NullCheck(Object sender, EventArgs e) {
/*
* Different ways to check for a null value as an example
* Assume we have TextBox1 that we want to fill with a value, but only if the string is not null
*/
//Set the value that we want to use
string NewMessage = null;
@Phuseos
Phuseos / ActivateGrid.cs
Last active August 10, 2016 09:56
Gridview handling with MySQL (C#)
void ActivateGrid()
{
try
{
string conStr = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
MySqlConnection con = new MySqlConnection(conStr); //Set a new connection
con.Open(); //Open the connection
@Phuseos
Phuseos / GenerateRandomInt.cs
Created June 1, 2016 11:10
Generate random number (C#)
//Void that generates a random number
//Call by using RandomInt(0, 100) where 0 is the minimum number and 100 the max
public static int GetRandom { get; set; }
void RandomInt(int MinRandom, int MaxRandom)
{
Random r = new Random();
GetRandom = r.Next(MinRandom, MaxRandom);
}