Here are six simple functions which allow you to quickly send an SQL command to get a DataRow, DataTable, integer, string, or date from your database (SQL Server). Another function allows you to execute an SQL statement (e.g. INSERT or UPDATE) on your database. Note that you need to have a connection string called "con" in your web.config file. For each function, you can send a simple SQL statement, but if you have variables, an overridden function provides you with a way to easily use SqlParameters (in one line). Using the parameter syntax, you can prevent sql injection. If you need error handling you will have to build it in. But other than that, these function are ready to save you lots of development time by simplying access to your data. You would probably in most cases want to put these functions in code behind (a .dll in your \bin directory) so that you have access to them in every file.
If you want ALL your code in a code behind so that you only have HTML in your aspx files (so that designed can edit it for example without having to deal with code), you can use this code as an example. It even accesses data in an SQL database.
When possible, you should minimize trips to the data layer. One way to do this is to send multiple SELECT statement with a DataReader. This code shows you how. Notice that afterward I refer to the fields with numbers and they are not the same kinds of fields.
SHOW CODESHOW CODE
Robert: Hello
While practising this exaple, I had a little problem using the following code.
TextBox txt = new TextBox();
txt.ID = "TextBox2";
TextBox txt2 = new TextBox();
txt2.ID = "TextBox3";
this.PlaceHolder1.Controls.Add(txt);
this.PlaceHolder1.Controls.Add(txt2);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConStr"].ToString());
using (con)
{
SqlCommand command = new SqlCommand("SELECT Count(*) As CountEmp FROM Employees;SELECT Count(*) As CountCat FROM Categories", con);
con.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
txt.Text = dr["CountEmp"].ToString();
txt2.Text = dr["CountCat"].ToString();
}
}
dr.Close();
}
When I use a string to get the value of the second SELECT statement, I get an error with CountCat as the only description.
If I use a number (number 1 for the second SELECT) i get an IndexOutOfRangeException.
Is using different tables in the command text possible?
If so, how can i access them? I can't get this to work. replyreply
One of the advantages of .NET is that you (can) work with safe types so that you don't have to guess or test for what type a variable is before you use it. This code shows you how to explicitly read in values from a database using DataReader.
Since the connection cannot be used until a DataReader has been closed, it is quite often necessary to read the results of a DataReader into an array, then close the DataReader, THEN you can process the data in the array while you use the connection for something else. I could only get this example to work with integers but not varchars (?).
If you were going to build a GenericDB-like meta application for displaying and editing databases with a browser in .NET, you would need to be able to get the meta information out of the DataReader or DataSet from your database. Here is how to read the DataReader for its meta information.
If you need to return only the first row of a DataReader result (perhaps for testing or to just get the meta structure of a result set), this is the syntax to use to return only one row.
You can save code by piling SQL statements into one SqlCommand and then getting a DataReader with multiple result sets. This code shows you how to run through these.
If you use SQL Server 2000, you can use the "FOR XML" command in the SELECT statement to return your record results as XML text (to process with XPath, etc.). Notice that with the parameter ELEMENTS in the SQL statement, you can define if you want the field data to be in attribute or element tags.
As this code shows, you can send two SQL statements, one to get a record set and one to get an integer, then using the parameterized T-SQL feature, you can read them both back from the command. Notice that we have to close the DataReader before the parameter value is accessible.
Using stored procedures can greatly simplify the front end logic of your web application -- you simply have to call them and the business logic is taken care of for you -- nice way to clean up your scripting. This code shows you how to use stored procedures.
This code shows how to send and retrieve variables via a stored procedure. Notice that the output variables are only accessible after the DataReader is closed.
If you have complex SQL statements which have many variables, you can simplify your programming by using parameterized T-SQL statements. This code shows you the basic syntax.
Was able to download files with Tortoise but (a) I still get a black screen and (b) there seems to be something wrong with my flash player for debugging.
This tutorial shows you how to download a trial version of VMware, download the free Fedora CDs, install Suse on your VMWare machine, then download and install the free VMware Player and connect your virtual Linux machine in your network so that you always have a Linux machine in your network at your disposal, great if you are learning Linux. 1. Download VMware Workstation Trial2. Install VMware on Windows XP3. Download the Fedora DVD
This file runs through the input filters codes and shows how e.g. filter_var($value, FILTER_SANITIZE_STRING) and filter_input($value, FILTER_SANITIZE_STRING) will alter strings. You can see an example this here..
Nusoap is convenient because you don't have to make any changes on the server, just download and copy the library files to a sub-directory. Nusoap also creates a WSDL file automatically for you. These examples show how to return an array of values so that you could, e.g. return 20 fields of information about a user, for instance. Not that on the nusoap examples I turn the cache off since when you have it on while you are developing and add a method to your soap server, your client doesn't recognize the changes. First download nusoap, then copy the files to a sub-directory on your web called "nusoap".
The following instructions show you the exact steps to set up a free development environment on windows which allows you to develop Flex applications with ActionScript 3. You won't have the nice step-through debugging or the design view of Adobe's FlexBuilder but this code shows you how to create a two-line MXML file which allows you to do all your programming back in ActionScript 3 classes anyway so you just kind of use the MXML file as a tunnel to be able use the rich Flex libraries. FlashDevelop has very nice code-completion and other features, and with traces you can do decent debugging. The code helps you get up to speed with event handlers which are a bit different than web scripting if that is all you have been doing so far. You can view a running example of this appliction here.
This works much better than my previous example since it enables FlashDevelop to do its nice code-completion on Flex objects such as "button". In FlashDevelop, new project, Flex, Flex 3 Project, then replace MXML code, then create a class and put in the ActionScript3 code.
In FlashDevelop, new project, Flex, Flex 3 Project, then replace MXML code, then create a class and put in the ActionScript3 code. Unfortunately it doesn't seem to have the nice code complete completion for e.g. the button object.
In FlashDevelop, New Project, AS3 Project, (check Create directory for project), (Project/Properties/Text Movie/Play in popup), then open up Main.as and replace it with this code.
I wanted to install Visual C# 2008 Express but knew I wanted to try out the Visual Basic and Visual Web Developer as well, so instead of doing three downloads from the web, I downloaded the .ISO file with all four of them on it, then downloaded the free Virtual Clone Drive which after you install it looks like you added a DVD drive to your system, right-click it and select Virtual CloneDrive and Mount, then navigate to and select the .iso file and it is as if you are loading from a DVD, nice, much smoother solution then one I was using back in 2002 -- it just works.
post comment or questionpost comment or question
tip
ECLIPSE TIP - added on October 9, 2008
Create hotkey macros in Eclipse
I've been using Eclipse for a long time and just found out that you can create a hotkey to perform all kinds of mundane tasks, e.g. if you find yourself committing a file every minute or so to check minute changes, go to Preferences | General | Keys and set up a e.g. CTRL-ALT-SHIFT-C to commit your file, explore this for awhile and save some time in the long run.
Tired of editing long lines extending out to the right in Eclipse? This plugin solves the problem, install as any plugin in Eclipse, restart, then right click and word wrap, simple as that.
If you need to write a quick PHP script that accesses a MySQL database, just copy in or include this class and you can do all your reads and writes with one-line satements.
Stackoverflow just launched publically today after being in beta for about two months. Last week I posted about 8 questions about vim and Perl and each question was answered by multiple people within 10 minutes. The site has a system of badges and reputation points you can earn for various things which turns people into hungry dogs waiting to answer your question first. You get to read the answers and mark them up and down based on their helpfulness and then select one that best answers your question. This site is truly unique, new, fresh, ajaxy in the right places, and very efficient, try it out.
post comment or questionpost comment or question
tip
PERL TIP - added on September 12, 2008
Always put "my" in front of variables inside functions
In Perl, if you define a variable inside a function without the word "my" (e.g. @record = ...), then that variable is treated as a global variable (wow!). Hence, always define function variables thusly: "my @records = ...." inside a function so that they do not change values outside the function, e.g. if you have too recordsets and you use the same variable name "@records" to loop through them.
Was on a Perl project where the code had 5 lines of database connection code for each time it needed to get data from the database, so I wrote these functions which allow you to get records, record, value or execute a command with one-liners. Records are an array of hashes and included the code to call the functions since figuring out what syntax you need to access the objects through pointers/references can be time-consuming. End result is: if you just need to use Perl quick to read and write to a MySQL database, this code will get you up to speed fast.
If you need to do something quick and useful in Perl, this will get you reading and writing in databases and text files. If you are a PHP programmer getting into Perl then look up the "eq" operator first.
This function checks if an IP address is between two others, not that ip2long has a problem by itself so you need the sprintf("%u") function as well. Also it returns false if any of the IP addresses are invalid as to avoid tampering.
SHOW CODESHOW CODE
patryksharks: shouldn't it be preg matches?
greetings
I installed Firefox 3 today and instantly found the search-history-in-the-URL-bar to be indispensible. All the more to my horror did I realize that Firebug was no longer installed because it was not compatible with Firefox 3. Luckily there is a beta being built that IS compatible and it works just like the old one. The SQLite Manager and Web Developer bar still work in Firefox 3 and so with these three, no reason not to upgrade! Enjoy.
This was a little experiment to get a useful base of cross-browser code that does some interactive stuff in Javascript. It is simple but handles mouseover, button highlight, and just has a nice, basic feel. The only issue is in Opera the text is at top of the buttons, but didn't have any nerves left for that one. At first I tried using :hover and :focus which brought me to the conclusion that this really isn't standard between the browsers so used onmouseover and onmouseout to deal with it. Also worked out a number of quirks that various browsers had, e.g. I removed the ugly dotted focus line in Firefox on the buttons, they left it there in Firefox 3 as well I noticed, I replaced it with a gradient background for the focussed button. So this code looks decent in Firefox 2/3, Internet Explorer 7 (probably 6 too), Opera 9.5 (in fact only after I upgraded from 8 did it show the background on the buttons), and Safari. The bad boy of the group is Opera, but since opera gets embedded in quite a number of devices these days (e.g. Philips TVs), you don't want to leave it out completely. You can view this example online here. Of course next step when implementing this is to make it read from a database or XML file, I just wanted to get the browser-compatibility as accurate as possible first.
If you are getting into AJAX and need a booster course on Javascript, this site has a nice concept/example format to get you up to speed fast without the make-the-background-color-blink examples you get on other sites. Note the "AJAX for N00bs" section as well.
If you have various translations of your website, a quick way to implement this is to have an .ini file with all the translations in it and read it out. This code shows you how to also use constants within the ini file.
This CSS script give you absolute pixel-exact control of where to place text in a left and right area of a wide div, e.g. a header or footer. I was not able to get it to align automatically, e.g. left/center/right, so you have to know your font size and your text and position things exactly, but at least you have control, checked in Firefox and Explorer.
SHOW CODESHOW CODE
patryksharks321: nice code
btw relative is according to the last element and absolute is towards the block it's in right?
greetings
I've been recently downloading and listening to some great IT talks from the Conversation Network, talks by e.g. Rasmus Lerdorf founder of PHP, Mark Shuttleworth, founder of Ubuntu, even things that are on the edge of technology such as stuff going on in Biology and other sciences, really interesting talks by some key people in the industry, I've listened to about 5 and they've all been good. They have a nice format: you sign up to their RSS feed and just download the ones you want straight onto your MP3 stick and go. The free MP3s have a plug to sign up at the beginning and the end but you can download and listen to anything you want. After you download one, you get an email to review it the next day and in one click you can say how you liked it, which I assume gives good feedback so the best float to the top. If you have a lot of listen/wait time in your daily life, this is a great way to stay up on the latest in the IT space.
EditPad Lite has been my text editor of choice for a long time since it is free and has an easy time-stamping feature (ALT-E-I) and you can set the time format. But it used to have a nasty habit of always writing a BOM to the beginning of UTF-8 files which PHP 5.2 can't deal with. I always had to go into jEdit and save the UTF-8Y file again as UTF-8. But just in March EditPad Lite came out with a new version that allows you edit UTF-8 files without the BOM characters at the beginning and and EditPadLite will *leave it this way*, very nice. This means that if I need to make a quick change to a PHP file, I don't have to fire up and wait the 15 long seconds for Eclipse to commence: I simply pop into EditPadLite and edit it without fear of adding a BOM (hmm, interesting that it sounds like "BOMB" since that is exactly what it does to your PHP file, nice). In any case, EditPad Lite is now an ideal free text editor to edit any kind of text file in any encoding and to convert to and fro between the various encodings (e.g. between Windows 1252, ISO-8859-1, and UTF-8). You have the option to simply change the encoding setting of a file but not translate the characters or you change the encoding setting of a file and translate the characters as well--just the tool you need when you find yourself in encoding hell.
You can do a lot more with filters, e.g. dynamically change them in ActionScript but here is the syntax for a basic shadow filter on a text.
SHOW CODESHOW CODE
Johann: Hello,
I'am very new to Flex 3 and programming but like it a lot. I've a ne question: i'am using multiple hsliders and the sum of the values of those sliders may not be over 100(%), so when the first slider value=60 the second slider is limited to value=40. What AS function do I need to achieve this?
Check out this example here. This shows you the basics of using styles in flex, I did all of this with the FlexBuilder in design mode, very easy to use, when you are in a CSS file just click on design. The alpha transparency is wonderfully easy and the background picture leaves behind all the problems you have with page backgrounds in HTML/CSS, namely, it constantly resizes it, etc.
SHOW CODESHOW CODE
post comment or questionpost comment or question
tip
FLEX TIP - added on June 13, 2008
Flex Builder 3 has a design mode for CSS style sheets
When you are in a CSS file in Flex Builder 3, you can click on the Design button and see your styles as they will appear. You can change them and the CSS code will change in the background, it gives a library/symbol feeling to CSS files, nice.
post comment or questionpost comment or question
tip
ECLIPSE TIP - added on June 12, 2008
Code completion for PHP templates
When you make a PHP tempalte (like a macro) with Windows|Preferences|search for "templates"|PHP Templates, and you include $$${variable} in your template text, you will be presented with a dropdown of all available variables in the context where you call your template, nice.
This is another example of access filesystem this time with ActionScript. Did an expanding window in this example, but there are some scrolling issues on this window I need to work out, couldn't find a scrollable=false in the application element. To try this example, download adobe air and then you can install this application.
SHOW CODESHOW CODE
Pramod: Is it possible to access the File System in Flex web application, not AIR?
If it is, how it is done?
FileSystem type is not recognized in a Flex web application.
I want to upload a file in Flex web application?
Need Help.
Thanks in advance...
replyreply
post comment or questionpost comment or question
tip
FLEX TIP - added on June 10, 2008
Flex allows you to comment out attribute values
Flex Builder is full of surprises, here is another uncommon but useful feature: two slashes in front of an attribute value comments it out. ...addendum: any variable after the line with the double-slash will not be recognized in code complete = bug. oh well, 1 point for ingenuity.
This shows you the basics of getting access to files and directories with AIR using FileSystemTree and FileSystemDataGrid. Would be interesting to see how these work in Linux and Mac. To try this example, download adobe air and then you can install this application.
Check out the example here. This shows you how to make a custom component (LanguageSelector) that you call from your main MXML file. The component throws a custom event which you handle externally in your main MXML file as well.
SHOW CODESHOW CODE
Udayan: Can you please let me know, if the code for LanguageSelectedEvent.as you mistakenly put the code of LanguageSelector.mxml. As both LanguageSelector.mxml and LanguageSelectedEvent.as looks same.