Pages

Monday, May 24, 2010

Database access in C# with MSSQL 2005

This tutorial is a step by step guide for accessing the database in MSSQL 2008 using C#.
Create a database named – student. Tables 1. Student info 2. Login_credentials.
Download this file to get database script. Run it in your sql server.

Prob statement - If database connection is successful, then display the message showing information about which database it is connected to, data source & version number of database engine
C# code –

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page
{
public SqlConnection connection;
public string connectionString = "Data Source=.\\SQLEXPRESS; Initial Catalog=Student; Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
try
{
connection = new SqlConnection(connectionString);
if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
lblMessage.Text = "Connection to '" + connection.Database + "' database on database engine"+connection.DataSource+" having version "+connection.ServerVersion+ " is successful";
}
}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
}
finally
{
connection.Close();
}
}
}

Try running the code in c#. Here is the complete source code.
The code is pretty self-explanatory. Just go through it and let us know if you have any doubt.

Thursday, May 20, 2010

Displaying ‘Hello world’ message using C#.

This tutorial demonstrates how to print/display message on screen. This is meant for novice programmers.

Open aspx page of the new website. Drag 1 label & 1 button from the toolbox at the left.
Have a look at images below. When you click on the button, the message is displayed.




On the button click we will get result like this





C# Code-

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class HelloWorld : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Event fires when page loads on each control event
}
protected void btn_Click(object sender, EventArgs e)
{
lblMessage.Text = "Hello World. Welcome to ASP.NET";
}
}

'lblMessage' is the name of label control
Here a simple label is shown on the button click event.
Page_load is a function which is triggered on the loading of page. So if you want to print the message right after the loading of page, then instead of using button click event, write that code in page_load section!

Click here to download complete code. Enjoy.

Wednesday, May 19, 2010

Tutorial 1 - asp.net, c#.net

In this series of tutorials, you are going to learn how to make websites in asp.net with c#.net
We intend to explain examples here. The concepts are covered with the examples. It is expected that you will do some R&D on those examples.

Start visual studio. Click File -> New -> website. You will get window like this.



Note those 2 highlighted fields.

We will be working on simple ‘ASP.NET website’. Once you click ok, it visual studio will create a folder for you with two files. One file will have extension as ‘.aspx’, while other will have ‘.aspx.cs’.
Every webpage you create will have these 2 files.

.aspx – this is the file which will be displayed on the screen whenever one accesses the webpage/website you have developed. It is GUI oriented, meaning, it has HTML code (and some pure ASP code), according to which the site will be displayed on the screen.
.aspx.cs – it’s where you put your logic. E.g. if you want to show something right on ‘page load’, you can write it under ‘page_load’ function. All the logical/algorithmic part of the program is written in this file.

If we want to put some controls on webpage (like textbox, button, label etc. ), we do that on this .aspx page. And the manipulation of these controls is done on .aspx.cs page.

In all the subsequent tutorials, always pay heed to both the files.