This tutorial will show you how to access tables in database. Use the same database as we used in previous tutorial. You can also download the database script here
Problem statement – Put a simple login box on page containing username & password. Once you click submit, it will either display ‘successful’ msg, or failed (if the entry does not contain in the connected database table)
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 DB_Connection : System.Web.UI.Page
{
public SqlConnection connection;
public SqlCommand command; public SqlDataAdapter adapter; public SqlDataReader reader;
public DataSet ds;
public int recordsAffected;
public string connectionString = "Data Source=.\\SQLEXPRESS; Initial Catalog=Student; Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
int success=connectDB();
if (success == 1)
{
try
{
if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
string username = txtUsername.Text;
string password = txtPassword.Text;
string query = "select * from login_credentials where user_name='" + username + "' and password='" + password + "'";//"Insert into login_credentials values('" + username + "','" + password + "')";
command = new SqlCommand(query, connection);
reader = command.ExecuteReader();
bool isSelected = reader.HasRows;
if (isSelected)
{
lblMessage.Text = "Login Successful";
}
else
{ lblMessage.Text = "Login Failed"; }
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
finally
{
connection.Close();
command.Dispose();
}
}
else
{
lblMessage.Text = "Can't Connect to Database";
}
}
public int connectDB()
{
try
{
connection = new SqlConnection(connectionString);
return (1);
}
catch (Exception ex)
{
return (0);
}
}
}
Here we are inputting username & password and checking across the database if they are valid or not with the help of query.
Download complete code here
asp.net, c#.net simplified..
This is tutorial of asp.net, c#.net. Prerequisite - the reader must be good at any one computer language. We intend to demonstrate the use of different controls provided by these technologies.
Tuesday, June 1, 2010
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.
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.
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.
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.
Subscribe to:
Posts (Atom)