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;
using System.Data;
using System.Web.Configuration;
public partial class blog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ DataList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!TextBox1.Text.Equals("") && !TextBox2.Text.Equals(""))
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ARJUN\Documents\Visual Studio 2010\WebSites\blog\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
con.Open();
string com = "INSERT INTO blog (Name,Message) VALUES (@name,@message)";
SqlCommand cmd = new SqlCommand(com, con);
cmd.Parameters.Add(new SqlParameter("@name", TextBox1.Text));
cmd.Parameters.Add(new SqlParameter("@message", TextBox2.Text));
cmd.ExecuteNonQuery();
TextBox1.Text = "";
TextBox2.Text = "";
Label1.Text = "Success";
con.Close();
}
else{
Label1.Text = "enter";
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="blog.aspx.cs" Inherits="blog" %>
<!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>
<br />
<br />
<br />
<br />
<asp:DataList ID="DataList1" runat="server" DataKeyField="Id"
DataSourceID="SqlDataSource2">
<ItemTemplate>
Id:
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
<br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
<br />
Message:
<asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Message") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [blog] WHERE [Id] = @Id"
InsertCommand="INSERT INTO [blog] ([Name], [Message]) VALUES (@Name, @Message)"
SelectCommand="SELECT * FROM [blog]"
UpdateCommand="UPDATE [blog] SET [Name] = @Name, [Message] = @Message WHERE [Id] = @Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Message" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Message" Type="String" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
Sir whenever i refresh the page the value from database update automatically even when i am not inserting
Database table
Id in primary key
Name nvarchar max
Message nvarchar max