我在DataGrid的模板列中绑定了Textbox控件控件,但是不知道怎么将写入Textbox中的数据读到数据库里,我的代码如下:
这是WEB层代码:
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;
using System.Configuration;
using System.Text;
using TimeSheet.TimeSheetManager.Biz;
using TimeSheet.SecurityService.Service;
namespace TimeSheet.TimeSheetManager.WEB
{
/// <summary>
/// TimeSheetView2 的摘要说明。
/// </summary>
public class TimeSheetView2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button btnSave;
//protected System.Web.UI.WebControls.Calendar.ReferenceEquals;
private TS tS;
DataSet dataSet = new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
tS=new TS();
if(!IsPostBack)
{
//进行数据绑定
BindGrid();
}
}
//序号
// public int getrecord(int itemIndex)
// {
// return (DataGrid1.CurrentPageIndex*DataGrid1.PageSize+itemIndex+1);
// }
private void BindGrid()
{
if(tS == null)
{
tS = new TS();
}
DataSet dataSet = new DataSet();
dataSet = tS.SearchTimeSheetView();
DataGrid1.DataSource = dataSet.Tables[0].DefaultView;
DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
}
}
private void btnSave_Click(object sender, System.EventArgs e)// 这是那段读取数据代码,不知道对不对?
{
DataTable table = new DataTable();
table.Columns.Add("ProjectID",typeof(int));
table.Columns.Add("NodeID",typeof(int));
table.Columns.Add("TS_Rtime",typeof(DateTime));
table.Columns.Add("WorkTime",typeof(string));
table.Columns.Add("OverTime",typeof(string));
table.Columns.Add("TS_Comment",typeof(string));
DataRow row;
foreach(DataGridItem item in this.DataGrid1.Items)
{
row = table.NewRow();
row["ProjectID"]=Convert.ToInt32(item.Cells[1].Text.Trim());
row["NodeID"] = Convert.ToInt32(item.Cells[2].Text.Trim());
row["TS_Rtime"] =((Calendar)item.Cells[3].Controls[1]).SelectedDate.Date;
row["WorkTime"] =((TextBox)item.Cells[4].Controls[1]).Text.Trim();
row["OverTime"] =((TextBox)item.Cells[5].Controls[1]).Text.Trim();
row["TS_Comment"] =((TextBox)item.Cells[6].Controls[1]).Text.Trim();
table.Rows.Add(row);
tS.AddTimeSheetsList(table);
//BindGrid();
}
}
下面是BIE层代码,主要是tS.AddTimeSheetsList(table)的定义:
public void AddTimeSheetsList(DataTable table)
{
//tS.AddTimeSheetsList( nTS_Rtime,nWorkTime,nOverTime,strTS_Comment);
foreach(DataRow row in table.Rows)
tS.AddTimeSheetsList(Convert.ToString(row[0]),Convert.ToString(row[1]),Convert.ToString(row[2]),Convert.ToString(row[3]),Convert.ToString(row[4]));
}
下面是DAO层代码,主要是tS.AddTimeSheetsList的定义:
public void AddTimeSheetsList(string projectID,string nodeID ,string workTime,string overTime,string tS_Comment)
{
string sql = "PageInfo_Add";
SqlParameter[] arrSqlParameter = new SqlParameter[5];
SqlParameter sqlParameter1 = new SqlParameter("@ProjectID",SqlDbType.Int,4);
sqlParameter1.Value =projectID;
SqlParameter sqlParameter2 = new SqlParameter("@NodeID",SqlDbType.Int,4);
sqlParameter2.Value =nodeID;
SqlParameter sqlParameter3 = new SqlParameter("@WorkTime",SqlDbType.VarChar,50);
sqlParameter3.Value =workTime;
SqlParameter sqlParameter4 = new SqlParameter("@OverTime",SqlDbType.VarChar,50);
sqlParameter4.Value =overTime;
SqlParameter sqlParameter5 = new SqlParameter("@TS_Comment",SqlDbType.VarChar,200);
sqlParameter5.Value =tS_Comment;
arrSqlParameter[0] = sqlParameter1;
arrSqlParameter[1] = sqlParameter2;
arrSqlParameter[2] = sqlParameter3;
arrSqlParameter[3] = sqlParameter4;
arrSqlParameter[4] = sqlParameter5;
DB.RunProc(sql,arrSqlParameter);
}
下面是存储过程PageInfo_Add:
CREATE PROCEDURE
PageInfo_Add
(
@projectID int,
@nodeID int,
@workTime varchar (50),
@overTime varchar (50),
@tS_Comment varchar(200)
)
AS
Insert INTO TimeSheets (ProjectID,NodeID,WorkTime,OverTime,TS_Comment)
Values (@projectID,@nodeID,@workTime,@overTime,@tS_Comment)
GO
请大家帮忙看看,哪里出了问题?谢谢!