Skip to main content

Posts

Showing posts from March, 2011

Get a Random Row From a SQL Databse with ASP 4.0 and C#

This tutorial will demonstrate how to get a random row from your SQL database using ASP.NET 4.0 and C#.  Selecting random rows from a database can be useful for many different purposes such as displaying random images, quotes, products, or anything else that your database may contain. In this example we will choose a random number from a database. Adding the Default.aspx Page At this point in the tutorial I have created a new ASP.NET Empty Web Site. What we need to do first is add in a blank Web Form that we will use to test our SQL query and ensure that we are getting the correct results. To do this: Right click the project in your Solution Explorer. Select Add New Item... Select a Web Form and name it Default.aspx. Click Add. We migrated our web sites to  Server Intellect  over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With  Server Intell...

Generating Random Number and String in C#

The Random class defined in the .NET Framework class library provides functionality to generate random numbers. The Random class constructors have two overloaded forms. It takes either no value or it takes a seed value. The Random class has three public methods - Next, NextBytes, and NextDouble. The Next method returns a random number, NextBytes returns an array of bytes filled with random numbers, and NextDouble returns a random number between 0.0 and 1.0. The Next method has three overloaded forms and allows you to set the minimum and maximum range of the random number. The following code returns a random number: int  num = random.Next(); The following code returns a random number less than 1000. int  num = random.Next(1000); The following code returns a random number between min and max: private   int  RandomNumber( int  min,  int  max) { Random random =  new  Random(); return  random.Next(min, max); ...