Add a single piece of data to the MSSQLServer in asp.net

Source: Internet
Author: User
Tags mssqlserver

HTM page

The code is as follows Copy Code

<%@ 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 ">

&lt;html xmlns= "http://www.w3.org/1999/xhtml" &gt;


&lt;head id= "Head1" runat= "Server" &gt;


&lt;title&gt; Add Single Data instance &lt;/title&gt;


&lt;/head&gt;


&lt;body background= "Image/background.gif" &gt;


&lt;form id= "Form1" runat= "Server" &gt;


&lt;div&gt;


&lt;div style= "Text-align:center" &gt;


&lt;br/&gt;


&lt;br/&gt;


&lt;table style= "WIDTH:668PX; height:230px; Background-image:url (image/background1.gif); " &gt;


&lt;tr&gt;


&LT;TD colspan= "4" style= "height:133px" &gt;


&lt;asp:image id= "Image2" runat= "Server" imageurl= "~/image/head.gif"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD style= "width:100px; height:47px; " &gt;


&lt;/td&gt;


&LT;TD style= "width:100px; Background-image:url (image/background1.gif); Height:48px "colspan=" 2 "align=" Center "&gt;


&lt;asp:image id= "Image3" runat= "Server" imageurl= "~/image/mail.gif"/&gt;&lt;/td&gt;


&LT;TD style= "width:100px; height:47px; " &gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD style= "width:100px; height:19px; " &gt;


&lt;/td&gt;


&LT;TD style= "width:100px; Background-image:url (image/background1.gif); height:48px "align=" Center "&gt;


&lt;asp:image id= "Image4" runat= "Server" imageurl= "~/image/address.gif"/&gt;&lt;/td&gt;


&LT;TD style= "width:100px; Background-image:url (image/background1.gif); height:48px; " &gt;


&lt;asp:textbox id= "TextBox1" runat= "Server" &gt;&lt;/asp:TextBox&gt;&lt;/td&gt;


&LT;TD style= "width:100px; height:19px; " &gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD style= "width:100px; Height:48px "&gt;


&lt;/td&gt;


&LT;TD style= "Background-image:url (image/background1.gif); width:100px; height:48px "align=" Center "&gt;


&lt;asp:image id= "Image5" runat= "Server" imageurl= "~/image/title.gif"/&gt;&lt;/td&gt;


&LT;TD style= "Background-image:url (image/background1.gif); width:100px; height:48px "align=" left "&gt;


&lt;asp:textbox id= "TextBox2" runat= "Server" &gt;&lt;/asp:TextBox&gt;&lt;/td&gt;


&LT;TD style= "width:100px; Height:48px "&gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD style= "width:100px; Height:34px "&gt;


&lt;/td&gt;


&LT;TD style= "Background-image:url (image/background1.gif); width:100px; Height:34px "&gt;


&lt;asp:image id= "Image6" runat= "Server" imageurl= "~/image/neirong.gif"/&gt;&lt;/td&gt;


&LT;TD style= "Background-image:url (image/background1.gif); width:100px; Height:34px "align=" left "&gt;


&lt;asp:textbox id= "TextBox3" runat= "Server" textmode= "MultiLine" height= "72px" width= "147px" &gt;&lt;/asp:textbox &gt;&lt;/td&gt;


&LT;TD style= "width:100px; Height:34px "&gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD style= "width:100px; Height:48px "&gt;


&lt;/td&gt;


&LT;TD style= "Background-image:url (image/background1.gif); Height:48px "colspan=" 2 "align=" Center "&gt;


&lt;asp:imagebutton id= "Imgbtnsend" runat= "Server" imageurl= "~/image/send.gif" onclick= "Imgbtnsend_click"/&gt;


&lt;asp:imagebutton id= "Imgbtncz" runat= "Server" imageurl= "~/image/cz.gif"/&gt;&lt;/td&gt;


&LT;TD style= "width:100px; Height:48px "&gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD style= "width:100px" &gt;


&lt;/td&gt;


&LT;TD style= "Background-image:url (image/background1.gif); Height:48px "colspan=" 2 "&gt;


&lt;asp:image id= "Image7" runat= "Server" imageurl= "~/image/notice.gif"/&gt;&lt;/td&gt;


&LT;TD style= "width:100px" &gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD colspan= "4" &gt;


&lt;asp:gridview id= "GridView1" runat= "server" backcolor= "white" bordercolor= "#DEDFDE"


Borderstyle= "None" borderwidth= "1px" cellpadding= "4" font-size= "smaller" forecolor= "BLACK"


Gridlines= "Vertical" width= "720px" allowpaging= "True" autogeneratecolumns= "False" onpageindexchanging= "gridview1_" Pageindexchanging "pagesize=" 3 "&gt;


&lt;footerstyle backcolor= "#CCCC99"/&gt;


&lt;rowstyle backcolor= "#F7F7DE"/&gt;


&lt;selectedrowstyle backcolor= "#CE5D5A" font-bold= "True" forecolor= "white"/&gt;


&lt;pagerstyle backcolor= "#F7F7DE" forecolor= "Black" horizontalalign= "right"/&gt;


&lt;headerstyle backcolor= "#6B696B" font-bold= "True" forecolor= "white"/&gt;


&lt;alternatingrowstyle backcolor= "White"/&gt;


&lt;Columns&gt;


&lt;asp:boundfield datafield= "recipient Address" headertext= "Recipient Address"/&gt;


&lt;asp:boundfield datafield= "title" headertext= "title"/&gt;


&lt;asp:boundfield datafield= "Content" headertext= "content"/&gt;


&lt;/Columns&gt;


&lt;/asp:GridView&gt;


&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD colspan= "4" &gt;


&lt;asp:image id= "Image1" runat= "Server" imageurl= "~/image/foot.gif"/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;/table&gt;


&lt;/div&gt;





&lt;/div&gt;


&lt;/form&gt;


&lt;/body&gt;


&lt;/html&gt;

Index.cs file

The code is as follows Copy Code

Using System;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;

Using System.Data.SqlClient;


public partial class _default:system.web.ui.page


{


protected void Page_Load (object sender, EventArgs e)


{


if (! Page.IsPostBack)


{


This.bindgridview ()//Call custom Method-bound control data


}


}


public void Bindgridview ()


{


SqlConnection con = new SqlConnection (configurationsettings.appsettings["Strcon"));


Con. Open ();//Opening database connection


SqlDataAdapter ada = new SqlDataAdapter ("Select * from tb_send05", con);//Create Data adapter


DataSet ds = new DataSet ();//Create Data set


Ada. Fill (DS)//Data set


Gridview1.datasource = ds;//bound data


Gridview1.databind ();


Con. Close ();


}


protected void Imgbtnsend_click (object sender, ImageClickEventArgs e)


{


Try


{


SqlConnection con = new SqlConnection (configurationsettings.appsettings["Strcon"));


Con. Open ();


String insertsql = "Insert into tb_send05 values (' + TextBox1.Text +" ', ' "+ TextBox2.Text +" ', ' "+ TextBox3.Text +") " ;


SqlCommand com = new SqlCommand (Insertsql, con);//Create SqlCommand object to execute SQL command


Com. ExecuteNonQuery ()//Add action


SqlDataAdapter ada = new SqlDataAdapter ("Select * from tb_send05", con);//Create Adapter


DataSet ds = new DataSet ();


Ada. Fill (DS);


Gridview1.datasource = ds;


Gridview1.databind ();


Con. Close ();


Response.Write ("&lt;script Language=javascript&gt;alert") (' Add success! ') &lt;/script&gt; ");


}


Catch


{


Response.Write ("&lt;script Language=javascript&gt;alert") (' Add failed! ') &lt;/script&gt; ");


}


}


protected void Gridview1_pageindexchanging (object sender, Gridviewpageeventargs e)


{


This. Gridview1.pageindex = E.newpageindex;


This.bindgridview ();


}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.