Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Inserting Data into SQL Server Database using Csharp and ASP.NET

In this article you will see how to insert some values in sql server tables, very usefull to many programs.

Introduction:

The data storage plays a very important role while developing any application. This tutorial will cover that how to create a SQL server express database with VS2008 and make a connection with it. The tutorial will also cover the data insertion using the ASP.NET and C#.

  • Step-1: Create ASP.NET Controls
  • Step-2: Create Database and Table in SQL Server
  • Step-3: Create Connection in web.config

Create ASP.NET Controls:

Open Visual Studio 2008; now go on File Menu -> Open New Website, after that open default.aspx page. On default page take & drop Textboxes, Button, as shown in given figure 1.

Initial UI with some fields

Figure 1: Initial UI with some fields

Listing 1: ASP.NET Script providing input for database insert

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MrBool.com – Interacting with Database</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table style="width:900px; height:100px; vertical-align:top">
    <tr>
    <td style="width:600px; padding-left:200px">
    <b style="padding-left:65px">id: </b>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <b style="padding-left:40px">Name:</b>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <b>Designation:</b>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
        <b style="padding-left:5px">Mobile_No:</b>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
        <b style="padding-left:26px">Address:</b>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click"/>
       
    </td>
    </tr>
    </table>
    
    </div>
    </form>
</body>
</html>

Create Database and Table in SQL Server:

Let’s now start with creating the database table using visual studio 2008. Open the solution explorer right click on website add new item – open item templates select SQL Server Database click on add button after that show message box click on “Yes” button.

Now follow the following steps to create the table:

  1. Go on Server explorer open database.mdf and right click on table
  2. Create new table
  3. Now insert field as Id(Int), Name(varchar(50)), Designation(varchar(50)), Mobile_No(Int), Address(varchar(50)).

SQL Server Database Table with given Columns

Figure 2: SQL Server Database Table with given Columns

After create database go on default.aspx page and double click on Submit Button and write down some code:

Listing 2: C# code to insert data in database


using System.Data.SqlClient;


protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection cnn = new SqlConnection();
        cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnection "].ConnectionString;
        cnn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from  TableName";
        cmd.Connection = cnn;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds, " TableName ");
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        DataRow drow = ds.Tables["TableName"].NewRow();
        drow["id"] = TextBox1.Text;
        drow["Name"] = TextBox2.Text;
        drow["Designation"] = TextBox3.Text;
        drow["Mobile_No"] = TextBox4.Text;
        drow["Address"] = TextBox5.Text;

        ds.Tables["TableName "].Rows.Add(drow);
        da.Update(ds, " TableName ");
        string script = @"<script language=""javascript"">
        alert('Information have been Saved Successfully.......!!!!!.');
       </script>;";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "myJScript1", script);
    }

Create Connection in Web.Config:

Now you have to create a web.config connection entry to connect to database. Note that this connection requires a Data Source, DB File Name, and provider name.

Listing 3: Web.config Entry for Connection Details

<connectionStrings>
<add name="sqlconnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
	<system.web>

Conclusion:

By following the above steps you can create a SQL express connection and make a small database application. My future articles will also cover some advance topics with database and ASP.NET.

Please comment below in case of any queries.



I am having total 7+ years of experience in developing applications using C#, VB.NET, ASP.NET, Silverlight, WCF, SQL Server and Oracle. I am holding a M.Tech degree in Intelligent systems. For more information, visit my Linkedin p...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login