private void showinfo_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string idstring=e.Item.Cells[1].Text;
string nastring=(((TextBox)e.Item.Cells[2].Controls[0]).Text).ToString();
string tystring=(((TextBox)e.Item.Cells[3].Controls[0]).Text).ToString();
string phstring=(((TextBox)e.Item.Cells[4].Controls[0]).Text).ToString();
string strup="Update setdeptinfo set deptname='"+nastring+"',depttype='"+tystring+"',deptphone='"+phstring+"' where deptid='"+idstring+"'";
OleDbCommand mycmd=new OleDbCommand(strup,showconn);
mycmd.ExecuteNonQuery();
showinfo.EditItemIndex=-1;
BindGrid(); }
{
string idstring=e.Item.Cells[1].Text;
string nastring=(((TextBox)e.Item.Cells[2].Controls[0]).Text).ToString();
string tystring=(((TextBox)e.Item.Cells[3].Controls[0]).Text).ToString();
string phstring=(((TextBox)e.Item.Cells[4].Controls[0]).Text).ToString();
string strup="Update setdeptinfo set deptname='"+nastring+"',depttype='"+tystring+"',deptphone='"+phstring+"' where deptid='"+idstring+"'";
OleDbCommand mycmd=new OleDbCommand(strup,showconn);
mycmd.ExecuteNonQuery();
showinfo.EditItemIndex=-1;
BindGrid(); }
解决方案 »
- JQuery 或者说js 事件绑定的问题
- C# 控制打印机打印office文件
- 用js的返回history.back(),但在中途遇到警告框时就停止返回了,怎样解决???
- 如何将一个空间里的数据传送到另一个空间里面去?
- 我把数据加载到 Hashtable 中,前台调用出错
- 关于DataGrid的动态绑定列问题
- 怎样判断一个字符串是不是能转换成数字类型
- C# asp.net下面怎么样把DataTable数据导入Excel呀,郁闷!!
- 在TEXTBOX上按下回车后,如何跳转焦点到指定BUTTON上,而且,执行Button_Click事件
- 一个挺简单的问题,请大家解决,
- 刚刚接触
- 数据库更新的问题,神仙救救我吧!!!!我要急死了!!!!!!!!
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.OleDb;
namespace WebApplication2
{
/// <summary>
/// showdeptinfo 的摘要说明。
/// </summary>
public class showdeptinfo : System.Web.UI.Page
{
protected System.Data.DataSet ds;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlGenericControl FONT1;
protected System.Web.UI.WebControls.Button gosetdetp;
protected System.Web.UI.WebControls.Button gomain;
protected System.Web.UI.WebControls.DataGrid showinfo;
OleDbConnection showconn;
public void Page_Load(object sender, System.EventArgs e)
{
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
strConnection+=Server.MapPath("cnetdata\\cnet.mdb");
//OleDbConnection showconn=new OleDbConnection(strConnection);
showconn=new OleDbConnection(strConnection);
showconn.Open(); // 在此处放置用户代码以初始化页面
//string showstring="select * from setdeptinfo";
//ds=new DataSet();
//OleDbDataAdapter myadapter=new OleDbDataAdapter(showstring,showconn);
//myadapter.Fill(ds,"setdeptinfo");
//showinfo.DataSource=ds;
//showinfo.DataMember="setdeptinfo";
//showinfo.DataBind();
if (!Page.IsPostBack)
BindGrid();
}
ICollection createtable()
{
string showstring="select * from setdeptinfo";
//ds=new DataSet();
OleDbDataAdapter myadapter=new OleDbDataAdapter(showstring,showconn);
myadapter.Fill(ds,"setdeptinfo");
//showinfo.DataSource=ds;
//showinfo.DataMember="setdeptinfo";
//showinfo.DataBind();
return ds.Tables["setdeptinfo"].DefaultView;
}
public void BindGrid()
{
showinfo.DataSource=createtable();
showinfo.DataBind();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ds = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
//
// ds
//
this.ds.DataSetName = "NewDataSet";
this.ds.Locale = new System.Globalization.CultureInfo("zh-CN");
this.gosetdetp.Click += new System.EventHandler(this.gosetdetp_Click);
this.gomain.Click += new System.EventHandler(this.gomain_Click);
this.showinfo.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.showinfo_PageIndexChanged);
this.showinfo.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.showinfo_CancelCommand);
this.showinfo.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.showinfo_EditCommand);
this.showinfo.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.showinfo_UpdateCommand);
this.showinfo.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.showinfo_DeleteCommand);
this.showinfo.SelectedIndexChanged += new System.EventHandler(this.showinfo_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit(); }
#endregion private void showinfo_SelectedIndexChanged(object sender, System.EventArgs e)
{
} private void gosetdetp_Click(object sender, System.EventArgs e)
{
Response.Redirect("setdeptinfo.aspx");
} private void gomain_Click(object sender, System.EventArgs e)
{
Response.Redirect("century.aspx");
} private void showinfo_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
showinfo.CurrentPageIndex=e.NewPageIndex;
BindGrid(); } private void showinfo_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
showinfo.EditItemIndex=(int)e.Item .ItemIndex;
BindGrid();
} private void showinfo_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
showinfo.EditItemIndex=-1;
BindGrid(); } private void showinfo_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string idstring=e.Item.Cells[1].Text;
string nastring=(((TextBox)e.Item.Cells[2].Controls[0]).Text).ToString();
string tystring=(((TextBox)e.Item.Cells[3].Controls[0]).Text).ToString();
string phstring=(((TextBox)e.Item.Cells[4].Controls[0]).Text).ToString();
string strup="Update setdeptinfo set deptname='"+nastring+"',depttype='"+tystring+"',deptphone='"+phstring+"' where deptid='"+idstring+"'";
OleDbCommand mycmd=new OleDbCommand(strup,showconn);
mycmd.ExecuteNonQuery();
showinfo.EditItemIndex=-1;
BindGrid(); } private void showinfo_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
}
}
这是所有的代码了,就是要对一个部门的信息的数据库进行更新等操作,这个表名叫,setdeptinfo,有四个字段,分别为deptid,deptname,depttype,deptphone,现在要更新,后面的三个字段操作,我最不明白的是string idstring=e.Item.Cells[1].Text;
string nastring=(((TextBox)e.Item.Cells[2].Controls[0]).Text).ToString();这两句是什么意思,是在书上看的,然后改成我的。所以不对了,
--------------------------------------------------------------------------------指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 137: string nastring=(((TextBox)e.Item.Cells[2].Controls[0]).Text).ToString();
行 138: string tystring=(((TextBox)e.Item.Cells[3].Controls[0]).Text).ToString();
行 139: string phstring=(((TextBox)e.Item.Cells[4].Controls[0]).Text).ToString();
行 140: string strup="Update setdeptinfo set deptname='"+nastring+"',depttype='"+tystring+"',deptphone='"+phstring+"' where deptid='"+idstring+"'";
行 141: OleDbCommand mycmd=new OleDbCommand(strup,showconn);
源文件: d:\inetpub\wwwroot\webapplication2\showdeptinfo.aspx.cs 行: 139 堆栈跟踪:
[InvalidCastException: 指定的转换无效。]
WebApplication2.showdeptinfo.showinfo_UpdateCommand(Object source, DataGridCommandEventArgs e) in d:\inetpub\wwwroot\webapplication2\showdeptinfo.aspx.cs:139
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
这是出错的页面
--------------我也有这样认为的倾向