How to Add SQL Database (.mdf) in ASP.NET Website

By
You can easily  Add .mdf Database in ASP.NET Application.You can perform Insertion Deletion and Updation operation on Database(.mdf).You can easily deploy your ASP.NET Application on any system without any problem.If you are facing any Type of   problem to Add .mdf Database then Read .


There are some steps to Add SQL Database (.mdf) in ASP.NET Website. Which are given below:
Step1:- First open your visual studio 2010-> go File-> click New Website->Select ASP.NET Empty website->click OK.
Go Solution Explorer->Add New web form(Default.aspx)-->Drag and drop some controls as shown below :-
see it:- 


Step2:- Now open Solution Explorer-> Right mouse click on website(Application) which is as  shown below:-
See it:-


Now Select SQL Server Database->click Add button ,which is shown below:-
see it:-


Step3:-  Now Double click on Database.mdf File in Solution Explorer->Now  Right click on Tables  Click Add New Table->Now Write required column Name-> Click save  button ->Write the table NAME-> Click OK. which is  as shown below :-
see it:-




Step4:-  Now Double click on Submit Button -> Write the following code for Database connectivity which are given below:-
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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=RAMASHANKER-PC;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");

    con.Open();
    String str = "INSERT INTO login (username,password) VALUES ('" + TextBox1.Text + "','" + TextBox2.Text + "')";
    SqlCommand cmd = new SqlCommand(str, con);
    int OBJ = Convert.ToInt32(cmd.ExecuteNonQuery());
    if (OBJ > 0)
    {
        Label3.Text = "Data is successfully inserted in database";
    }
    else
    {
        Label3.Text = "Data is not inserted in database";  
    }
    con.Close();

    }
}

see it:-


Step5:-  Run the Application(Press F5).Which are shown below:
Output:-



Step6:- Now you can see the inserted data in Database.
see it:-

For More:

  1. Generate unique Number and save it in the database
  2. Make Custom Registration and Login page With Ajax in ASP.NET
  3. E-Post System Project
  4. File handling  Real Application
  5. Make Captcha image for Asp.Net Application
  6. Host ASP.NET Application on Server Free
  7. Host Asp.Net Application on IIS Server
  8. Create a Setup File
  9.  Take Print receipt in Windows Form Application
  10. Print the Grid Data in Windows Forms Application
To Get the Latest  Free Updates Subscribe
Click below for Download whole Application
               DOWNLOAD

23 comments:

  1. wel thnks man, u saved me, its nw workng, i m able to do it, just need to try it in anothr project..thnks..

    ReplyDelete
  2. Really thanks

    ReplyDelete
  3. thnku you so much dude...

    ReplyDelete
  4. Thanks bro.

    Can you explain how to add sql server db to setup file for Windows application.

    ReplyDelete
  5. @sekhar!
    subscribe my site,so that you will get latest updates.I will get you latest updates wait...

    ReplyDelete
    Replies
    1. thanku very much!!!!!!!!!!!!!!!!!!!!!!!!!!!!for ur tutorials....without any error icompleted the set up process.............

      Delete
  6. Nice Explanation. Got lot's of new information. But i have some doubt. We did not add DB with this Setup file. Then how can we install this setup with others system??

    ReplyDelete
  7. @vijaydhas c visit this link: http://www.msdotnet.co.in/2013/07/how-to-create-setup-fileexe-with.html

    ReplyDelete
  8. hi dude i get an error while running the above application...the error is that
    The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)
    basicaly i get an error in the..................... using System.Linq;

    ReplyDelete
    Replies
    1. Remove this name space using System.Linq; then it will work fine............

      Delete
  9. Hi Brother,
    I like your work and am new to C# coding i have been majorly doing PHP and HTML and MySQLkindly help me with more tutorials as pdf or any thanks alot..
    EMAIL : basiljereh@gmail.com

    ReplyDelete
  10. sir how i add sqlconnetion on my pc like ur above code @"Data Source=RAMASHANKER-PC;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;"

    ReplyDelete
    Replies
    1. Hi ALVIN Roy !
      1.) RAMASHANKER-PC is SQL SERVER NAME. open your sql server --> Data Source = server name ;all other things will be same if your database name= Database.mdf ,otherwise change in your connection string then it will work perfectly.
      2.) If you want, where this string generated-->then follow add sql Data source on your page from toolbox--> configure it-->open below link and see step 4 then you can see this string.
      http://www.msdotnet.co.in/2014/07/how-to-implement-3-tier-architecture.html#more

      Delete
  11. DB file is missing in the example

    ReplyDelete
  12. thanks a ton, man. it works and i dont have to use access too.. awesome..

    ReplyDelete
  13. if the machine does not have installed sqlserver then it wont working I think?

    ReplyDelete
  14. Please how can I add access database MDF to my set up file

    ReplyDelete
  15. Very informative error.Thank you author for posting this kind of errors .


    http://www.wikitechy.com/fix-error/the-type-or-namespace-name-toint32-does-not-exist-in-the-namespace-convert



    Both are really good.
    Cheers,
    Venkat

    ReplyDelete
  16. PLEASE ACCEPT OUR POST AND REPLY
    WE REALLY NEED HELP

    THANKS IN ADVANCE

    ReplyDelete

Powered by Blogger.