📄 addtitle.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Chapter_09
{
/// <summary>
/// Summary description for AddTitle.
/// </summary>
public class AddTitle : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox title_id;
protected System.Web.UI.WebControls.TextBox title;
protected System.Web.UI.WebControls.TextBox type;
protected System.Web.UI.WebControls.TextBox pub_id;
protected System.Web.UI.WebControls.TextBox price;
protected System.Web.UI.WebControls.TextBox advance;
protected System.Web.UI.WebControls.TextBox royalty;
protected System.Web.UI.WebControls.TextBox ytd_sales;
protected System.Web.UI.WebControls.TextBox notes;
protected System.Web.UI.WebControls.TextBox pubdate;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid titlegrid;
private void Page_Load(object sender, System.EventArgs e)
{
DataSet MyDS = new DataSet();
DataTable TitleTable;
DataRow TitleRow;
string ConnStr;
ConnStr = @"server=(local)\VSdotNET;database=pubs;" +
"Trusted_Connection=yes";
string SQL;
SQL = "SELECT * FROM Titles";
SqlConnection MySqlConn = new SqlConnection(ConnStr);
SqlDataAdapter MySqlAdapter = new SqlDataAdapter(SQL,ConnStr);
SqlCommandBuilder MySqlCB = new SqlCommandBuilder(MySqlAdapter);
MyDS.ReadXmlSchema(Server.MapPath("AddTitle.xsd"));
if ( this.IsPostBack )
{
TitleTable = MyDS.Tables[0];
TitleRow = TitleTable.NewRow();
TitleRow["title_id"] = title_id.Text;
TitleRow["title"] = title.Text;
TitleRow["type"] = type.Text;
TitleRow["pub_id"] = pub_id.Text;
TitleRow["price"] = double.Parse(price.Text);
TitleRow["advance"] = double.Parse(advance.Text);
TitleRow["royalty"] = int.Parse(royalty.Text);
TitleRow["ytd_sales"] = int.Parse(ytd_sales.Text);
TitleRow["notes"] = notes.Text;
TitleRow["pubdate"] = DateTime.Parse(pubdate.Text);
TitleTable.Rows.Add(TitleRow);
//Update back-end table based on new row
MySqlAdapter.Update(MyDS);
//Reset dataset before filling with data from DB
MyDS.Reset();
//Fill dataset with data from the Titles table
MySqlAdapter.Fill(MyDS);
titlegrid.DataSource = MyDS.Tables[0].DefaultView;
titlegrid.DataBind();
}
else
{
//To prevent conflicts on multiple inserts, we can
//generate a random value for title_id
Random RandomNum = new Random();
title_id.Text = "XX" + String.Format("{0:000#}", RandomNum.Next(9999));
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -