Insert data into database
source :
<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
first name
<asp:TextBox ID="fistnametxt" runat="server"></asp:TextBox>
<br />
<br />
last name
<asp:TextBox ID="lastnametxt" runat="server"></asp:TextBox>
<br />
<br />
username
<asp:TextBox ID="usernametx t" runat="server"></asp:TextBox>
<br />
<br />
password
<asp:TextBox ID="passwordtxt" runat="server"></asp:TextBox>
<br />
<br />
compair-password
<asp:TextBox ID="conpasswordtxt" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
Text="create" />
<br />
<br />
</div>
</form>
</body>
</html>
default.aspx.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
SqlCommand com;
string str;//connection string variable
protected void Page_Load(object sender, EventArgs e)
{
str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\alex\Documents\Visual Studio 2010\WebSites\insertdata\App_Data\Database.mdf;Integrated Security=True;User Instance=True";
con = new SqlConnection(str);
con.Open();//open your connection
}
protected void Button1_Click1(object sender, EventArgs e)
{
com = new SqlCommand("insert into data values('" + fistnametxt.Text + "','" + lastnametxt.Text + "','" + usernametxt.Text + "','" + passwordtxt.Text + "')", con);
int a=com.ExecuteNonQuery();
if (a == 1)
{
Response.Write("<script>alert('data inserted')</script>");
}
else
{
Response.Write("<scrip>alert('data not inserted ')</script>");
}
}
}
Comments
Post a Comment