datagrid中TextBox控件使用和数据更新问题

wyxlxp 2003-08-22 07:26:16
为何数据不更新?帮我看看有何错误?

private void calculate_Click(object sender, System.EventArgs e)
{
for(int i=0;i<order.Items.Count;i++)
{
TextBox tb=(TextBox)order.Items[i].FindControl("Quantity");

int qt=Convert.ToInt32(tb.Text);
string ud=(string)Session["userid"];
string od=(string)Session["OrderID"];

SqlConnection myConnection=new SqlConnection("user id=sa;pwd=761110;initial catalog=xjuser");
string sql="update Order1 set Quantity='"+qt+"' where OrderID='"+od+"'and userid='"+ud+"'";
SqlDataAdapter myCommand=new SqlDataAdapter(sql,myConnection);
DataSet ds=new DataSet();
myCommand.Fill(ds,"aa");
DataTable dt=ds.Tables["aa"];
string s="select * from Order1 where OrderID='"+od+"'and userid='"+ud+"'";
SqlDataAdapter com=new SqlDataAdapter(s,myConnection);
DataSet ds2=new DataSet();
com.Fill(ds2,"as");
DataTable dt2=ds2.Tables["as"];
order.DataSource=dt2;
order.DataBind();
}




}
...全文
33 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyxlxp 2003-08-22
  • 打赏
  • 举报
回复
to sgsh51():
解决了!就是要把Page_Load中的数据绑定代码放在if(!Page.ISPostBack)
{
}
中,太感谢了,谢谢你一直给我回复。
wyxlxp 2003-08-22
  • 打赏
  • 举报
回复
可是if(!Page.ISPostBack)
{
}
一般是放在Page_Load()中的,而我要求在button事件中。或许我没有理解你的意思,真不好意思。笨得简直....
sgsh51 2003-08-22
  • 打赏
  • 举报
回复
把DataGrid绑定DataSet的那一段代码,写在
if(!Page.ISPostBack)
{
}
中之后不应该取不到TextBox的值呀(残念。。。。)
wyxlxp 2003-08-22
  • 打赏
  • 举报
回复
很不好意思,我没有qq。要不你发到我的信箱吧。wang_yunxia@sina.com
sgsh51 2003-08-22
  • 打赏
  • 举报
回复
39296853上qq来,帮你解决
wyxlxp 2003-08-22
  • 打赏
  • 举报
回复
是的,就是取不到。
sgsh51 2003-08-22
  • 打赏
  • 举报
回复
还是qt无法得到TextBox框中输入的值是吗?
wyxlxp 2003-08-22
  • 打赏
  • 举报
回复
还是不行,我快崩溃了。要不用其他方法试试?
sgsh51 2003-08-22
  • 打赏
  • 举报
回复
把你用DataSet绑定到DataGrid的那段代码写在
if(!Page.ISPostBack)
{
}

试试看
wyxlxp 2003-08-22
  • 打赏
  • 举报
回复
发现错误,TextBox tb=(TextBox)order.Items[i].FindControl("Quantity");

int qt=Convert.ToInt32(tb.Text);所取到的值始终是数据库中原有的值,button后无法改变。该如何修改呢?
sgsh51 2003-08-22
  • 打赏
  • 举报
回复
SqlCommand cmd = new SqlCommand(sql,myConnection);
在这句话之前设个断点,把得到sql语句放到数据库中去操作一下
wyxlxp 2003-08-22
  • 打赏
  • 举报
回复
谢谢二位,可是数据还是无法更新。
TextBox tb=(TextBox)order.Items[i].FindControl("Quantity");

int qt=Convert.ToInt32(tb.Text);
这个值能取到,可放在更新数据库语句中好像取不到。再帮我看看吧!
sgsh51 2003-08-22
  • 打赏
  • 举报
回复
把最后的大括号,提到那句中文的前面,晕?
sgsh51 2003-08-22
  • 打赏
  • 举报
回复
private void calculate_Click(object sender, System.EventArgs e)
{
for(int i=0;i<order.Items.Count;i++)
{
TextBox tb=(TextBox)order.Items[i].FindControl("Quantity");

int qt=Convert.ToInt32(tb.Text);
string ud=(string)Session["userid"];
string od=(string)Session["OrderID"];

SqlConnection myConnection=new SqlConnection("user id=sa;pwd=761110;initial catalog=xjuser");
string sql="update Order1 set Quantity='"+qt+"' where OrderID='"+od+"'and userid='"+ud+"'";
SqlCommand cmd = new SqlCommand(sql,myConnection);
cmd.ExecuteNonQuery();

string s="select * from Order1 where OrderID='"+od+"'and userid='"+ud+"'";
SqlDataAdapter com=new SqlDataAdapter(s,myConnection);
DataSet ds2=new DataSet();
com.Fill(ds2,"as");
order.DataSource=ds2;
order.DataBind();
}
你数据更新思绪有点混乱,帮你改了一下看看能用吗



}
guoyan19811021 2003-08-22
  • 打赏
  • 举报
回复
TextBox tb=(TextBox)order.Items[i].FindControl("Quantity");

int qt=Convert.ToInt32(tb.Text);
能取到这个值吗?
wyxlxp 2003-08-22
  • 打赏
  • 举报
回复
问题还是老样子!
guoyan19811021 2003-08-22
  • 打赏
  • 举报
回复
private void calculate_Click(object sender, System.EventArgs e)
{
for(int i=0;i<order.Items.Count;i++)
{
TextBox tb=(TextBox)order.Items[i].FindControl("Quantity");

int qt=Convert.ToInt32(tb.Text);
string ud=(string)Session["userid"];
string od=(string)Session["OrderID"];

SqlConnection myConnection=new SqlConnection("user id=sa;pwd=761110;initial catalog=xjuser");
string sql="update Order1 set Quantity='"+qt+"' where OrderID='"+od+"'and userid='"+ud+"'";
SqlDataCommand myCommand1=new SqlDataCommand (sql,myConnection);
SqlDataAdapter myCommand=new SqlDataAdapter()
myCommand.Selectcommand=myCommand1 DataSet ds=new DataSet();
myCommand.Fill(ds,"aa");
DataTable dt=ds.Tables["aa"];
string s="select * from Order1 where OrderID='"+od+"'and userid='"+ud+"'";
SqlDataAdapter com=new SqlDataAdapter(s,myConnection);
DataSet ds2=new DataSet();
com.Fill(ds2,"as");
DataTable dt2=ds2.Tables["as"];
order.DataSource=dt2;
order.DataBind();
}

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧