Archive for the ‘Uncategorized’ Category
Asp.net VB Textbox to Sql
Ok I have visual Studio web developer and I have a database now what
it is some times frustrating to find custom SQL in Asp.net tutorials , you always get the same thing how to data bind , grid views ect but what if you just want to go from a text box control to a sql insert ? well it is pretty easy if you know where to start … in visual studio Web developer first make a database table and include an id and set it as a primary key and a text field Varchar lets call it “name” varchar (50) add some data to it then drag a good old fashion text box control and button on to a new Asp.net page then you will need to add a New SqlDataSource on the page , connect to it then next then click advanced . and tick generate insert, update, Delete statements…and ok then finish now that you have all that click on the button to make an event handler
In VB.. Add this script to the event handler
SqlDataSource1.InsertParameters(“name”).DefaultValue = textbox1.Text.ToString()
SqlDataSource1.Insert()
DONE ! then you can see that your data has been inserted in to the table why not ad a grid view to test this below it
assuming you have an table and with id set to primary key and “name”
Example
Default.aspx:
<%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”again.aspx.vb” Inherits=”again” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title> </title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
Name
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<asp:Button ID=”Button1″ runat=”server” Text=”Button” />
<br />
<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server”
ConnectionString=”<%$ ConnectionStrings:customsiteConnectionString2 %>”
DeleteCommand=”DELETE FROM [mysimpletable] WHERE [id] = @id”
InsertCommand=”INSERT INTO [mysimpletable] ([name]) VALUES (@name)”
SelectCommand=”SELECT * FROM [mysimpletable]”
UpdateCommand=”UPDATE [mysimpletable] SET [name] = @name WHERE [id] = @id”>
<DeleteParameters>
<asp:Parameter Name=”id” Type=”Int32″ />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name=”name” Type=”String” />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name=”name” Type=”String” />
<asp:Parameter Name=”id” Type=”Int32″ />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
Default.Asp.VB:
Partial Class again
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SqlDataSource1.InsertParameters(“name”).DefaultValue = TextBox1.Text.ToString()
SqlDataSource1.Insert()
End Sub
End Class
and that is it not to bad
Ajex Is Easy
A lot of people don’t know the basics of ajex : or passing vars from a page and using JavaScript to call another asp/php/asp.net/javascript page … I f you know php or asp or asp.net and JavaScript event handlers then my friend you are ready to start making use of simple then more advanced Ajax like web interfaces … lts here is a fun simple example :
the “magic” takes place with the JavaScript XMLHttpRequest()
Put this in the head section of a html / php page
<head>
<title>Some Document </title>
<script type="text/javascript">
function loadXMLDoc(int)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","result.php?var="+int,true);
xmlhttp.send();
}
function changecolour(){
document.getElementById("change").style.color="red"
}
</script>
</head>
that gets it going note it will call a php page called result.php with a url encode ?var= the javascript var
<div id="myDiv"><h2>Let AJAX see if you type hello</h2>
<br />
<input type="text" onblur="loadXMLDoc(this.value)"/>
</div>
(note just type hello) then make a new page called
result.php... and in it put this in it
<?php
$thevar = $_GET['var'];
echo $_GET['var'];
if($_GET['var'] =="hello"){
// could be password validation or what ever
echo "You Typed hello You Are Cool";
$x=1;
}else{
echo"<br/>
<input type='text' value='".$thevar."' style='color:#FF0000' onblur='loadXMLDoc(this.value)'/> \n";
echo" <br/>Not valid please try again just type `Hello` ";
}
?>
there you go job done you can do any thing in that result page
download example here and have fun
once you have this idea you can easily see how you can use
(real) xml to communicate between many applications
Allot of people don’t know the basics of ajex : or passing vars from a page and using JavaScript to call another asp/php/asp.net/javascript page …
I f you know php or asp or asp.net and JavaScript event handlers then my friend you are ready to start making use of simple then more advanced Ajax like web interfaces … lts here is a fun simple example :
the “magic” takes place with the XMLHttpRequest()
How To Display Content Based on Ip (Geolocation)
Let me show you how to do Geolocation Content based on ip address !(with php and mysql)
first you need a database eg make a database call it what ever you want now you will need to Get A Database of ip 80% world wide accuriate Click Here to download ( ip2nation )
Then insert import the sql (taken from ip2nation but small changes)
note Go to http://ip2nation.com/ip2nation/Sample_Scripts
and Get it …the bottom of code is different examples of use
<?php
////////////////dot use this get it from ip2nation.com
$server = ''; // MySQL hostname
$username = ''; // MySQL username
$password = ''; // MySQL password
$dbname = ''; // MySQL db name
$table = "ip2nation";
$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$sql = 'SELECT `country`FROM
`$table`
WHERE
`ip` < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
ORDER BY
`ip` DESC
LIMIT 0,1';
list($country) = mysql_fetch_row(mysql_query($sql));
// use a switch statement for redirect
switch ($country) {
case 'ca':
// Get the Canadians to a canadians site
header('Location: http://www.yoursite.ca/');
exit;
case 'us':
// And redirect US visitors to your .com
header('Location: http://www.yoursite.com/');
exit;
default:
// The rest of the world can go to defult
header('Location: http://www.yoursite.com.au/');
exit;
}
//Get The Basic Idea include a file
if($country = 'ca'){
include ("canada.php");
}else{
include ("USA.php");
}
// Display an image
switch ($country) {
case 'ca':
// show canada flag
echo"<img src='images/canada.jpg' />";
exit;
case 'us':
// show us flag
echo"<img src='images/usa.jpg' />";
exit;
default:// the default flag
echo"<img src='images/australia.jpg' />";');
}
?>
try it
http://taggartjensen.com/ip
Have Fun this demo is for Australia, Canada, Usa











