Asp.Net – Adding Parameters to CommandType.Text

This is how you add parameters to a standard CommandType.Text SQL call.

This should prevent sql injection attacks.

        Using conSQL = New SqlConnection("CONNECTION STRING TO DATABASE")
            Try
                str = "SELECT * FROM TableName where customerid = @CustomerID and CartType = @CartType"
                cmdSQL = New SqlCommand(str, conSQL)
                cmdSQL.CommandType = CommandType.Text
                cmdSQL.Parameters.AddWithValue("@CustomerID", CustomerID)
                cmdSQL.Parameters.AddWithValue("@CartType", cartType)
                cartcount = cmdSQL.ExecuteScalar()
            Catch ex As Exception
                cartcount = 0
            End Try
        End Using

Asp DropDownList – States – ListItem Example

Asp DropDownList - States - ListItem Example
Compiled List of US State Drop Downs for Asp.Net Forms:

Hope this helps!

<asp:DropDownList ID="ddlState" runat="server">
    <asp:ListItem Value="" Selected="True">Select a State</asp:ListItem>
    <asp:ListItem value="AL">Alabama</asp:ListItem>
    <asp:ListItem value="AK">Alaska</asp:ListItem>
    <asp:ListItem value="AZ">Arizona</asp:ListItem>
    <asp:ListItem value="AR">Arkansas</asp:ListItem>
    <asp:ListItem value="CA">California</asp:ListItem>
    <asp:ListItem value="CO">Colorado</asp:ListItem>
    <asp:ListItem value="CT">Connecticut</asp:ListItem>
    <asp:ListItem value="DC">D.C.</asp:ListItem>
    <asp:ListItem value="DE">Delaware</asp:ListItem>
    <asp:ListItem value="FL">Florida</asp:ListItem>
    <asp:ListItem value="GA">Georgia</asp:ListItem>
    <asp:ListItem value="HI">Hawaii</asp:ListItem>
    <asp:ListItem value="ID">Idaho</asp:ListItem>
    <asp:ListItem value="IL">Illinois</asp:ListItem>
    <asp:ListItem value="IN">Indiana</asp:ListItem>
    <asp:ListItem value="IA">Iowa</asp:ListItem>
    <asp:ListItem value="KS">Kansas</asp:ListItem>
    <asp:ListItem value="KY">Kentucky</asp:ListItem>
    <asp:ListItem value="LA">Louisiana</asp:ListItem>
    <asp:ListItem value="ME">Maine</asp:ListItem>
    <asp:ListItem value="MD">Maryland</asp:ListItem>
    <asp:ListItem value="MA">Massachusetts</asp:ListItem>
    <asp:ListItem value="MI">Michigan</asp:ListItem>
    <asp:ListItem value="MN">Minnesota</asp:ListItem>
    <asp:ListItem value="MS">Mississippi</asp:ListItem>
    <asp:ListItem value="MO">Missouri</asp:ListItem>
    <asp:ListItem value="MT">Montana</asp:ListItem>
    <asp:ListItem value="NE">Nebraska</asp:ListItem>
    <asp:ListItem value="NV">Nevada</asp:ListItem>
    <asp:ListItem value="NH">New Hampshire</asp:ListItem>
    <asp:ListItem value="NJ">New Jersey</asp:ListItem>
    <asp:ListItem value="NM">New Mexico</asp:ListItem>
    <asp:ListItem value="NY">New York</asp:ListItem>
    <asp:ListItem value="NC">North Carolina</asp:ListItem>
    <asp:ListItem value="ND">North Dakota</asp:ListItem>
    <asp:ListItem value="OH">Ohio</asp:ListItem>
    <asp:ListItem value="OK">Oklahoma</asp:ListItem>
    <asp:ListItem value="OR">Oregon</asp:ListItem>
    <asp:ListItem value="PA">Pennsylvania</asp:ListItem>
    <asp:ListItem value="RI">Rhode Island</asp:ListItem>
    <asp:ListItem value="SC">South Carolina</asp:ListItem>
    <asp:ListItem value="SD">South Dakota</asp:ListItem>
    <asp:ListItem value="TN">Tennessee</asp:ListItem>
    <asp:ListItem value="TX">Texas</asp:ListItem>
    <asp:ListItem value="UT">Utah</asp:ListItem>
    <asp:ListItem value="VT">Vermont</asp:ListItem>
    <asp:ListItem value="VA">Virginia</asp:ListItem>
    <asp:ListItem value="WA">Washington</asp:ListItem>
    <asp:ListItem value="WV">West Virginia</asp:ListItem>
    <asp:ListItem value="WI">Wisconsin</asp:ListItem>
    <asp:ListItem value="WY">Wyoming</asp:ListItem>
</asp:DropDownList>

More State Drop Downs After the fold:
Continue reading “Asp DropDownList – States – ListItem Example”

Asp.Net VB – Listview – ItemCommand – DataKeys – CommandName – SQL

Asp.Net VB - Listview - ItemCommand - DataKeys - CommandName - SQL

Here is a basic Listview that has an ItemCommand and ItemDataBound:

Frontend Code:

<asp:ListView ID="lvAddresses" runat="server" DataKeyNames="AddressID,BillingAddressID,ShippingAddressID">
        <LayoutTemplate>
            <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
        </LayoutTemplate>
        <ItemTemplate>
            <table class="noborder address_book" cellpadding="0" cellspacing="0"><tbody>
                <tr>
                    <td colspan="2">
                        <%#DataBinder.Eval(Container.DataItem, "FirstName")%> <%#DataBinder.Eval(Container.DataItem, "LastName")%><br>
                        <%#DataBinder.Eval(Container.DataItem, "Address1")%><br>
                        <%#DataBinder.Eval(Container.DataItem, "City")%>, <%#DataBinder.Eval(Container.DataItem, "State")%>, <%#DataBinder.Eval(Container.DataItem, "Zip")%><br>
                        <%#DataBinder.Eval(Container.DataItem, "Phone")%><br>
                        <span class="caption"><asp:LinkButton ID="lnkMakeDefault_Billing" CommandName="MakeDefault_Billing" runat="server">Click to make Default Billing Address</asp:LinkButton></span>
                        <span class="caption"><asp:Literal ID="litMakeDefault_Billing" runat="server" Text="Default Billing Address"></asp:Literal></span>
                        <br />
                        <span class="caption"><asp:LinkButton ID="lnkMakeDefault_Shipping" CommandName="MakeDefault_Shipping" runat="server">Click to make Default Shipping Address</asp:LinkButton></span>
                        <span class="caption"><asp:Literal ID="litMakeDefault_Shipping" runat="server" Text="Default Shipping Address"></asp:Literal></span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="EDIT" CommandName="EDITAddress"/>
                    </td>
                    <td>
                        <asp:Button ID="btnREMOVE" runat="server" Text="REMOVE" CommandName="REMOVEAddress"/>
                    </td>
                </tr>
            </tbody></table>
 
        </ItemTemplate>
    </asp:ListView>

Backend Code:

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
 
        FindIt()
 
    End Sub
    Private Sub FindIt()
 
        lblAddresses.Text = ""
 
        Dim sqlDataConnection As System.Data.SqlClient.SqlConnection = CDataAccess.OpenDatabase
        Dim conStr As String = sqlDataConnection.ConnectionString
        sqlDataConnection.Close()
 
        Dim selectCommand As String = "SELECT Address.*, Customer.BillingAddressID, Customer.ShippingAddressID"
        selectCommand &= " FROM Address INNER JOIN Customer ON Address.CustomerID = Customer.CustomerID"
        selectCommand &= " WHERE Address.CustomerID = @CustomerID AND Address.Deleted = 0"
 
        Dim sds As New SqlDataSource()
        sds.ConnectionString = conStr
        sds.SelectParameters.Clear()
 
        sds.SelectParameters.Add("CustomerID", 12345)
        sds.SelectCommand = selectCommand
 
        lvAddresses.DataSource = sds
        lvAddresses.DataBind()
    End Sub
 
    Protected Sub lvAddresses_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lvAddresses.ItemCommand
        Dim dataItem As ListViewDataItem = e.Item
        Dim currentDataKey As DataKey = lvAddresses.DataKeys(dataItem.DataItemIndex)
        Dim AddressID As Integer = currentDataKey("AddressID")
        Dim BillingAddressID As Integer = currentDataKey("BillingAddressID")
        Dim ShippingAddressID As Integer = currentDataKey("ShippingAddressID")
 
        'MakeDefault_Billing
        'MakeDefault_Shipping
        'EDITAddress
        'REMOVEAddress
        If String.Equals(e.CommandName, "MakeDefault_Billing") Then
            MakeDefault(AddressID, True)
        ElseIf String.Equals(e.CommandName, "MakeDefault_Shipping") Then
            MakeDefault(AddressID, False)
        ElseIf String.Equals(e.CommandName, "EDITAddress") Then
            EditAddress(AddressID)
        ElseIf String.Equals(e.CommandName, "REMOVEAddress") Then
            RemoveAddress(AddressID)
        End If
    End Sub
 
    Protected Sub lvAddresses_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvAddresses.ItemDataBound
        Dim dataItem As ListViewDataItem = e.Item
 
        If e.Item.ItemType = ListViewItemType.DataItem Then
            Dim currentDataKey As DataKey = lvAddresses.DataKeys(dataItem.DataItemIndex)
            Dim AddressID As Integer = currentDataKey("AddressID")
            Dim BillingAddressID As Integer = currentDataKey("BillingAddressID")
            Dim ShippingAddressID As Integer = currentDataKey("ShippingAddressID")
            lit = e.Item.FindControl("litMakeDefault_Billing")
            Dim lit2 As Literal = e.Item.FindControl("litMakeDefault_Shipping")
            lnk = e.Item.FindControl("lnkMakeDefault_Billing")
            Dim lnk2 As LinkButton = e.Item.FindControl("lnkMakeDefault_Shipping")
 
            lit.Visible = Not lnk.Visible
        End If
 
    End Sub

ASPDotNetStoreFront: Adding Topic to XML Packages or .aspx Pages

This is how you add a DotNetStoreFront’s Topic into a XMLPackage:

<xsl:value-of select="aspdnsf:Topic('TOPIC_NAME')" disable-output-escaping="yes"/>

Now, if you want to add it to a .aspx or .ascx page on dnsf:

<aspdnsf:Topic runat="server" ID="Topic1" TopicName="TOPIC_NAME" />

Simple PHP Pseudo Proxy

php proxy simple pseudo

This is the code for a simple php pseudo proxy. If you are blocked by a firewall and google cache isn’t working for some reason. You can use a url with this php file in it:

<?php
$filename = $_GET["url"];
if (isset($filename))
{
        $handle = fopen($filename, "rb");
        $contents = stream_get_contents($handle);
        print $contents;
        fclose($handle);
}
else
{
        echo "url";
?>
<form method="GET">
<input type="text" name="url" /><input type="submit"/>
</form>
<?php
}
?>

Then you can go to where the url is hosted and try something like this:

index.php?url=http%3A%2F%2Fwww.last.fm%2Fuser%2Fdanfolkes%2Fcharts%3Frangetype%3D6month%26subtype%3Dartists

Facebook Website Integration – HTML


To integrate Facebook on to a website without the use of a facebook app you need these things:

Once you have both of those things you can do this in feedburner:

  • Add a new feed (your facebook url)
  • Go to the publicize tab
  • Go to BuzzBoost
  • Sroll down and fill out the form, you will be given html code.
  • <script src="http://feeds.feedburner.com/DanielFolkessFacebookStatusUpdates?format=sigpro" type="text/javascript" ></script>
    <noscript><p>Subscribe to RSS headline updates from: 
    <a href="http://feeds.feedburner.com/DanielFolkessFacebookStatusUpdates"></a>
    <br/>Powered by FeedBurner</p> 
    </noscript>
  • Preview:

And that’s it!
You can now style it up a little bit with some CSS and stuff.

Java – Singleton / Global – Data Access – SQLite

This will make a Java global object:

import java.io.File;
import java.io.IOException;
 
import com.almworks.sqlite4java.SQLite;
import com.almworks.sqlite4java.SQLiteConnection;
import com.almworks.sqlite4java.SQLiteException;
import com.almworks.sqlite4java.SQLiteJob;
import com.almworks.sqlite4java.SQLiteQueue;
import com.almworks.sqlite4java.SQLiteStatement;
 
public final class DataAccess {
	// Singleton Stuff
	private static class SingletonHolder { 
	     public static final DataAccess INSTANCE = new DataAccess();
	}
	public static DataAccess getInstance() {
	     return SingletonHolder.INSTANCE;
	}
	// End Singleton Stuff
 
 
	private String HomePath = "";
	private File DBFile;
	private static SQLiteQueue queue;
	private boolean initalized = false;
 
	private DataAccess()
	{
		if (!initalized)
		{
			initDB();
 
			initalized = true;
		}
	}
 
	private void initDB()
	{
		DBFile = locateDBFile();
		DeleteDatabaseContents();
	}
	private File locateDBFile()
	{
		File f = null;
		try{
			HomePath = System.getProperty("user.home");
    	    System.out.println("HomePath: " + HomePath);
    	    f = new File(HomePath + "/database.sqlite");
    	    if (f.canRead())
    	    	return f;
    	    else
    	    {
        	    boolean success = f.createNewFile();
        	    if (success) {
        	        // File did not exist and was created
        	    } else {
        	        // File already exists
 
        	    }
    	    }
    	} catch (IOException e) {
    		//Maybe try a new directory.
    	}
		return f;
	}
 
	private void DeleteDatabaseContents()
	{
		queue.execute(new SQLiteJob<Object>() 
		{
		    protected Object job(SQLiteConnection connection) throws SQLiteException 
		    {
		      // this method is called from database thread and passed the connection
				StringBuilder sb = new StringBuilder();
				sb.append("DROP TABLE IF EXISTS Playlist;");
				sb.append("DROP TABLE IF EXISTS Settings;");
				//sb.append("DELETE FROM someTable;");
				//sb.append("DELETE FROM someTable;");
 
 
				connection.exec(sb.toString());
				return null;
		    }
		});
	}
	public String getHomePath()
	{
		return HomePath;
	}
}

Now you can run this from anywhere:

DataAccess da;
da = DataAccess.getInstance();

Source: http://en.wikipedia.org/wiki/Singleton_pattern

PHP API – Google Calendar – Updating Visibility

The Privacy / Visibility Part:

$event->visibility->value = "http://schemas.google.com/g/2005#event.private";

Here it is in context: Continue reading “PHP API – Google Calendar – Updating Visibility”

Minecraft – Script – Backup – Update Server – Restart

This is the script I use that will:

  • Automatically backup minecraft map
  • Automatically update minecraft server
  • Automatically restart the server if it stops or dies
#!/bin/bash
while [ true ]; do
   # make md5 of current file for update check.
   md5sum minecraft_server.jar > minecraft_server.jar.md5
   wget -o updatelog.log -N --trust-server-names http://www.minecraft.net/download/minecraft_server.jar
   # display updated status
   if md5sum -c --status minecraft_server.jar.md5 
   then
    echo "Server Up to Date."
   else
    echo "Updated Server!"
   fi
   java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui
   rsync -a --delete --progress /home/dan/serv/minecraft/world "/home/dan/serv/minecraft/bak/world"`eval date +%Y%m%d`""
done

Code Explained:

This will put the server in an infinite loop. You can kill it by hitting Control+C a couple times in a row.

while [ true ]; do

This will make a hash of the current minecraft_server.jar and then download the newest copy using wget.

md5sum minecraft_server.jar > minecraft_server.jar.md5
wget -o updatelog.log -N --trust-server-names http://www.minecraft.net/download/minecraft_server.jar

This will run minecraft in headless mode and with a limit on RAM.

java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui

This will create a backup of the minecraft world folder in a backup folder. You probably should work on this part.

rsync -a --delete --progress /path/to/minecraft/world "/path/to/minecraft/bak/world"`eval date

Well, hope this helps someone. Have a good day.

Rsync – Speed Limit – Trickle – Slow

Install rsync and trickle:

sudo apt-get install rsync
sudo apt-get install trickle

Now you can run rsync:
This will download at a limit of 80 KB/s from host:

rsync -auvPe "trickle -d 80 ssh" user@host:/src/ /dst/

Explanation of Commands:

-a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
-u, --update                skip files that are newer on the receiver
-v, --verbose               increase verbosity
-P  --progress              show progress during transfer
-e, --rsh=COMMAND           specify the remote shell to use
trickle -d 80  = -d rate    Limit the download bandwidth consumption to rate KB/s.