How to Connect ASP.NET Web Page with MS SQL DATABASE (For beginners)

Hello Friends ,
 After almost 4-5 months I am back this time with Database connectivity in asp.net Web page with C#
Most of you know about Database , What Exactly database is ? and all these  Today we learn how to connect Database with ASP.NET webpage using C#

First of all we know that we use Very Powerful IDE for ASP.NET Web Developing i.e Visual Studio

So Let Understand STEP by STEP With the help of Login Page :

Step :1  Open a New Website In Visual Studio (open Empty ASP.net Website )

Step 2: Add a aspx web page then you see like this


Step 3: Now use 2 Labels , 2 TextBox , 1 Botton to make a login page or use these codes:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="position:absolute;left:40%; top:32%; background:#e2e2e2; width:52%; height:233px;">

       User Name: <asp:TextBox ID="TextBox1" runat="server" 
            style="position:absolute; top: 1px; left: 140px;"></asp:TextBox><br />
        <br />
        <br />
        <br />
        Password<br />
        <asp:TextBox ID="TextBox2" style="position:absolute; top: 73px; left: 136px;" 
            runat="server" TextMode="Password"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" 
            style="position:absolute; top: 138px; left: 130px;" />
    </div>
    </form>
</body>
</html>

Step 4:  Now We do important part Go to VIEW>SERVER EXPLORER
                    Something Looks like this:

Now Right Click on Data Connection there is a option Add Database and Create New SQL Server Database

If you already have Database created USE ADD DATABASE 
other wise Create New SQL SERVER DATABASE;

I am creating new Database called EMPLOYEE

when you select new sql server database there is a dialog box open
provide the SERVER NAME AS .  (single dot)
and write the database name what ever you want I use employee
and use Windows authentication :

Now your database is connected with the ASP.net Website But you have to work little bit with that
Now expand the database and there is a option of TABLES
Now Right click on table and add new table



Now create a table 


Now the main Part of the Connectivity  is coming :

 you have to go to design view of webpage and double click on button then you see the code behind codes :

In Code behind codes 
ADD 2 LIBRARIES
1. using System.Data.SqlClient;
2. using System.Data;

after this
 write these codes in your button_click event function


    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=employee;Integrated Security=True;Pooling=False");
    SqlCommand mycom;
  

        con.Open();
        mycom=new SqlCommand("select * from emp",con);
        SqlDataReader myrdr = new SqlDataReader();
        myrdr = mycom.ExecuteReader();
        if (myrdr.HasRows)
        {
            while (myrdr.Read())
            {
                if (TextBox1.Text == myrdr["empid"].ToString() && TextBox2.Text == myrdr["pass"].ToString())
                {
                    Response.Redirect("newpage.aspx");
                }
                else
                {
                    Response.Write("Please enter correct username and password");
                }
            }
        }
       
    }

 the codes looks like 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=employee;Integrated Security=True;Pooling=False");

    SqlCommand mycom;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        mycom=new SqlCommand("select * from emp",con);
        SqlDataReader myrdr = new SqlDataReader();
        myrdr = mycom.ExecuteReader();
        if (myrdr.HasRows)
        {
            while (myrdr.Read())
            {
                if (TextBox1.Text == myrdr["empid"].ToString() && TextBox2.Text == myrdr["pass"].ToString())
                {
                    Response.Redirect("newpage.aspx");
                }
                else
                {
                    Response.Write("Please enter correct username and password");
                }
            }
        }
       
       

    }

}

 That's all about Connecting ASP.NET with Database 
If you have any query and any problem regarding this Leave your valuable comment I try to solve your problem .





How to Connect ASP.NET Web Page with MS SQL DATABASE (For beginners) How to Connect ASP.NET Web Page with MS SQL DATABASE (For beginners) Reviewed by Admin on 04:26 Rating: 5

2 comments:

  1. It's always interesting to read through articles from other writers and use a little something from other websites.
    San Francisco web design

    ReplyDelete
    Replies
    1. Yeah its always that what we call sharing is always caring lol
      Thanks for your valuable Comment :)

      Delete

Follow and +1 Us

Powered by Blogger.