A few examples from the DataGrid have learned to read data from the database, so we learn to insert the data into the databases, add a simple input form to the page, and execute the Insert command in the form submission event handler. As with the first two examples, the value of the command is populated with the Parameters collection of the Command object. Note that before attempting to insert into the database, check to make sure that the value you want is not empty. This prevents accidental conflicts with the database's field constraints. You also need to execute the INSERT command inside the Try/catch block to prevent the primary key of the inserted row from already existing.
Aspx.cs file
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;
#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Submit1.serverclick + = new System.EventHandler (this. Submit1_ServerClick);
This. Load + = new System.EventHandler (this. Page_Load);
SqlCommand mycommand = new SqlCommand (Insertcmd, MyConnection);
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Id", SqlDbType.NVarChar, 11));
mycommand.parameters["@Id"]. Value = au_id. Value;
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@LName", SqlDbType.NVarChar, 40));
mycommand.parameters["@LName"]. Value = au_lname. Value;
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@FName", SqlDbType.NVarChar, 20));
mycommand.parameters["@FName"]. Value = au_fname. Value;
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Phone", SqlDbType.NChar, 12));
mycommand.parameters["@Phone"]. Value = phone. Value;
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Address", SqlDbType.NVarChar, 40));
mycommand.parameters["@Address"]. Value = Address. Value;
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@City", SqlDbType.NVarChar, 20));
mycommand.parameters["@City"]. Value = City. Value;
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@State", SqlDbType.NChar, 2));
mycommand.parameters["@State"]. Value = state. Value;
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Zip", SqlDbType.NChar, 5));
mycommand.parameters["@Zip"]. Value = zip. Value;
MYCOMMAND.PARAMETERS.ADD (New SqlParameter ("@Contract", sqldbtype.nvarchar,1));
mycommand.parameters["@Contract"]. Value = contract. Value;
MyCommand.Connection.Open ();
Try
{
Mycommand.executenonquery ();
message.innerhtml = "<b> added record </b><br>" + Insertcmd + "<p>";
}
catch (SqlException ex)
{
if (ex. Number = 2627)
message.innerhtml = "Error: Record with same primary key already exists <p>";
Else
message.innerhtml = "Error: Failed to add record, make sure the field <p>" is filled in correctly;
message.style["COLOR"] = "red";
}
MyCommand.Connection.Close ();
}
Bindgrid ();
}
public void Bindgrid ()
{
SqlDataAdapter mycommand = new SqlDataAdapter ("select * from Authors", MyConnection);
DataSet ds = new DataSet ();
Mycommand.fill (ds, "Authors");
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.