MyDataGrid_Update部分代码:
UComm.Connection.Open();
try
{
UComm.ExecuteNonQuery();
Message.InnerHtml = "<b>已更新记录</b><br>" + updateCmd;
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "错误:已存在具有相同主键的记录";
else
Message.InnerHtml = "错误:未能更新记录,请确保正确填写了字段";
Message.Style["color"] = "red";
}
UComm.Connection.Close();
BindGrid();
请高手指教!!!
16 个解决方案
#1
不明白什么是“两个合成一个”
说具体点!
说具体点!
#2
你在显示的文件中重新查询,绑定
#3
就是把两个文件1.aspx和1.aspx.cs合成一个文件1.aspx时,不出现这种问题!
bindgrid();就是重新绑定啊
public void BindGrid()
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from pfgs", UConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "pfgs");
MyDataGrid.DataSource=ds.Tables["pfgs"].DefaultView;
MyDataGrid.DataBind();
}
为什么不显示呢??
bindgrid();就是重新绑定啊
public void BindGrid()
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from pfgs", UConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "pfgs");
MyDataGrid.DataSource=ds.Tables["pfgs"].DefaultView;
MyDataGrid.DataBind();
}
为什么不显示呢??
#4
应该很正常呀,没有遇到过这种情况。等待高手的回答。
#5
贴出点击按钮的代码!
#6
还有PAGELOAD的代码
#7
贴你所有的代码
#8
跟踪一下看看嘛!
#9
private void Page_Load(object sender, System.EventArgs e)
{
UConn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=yjhk");
if (!IsPostBack)
BindGrid();
}
#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);
}
private void InitializeComponent()
{
this.MyDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.Item_Bound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs E)
{ String updateCmd ="UPDATE pfgs SET num=@num,one=@one,two=@two,three=@three,four=@four,five=@five,six=@six,seven=@seven,eight=@eight,nine=@nine,ten=@ten,eleven=@eleven,twelve=@twelve,total=@total,lasttime4=@lasttime4 where num=@num";
SqlCommand UComm = new SqlCommand(updateCmd, UConn);
UComm.Parameters.Add(new SqlParameter("@num", SqlDbType.Char, 4));
UComm.Parameters.Add(new SqlParameter("@one", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@two", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@three", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@four", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@five", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@six", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@seven", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@eight", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@nine", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@ten", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@eleven", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@twelve", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@total", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@lasttime4", SqlDbType.DateTime, 8));
UComm.Parameters["@lasttime4"].Value = DateTime.Now;
UComm.Parameters["@num"].Value = MyDataGrid.DataKeys[(char)E.Item.ItemIndex];
String[] cols ={"num", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve","total","@lasttime4"};
for (int i=1; i<14; i++)
{
String colvalue = ((TextBox)E.Item.FindControl("edit_" + cols[i])).Text;
UComm.Parameters["@" + cols[i]].Value = colvalue;
}
UComm.Connection.Open();
try
{
UComm.ExecuteNonQuery();
Message.InnerHtml = "<b>已更新记录</b><br>" + updateCmd;
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "错误:已存在具有相同主键的记录";
else
Message.InnerHtml = "错误:未能更新记录,请确保正确填写了字段";
Message.Style["color"] = "red";
}
UComm.Connection.Close();
BindGrid();
}
bindgrid()见上已贴!
真是很奇怪!!55555555555
{
UConn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=yjhk");
if (!IsPostBack)
BindGrid();
}
#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);
}
private void InitializeComponent()
{
this.MyDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.Item_Bound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs E)
{ String updateCmd ="UPDATE pfgs SET num=@num,one=@one,two=@two,three=@three,four=@four,five=@five,six=@six,seven=@seven,eight=@eight,nine=@nine,ten=@ten,eleven=@eleven,twelve=@twelve,total=@total,lasttime4=@lasttime4 where num=@num";
SqlCommand UComm = new SqlCommand(updateCmd, UConn);
UComm.Parameters.Add(new SqlParameter("@num", SqlDbType.Char, 4));
UComm.Parameters.Add(new SqlParameter("@one", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@two", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@three", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@four", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@five", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@six", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@seven", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@eight", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@nine", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@ten", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@eleven", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@twelve", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@total", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@lasttime4", SqlDbType.DateTime, 8));
UComm.Parameters["@lasttime4"].Value = DateTime.Now;
UComm.Parameters["@num"].Value = MyDataGrid.DataKeys[(char)E.Item.ItemIndex];
String[] cols ={"num", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve","total","@lasttime4"};
for (int i=1; i<14; i++)
{
String colvalue = ((TextBox)E.Item.FindControl("edit_" + cols[i])).Text;
UComm.Parameters["@" + cols[i]].Value = colvalue;
}
UComm.Connection.Open();
try
{
UComm.ExecuteNonQuery();
Message.InnerHtml = "<b>已更新记录</b><br>" + updateCmd;
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "错误:已存在具有相同主键的记录";
else
Message.InnerHtml = "错误:未能更新记录,请确保正确填写了字段";
Message.Style["color"] = "red";
}
UComm.Connection.Close();
BindGrid();
}
bindgrid()见上已贴!
真是很奇怪!!55555555555
#10
UComm.Connection.Close();
BindGrid();
改成:
BindGrid();
UComm.Connection.Close();
BindGrid();
改成:
BindGrid();
UComm.Connection.Close();
#11
to anbeel:还是老样子,真不知道是哪里的原因了,好愁!!!!
#12
哪位大哥帮个忙!!看一下,这个问题我实在觉得奇怪!!
#13
没有人理我,只好自己up一下了
#14
应该是DataGrid设置的问题:
重新建立一个页面,重新设置属性把代码考过去,应该就没事了。
重新建立一个页面,重新设置属性把代码考过去,应该就没事了。
#15
还是不行,真是奇怪极了!
#16
up
#1
不明白什么是“两个合成一个”
说具体点!
说具体点!
#2
你在显示的文件中重新查询,绑定
#3
就是把两个文件1.aspx和1.aspx.cs合成一个文件1.aspx时,不出现这种问题!
bindgrid();就是重新绑定啊
public void BindGrid()
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from pfgs", UConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "pfgs");
MyDataGrid.DataSource=ds.Tables["pfgs"].DefaultView;
MyDataGrid.DataBind();
}
为什么不显示呢??
bindgrid();就是重新绑定啊
public void BindGrid()
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from pfgs", UConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "pfgs");
MyDataGrid.DataSource=ds.Tables["pfgs"].DefaultView;
MyDataGrid.DataBind();
}
为什么不显示呢??
#4
应该很正常呀,没有遇到过这种情况。等待高手的回答。
#5
贴出点击按钮的代码!
#6
还有PAGELOAD的代码
#7
贴你所有的代码
#8
跟踪一下看看嘛!
#9
private void Page_Load(object sender, System.EventArgs e)
{
UConn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=yjhk");
if (!IsPostBack)
BindGrid();
}
#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);
}
private void InitializeComponent()
{
this.MyDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.Item_Bound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs E)
{ String updateCmd ="UPDATE pfgs SET num=@num,one=@one,two=@two,three=@three,four=@four,five=@five,six=@six,seven=@seven,eight=@eight,nine=@nine,ten=@ten,eleven=@eleven,twelve=@twelve,total=@total,lasttime4=@lasttime4 where num=@num";
SqlCommand UComm = new SqlCommand(updateCmd, UConn);
UComm.Parameters.Add(new SqlParameter("@num", SqlDbType.Char, 4));
UComm.Parameters.Add(new SqlParameter("@one", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@two", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@three", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@four", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@five", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@six", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@seven", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@eight", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@nine", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@ten", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@eleven", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@twelve", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@total", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@lasttime4", SqlDbType.DateTime, 8));
UComm.Parameters["@lasttime4"].Value = DateTime.Now;
UComm.Parameters["@num"].Value = MyDataGrid.DataKeys[(char)E.Item.ItemIndex];
String[] cols ={"num", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve","total","@lasttime4"};
for (int i=1; i<14; i++)
{
String colvalue = ((TextBox)E.Item.FindControl("edit_" + cols[i])).Text;
UComm.Parameters["@" + cols[i]].Value = colvalue;
}
UComm.Connection.Open();
try
{
UComm.ExecuteNonQuery();
Message.InnerHtml = "<b>已更新记录</b><br>" + updateCmd;
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "错误:已存在具有相同主键的记录";
else
Message.InnerHtml = "错误:未能更新记录,请确保正确填写了字段";
Message.Style["color"] = "red";
}
UComm.Connection.Close();
BindGrid();
}
bindgrid()见上已贴!
真是很奇怪!!55555555555
{
UConn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=yjhk");
if (!IsPostBack)
BindGrid();
}
#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);
}
private void InitializeComponent()
{
this.MyDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.Item_Bound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs E)
{ String updateCmd ="UPDATE pfgs SET num=@num,one=@one,two=@two,three=@three,four=@four,five=@five,six=@six,seven=@seven,eight=@eight,nine=@nine,ten=@ten,eleven=@eleven,twelve=@twelve,total=@total,lasttime4=@lasttime4 where num=@num";
SqlCommand UComm = new SqlCommand(updateCmd, UConn);
UComm.Parameters.Add(new SqlParameter("@num", SqlDbType.Char, 4));
UComm.Parameters.Add(new SqlParameter("@one", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@two", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@three", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@four", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@five", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@six", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@seven", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@eight", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@nine", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@ten", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@eleven", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@twelve", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@total", SqlDbType.Money, 8));
UComm.Parameters.Add(new SqlParameter("@lasttime4", SqlDbType.DateTime, 8));
UComm.Parameters["@lasttime4"].Value = DateTime.Now;
UComm.Parameters["@num"].Value = MyDataGrid.DataKeys[(char)E.Item.ItemIndex];
String[] cols ={"num", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve","total","@lasttime4"};
for (int i=1; i<14; i++)
{
String colvalue = ((TextBox)E.Item.FindControl("edit_" + cols[i])).Text;
UComm.Parameters["@" + cols[i]].Value = colvalue;
}
UComm.Connection.Open();
try
{
UComm.ExecuteNonQuery();
Message.InnerHtml = "<b>已更新记录</b><br>" + updateCmd;
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "错误:已存在具有相同主键的记录";
else
Message.InnerHtml = "错误:未能更新记录,请确保正确填写了字段";
Message.Style["color"] = "red";
}
UComm.Connection.Close();
BindGrid();
}
bindgrid()见上已贴!
真是很奇怪!!55555555555
#10
UComm.Connection.Close();
BindGrid();
改成:
BindGrid();
UComm.Connection.Close();
BindGrid();
改成:
BindGrid();
UComm.Connection.Close();
#11
to anbeel:还是老样子,真不知道是哪里的原因了,好愁!!!!
#12
哪位大哥帮个忙!!看一下,这个问题我实在觉得奇怪!!
#13
没有人理我,只好自己up一下了
#14
应该是DataGrid设置的问题:
重新建立一个页面,重新设置属性把代码考过去,应该就没事了。
重新建立一个页面,重新设置属性把代码考过去,应该就没事了。
#15
还是不行,真是奇怪极了!
#16
up