Intro to SQL Server with ASP.NET - Page 4
       by kirupa  |  25 December 2006

After the behind-the-scenes work you performed in the previous page, you are finally at a point where your users can directly see what you are working on.

Creating the Input Form
We are finally finished with setting up our database and related details. It's all easy coasting and fun from here! In your Games.aspx page, create two textboxes and and a Submit button. It is entirely up to you on how you decide to design your form, but to give you an idea, here is how my form looks like:

[ how my input form for sending data to the database looks like ]

Regardless of how your form looks, make sure that your Name textbox has the ID txtGameName, the Platform textbox has the ID txtGamePlatform, and the Submit button has the name btnSubmit.

The code that I will have you write will refer to the above control names. Speaking of code, double-click on your Submit button to open your Games.aspx.cs file. Fill in the lines of code that are missing from your project (highlighted in yellow):

I will explain in greater detail what the lines of code do later, but for now, I first open a connection to the database, add in the data from our two textboxes, execute the commands to modify the database, and then close the connection.

Anyway, it's time to test our application. Press Ctrl + F5 to launch the browser and run our program. Type in some data into both of your textboxes and press the Submit button. You will receive a Page not Found message because we haven't created our Results.aspx file yet, so don't worry. If you do not receive any errors, proceed to the next page.

If You are Receiving Errors
Depending on your computer setup, you may be asked via a scary yellow-colored error page to add some extra parameters to your Web.Config file. From earlier, you should be familiar with the ConnectionStrings area of Web.Config, and, for example, in Vista, I was requested to add the Asynchronous Processing=True line to my Web.Config's ConnectionString node.

For reference purposes, here is my full ConnectionString:

<connectionStrings>
<add name="GamesConnection" connectionString="Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\Games.mdf; Integrated Security=True;User Instance=True; Asynchronous Processing=True" providerName="System.Data.SqlClient" />
</connectionStrings>

If your errors are more sinister errors, doing a simple web search for the error text should help you solve the problem. I have often found that closing Visual Studio and then manually visiting the localhost URL in the browser solves many problems also - especially "Cannot open user default database" errors.

If you find that you are unable to solve your database error, please post your questions on the forums, and I or somebody else can help provide solutions to them.

For everybody else, there is always the next page where we talk about viewing the data you just added.

Onwards to the next page!

1 | 2 | 3 | 4 | 5 | 6




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.