📄 st_roomaddmodule.ascx.cs
字号:
namespace ST_GROUP.Modules
{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
/// <summary>
/// ST_RoomAddModule 的摘要说明。
/// </summary>
public partial class ST_RoomAddModule : ST_ModuleBase
{
private void Page_Load(object sender, System.EventArgs e)
{
//绑定房间类型信息下拉列表框
if(!IsPostBack)
{
//从文件Web.config中读取连接字符串
string ST_sqldb= ConfigurationSettings.AppSettings["ConnectionString"];
//连接ST_GinShopManage数据库
SqlConnection ST_Conn= new SqlConnection (ST_sqldb);
ST_Conn.Open ();
//定义sql语句
string ST_mysql="select ST_RCategoryId,ST_Name from ST_RoomCategory ";
SqlCommand ST_cm=new SqlCommand (ST_mysql,ST_Conn);
SqlDataReader dr=ST_cm.ExecuteReader ();
while(dr.Read ())
{
ListItem li=new ListItem(dr["ST_Name"].ToString(),dr["ST_RCategoryId"].ToString());
RCategoryNameList.Items.Add (li);
}
ST_Conn.Close ();
}
}
//验证房间号是否已登记
public void IsIdValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
//从文件Web.config中读取连接字符串
string ST_sqldb= ConfigurationSettings.AppSettings["ConnectionString"];
//连接ST_GinShopManage数据库
SqlConnection ST_Conn= new SqlConnection (ST_sqldb);
ST_Conn.Open ();
//构造SQL语句,该语句在RoomCategory表中检查房间类型是否已存在
string checksql= "select * from ST_RoomsInfo where ST_RoomId='"+RoomIdTextBox.Text.Trim() +"'";
//创建Command对象
SqlCommand ST_mycommand=new SqlCommand (checksql,ST_Conn);
//执行ExecuteReader ()方法
SqlDataReader dr=ST_mycommand.ExecuteReader ();
if(dr.Read ())
{
args.IsValid =false;//房间号已存在
}
else
{
args.IsValid =true;//房间号未登记
}
//关闭连接
ST_Conn.Close();
}
//房间类型是否已选定
public void NotNullValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
if(RCategoryNameList.SelectedIndex==0)
{
args.IsValid =false;//房间类型未选
}
else
{
args.IsValid =true;//房间类型已选
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SubmitButton.Click += new System.EventHandler(this.SubmitButton_Click);
this.ReturnButton.Click += new System.EventHandler(this.ReturnButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void SubmitButton_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
//从文件Web.config中读取连接字符串
string ST_sqldb= ConfigurationSettings.AppSettings["ConnectionString"];
//连接ST_GinShopManage数据库
SqlConnection ST_Conn= new SqlConnection (ST_sqldb);
ST_Conn.Open ();
//利用Command对象调用存储过程
SqlCommand ST_mycommand=new SqlCommand ("ST_InsertRoom",ST_Conn);
//将命令类型转为存储类型
ST_mycommand.CommandType =CommandType.StoredProcedure ;
//往存储过程中添加参数
ST_mycommand.Parameters .Add ("@RoomId",SqlDbType.Int);
ST_mycommand.Parameters .Add ("@RCategoryId",SqlDbType.Int);
ST_mycommand.Parameters .Add ("@RPosition",SqlDbType.NVarChar);
ST_mycommand.Parameters .Add ("@Description",SqlDbType.NVarChar);
//给存储过程的参数赋值
ST_mycommand.Parameters ["@RoomId"].Value =int.Parse(RoomIdTextBox.Text.Trim());
ST_mycommand.Parameters ["@RCategoryId"].Value = RCategoryNameList.SelectedIndex;
ST_mycommand.Parameters ["@RPosition"].Value =RPositionTextBox.Text.Trim();
ST_mycommand.Parameters ["@Description"].Value =DescriptionTextBox.Text.Trim();
try
{
ST_mycommand.ExecuteNonQuery();
ShowMsg.Text="新房间信息添加成功";
ShowMsg.Style["color"]="green";}
catch(SqlException error)
{
ShowMsg.Text="添加未成功,请稍后再试。原因:"+error.Message;
ShowMsg.Style["color"]="red";
}
//关闭连接
ST_Conn.Close();
}
}
private void ReturnButton_Click(object sender, System.EventArgs e)
{
Response.Redirect(PathPrefix+"/ST_RoomsMan.aspx");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -