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.
Some of my previous articles are as follows: Insert,
Update, Delete Operation In Asp.Net MVC Using Entity Framework C#.Net ,CRUD
Functionality, CalendarExtender
Example With Custom Style By Css Uaing Asp.net and Css, How
to Create Tab Control Using jQuery In Asp.Net, User
Registration Form With ToolTip Message and Validation Using jQuery, C#.Net in
Asp..Net, Link
Button or HyperLink With Mouse Tracking ToolTip Using jQuery in Asp.Net, jQuery
Message Open As Modal Dialog Box In Asp.Net Without Refresh With Ok Button.
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
Hi, would you know how to do this in VB 2010?
ReplyDelete