Tuesday, 4 November 2014

Save Or Insert TextBox Value into Sql DataBase Table In Asp.Net Using C#.Net

11/04/2014 - By Pranav Singh 1

This  article will show you how you can save or insert the textbox value into sql server database in asp.net using c#.net. In this I have saved name and address in sql table. In this I have used SqlDataAdapter, SqlConnection to save the data.


Now open your sql server and now create the table as shown below.


Now check the table data.


Sql query to create the table.

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[UserDetail](
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [Name] [varchar](50) NULL,
      [Address] [varchar](100) NULL,
 CONSTRAINT [PK_UserDetail] PRIMARY KEY CLUSTERED
(
      [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

So for this article first I have created a new asp.net application and add below mention control on form. After adding all the controls your page code will look as shown below. 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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>Save Or Insert TextBox Value into Sql DataBase Table In Asp.Net Using C#</title>   
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%" cellpadding="4" cellspacing="4">
    <tr>
    <td colspan="2" align="center"><h3>USER DETAIL</h3></td>
    </tr>
        <tr>
            <td align="right">
                Name :
            </td>
            <td align="left">
                <asp:TextBox ID="txtName" runat="server" Width="200px" autocomplete="off"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right">
                Address :
            </td>
            <td align="left">
                <asp:TextBox ID="txtAddress" runat="server" Width="200px" autocomplete="off"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right">
                 </td>
            <td align="left">
                <asp:Label ID="lblmessage" runat="server" style="color: #FF3300"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="right">
                 </td>
            <td align="left">
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Now generate the button click event and add the below code into your .cs page.

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;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(System.Configuration.
ConfigurationManager.ConnectionStrings["con"].ToString());
            try
            {
                string query = "insert into UserDetail(Name,Address) 
values('" + txtName.Text + "','" + txtAddress.Text + "');";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.SelectCommand.ExecuteNonQuery();
                con.Close();
                lblmessage.Text = "Data saved successfully.";
            }
            catch
            {
                con.Close();
                lblmessage.Text = "Error while saving data.";
            }

        }
    }
}

In above code I have used SqlDataAdapter, SqlConnection to save the data. In above code I have used the connection string. In this you replace the connection with your connection string. Now insert quert to save the data. Now I have open the connection and ExecuteNonQuery to sql query.

Now we have done run the application to check the output.


Now add value and click on save button to save the data.

 

Now check the sql table


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

  1. Hi, would you know how to do this in VB 2010?

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top