CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

asp.net(c#)中关于更新的问题

楼主midren(你好好)2003-06-02 22:52:33 在 .NET技术 / ASP.NET 提问

初学asp.net  
  部分程序如下:  
  void   DEDR_Update(Object   sender,DataGridCommandEventArgs   e)  
  {  
  DataSet   objDataSet=(DataSet)Session["studentTable"];  
  int   row=Convert.ToInt32(e.Item.ItemIndex);  
  DataRow   objRow=objDataSet.Tables["12"].Rows[row];  
  for(int   i=0;i<e.Item.Cells.Count;i++)  
  {  
  TextBox   EditText=null;  
  EditText=(TextBox)e.Item.Cells[i].Controls[0];  
  objDataSet.Tables["12"].Rows[row][i]=EditText.Text;  
  }  
  OleDbDataAdapter   objAdapter=(OleDbDataAdapter)Session["stuAdapter"];  
  OleDbCommandBuilder   objCommandBuilder=new   OleDbCommandBuilder(objAdapter);  
  objAdapter.UpdateCommand=objCommandBuilder.GetUpdateCommand();  
  objAdapter.InsertCommand=objCommandBuilder.GetInsertCommand();  
  objAdapter.DeleteCommand=objCommandBuilder.GetDeleteCommand();  
  objAdapter.Update(objDataSet,"12");  
  dgStuList.EditItemIndex=-1;  
  dgStuList.DataSource=LoadDataView();  
  dgStuList.DataBind();  
  }  
  我是想将datagrid中一行编辑的数据更新到数据库中  
  在for循环中出现问题:指定的转换无效。  
  再问一下:用这种更新方法行不行。  
  问题点数:20、回复次数:6Top

1 楼caoit(lost)回复于 2003-06-02 23:07:02 得分 15

这样可以更新的,请问你的dgStuList里有多少可编辑的列?Top

2 楼caoit(lost)回复于 2003-06-02 23:10:58 得分 0

给你一段代码,让你参考吧  
   
  void   Update(Object   sender,DataGridCommandEventArgs   e)  
  {  
    string   UpdateCommand;  
    UpdateCommand="UPDATE   Asia   set   Data=@Data,Tim=@Tim,Img=@Img,Pl1=@Pl1,Pk=@Pk,Pl2=@Pl2,O1=@O1,O2=@O2,O3=@O3,Color1=@color1,Color2=@color2,Color3=@color3,Color4=@color4   where   ID=@ID";  
    SqlConnection   Conn=new   SqlConnection("server=   db701.now.net.cn;database=db_asiaodds_com;pwd=mja4md;uid=d26702");  
    SqlCommand   MyCommand=new   SqlCommand(UpdateCommand,Conn);  
    MyCommand.Parameters.Add(new   SqlParameter("@ID",SqlDbType.Int,4));  
    MyCommand.Parameters.Add(new   SqlParameter("@Data",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@Tim",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@Img",SqlDbType.VarChar,50));    
    MyCommand.Parameters.Add(new   SqlParameter("@Pl1",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@Pk",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@Pl2",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@O1",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@O2",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@O3",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@color1",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@color2",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@color3",SqlDbType.VarChar,50));  
    MyCommand.Parameters.Add(new   SqlParameter("@color4",SqlDbType.VarChar,50));  
       
    MyCommand.Parameters["@ID"].Value=DataGrid1.DataKeys[e.Item.ItemIndex];  
    string[]   Cols={"@Img","@Data","@Tim","@Pl1","@Pk","@Pl2","@O1","@O2","@O3"};  
    int   NumCols=e.Item.Cells.Count;  
    int   i;  
    for(i=9;i<NumCols-1;i++)  
    {  
      TextBox   CurrentTextBox;  
      CurrentTextBox=(TextBox)e.Item.Cells[i].Controls[0];  
      string   ColValue=CurrentTextBox.Text;    
      MyCommand.Parameters[Cols[i-9]].Value=ColValue;  
      }  
      MyCommand.Parameters["@color1"].Value=((RadioButtonList)e.Item.FindControl("c1_edit")).SelectedItem.Value;  
      MyCommand.Parameters["@color2"].Value=((RadioButtonList)e.Item.FindControl("c2_edit")).SelectedItem.Value;  
      MyCommand.Parameters["@color3"].Value=((RadioButtonList)e.Item.FindControl("c3_edit")).SelectedItem.Value;  
      MyCommand.Parameters["@color4"].Value=((RadioButtonList)e.Item.FindControl("c4_edit")).SelectedItem.Value;  
      MyCommand.Connection.Open();  
      MyCommand.ExecuteNonQuery();  
      MyCommand.Connection.Close();  
      DataGrid1.EditItemIndex=-1;  
      Grid_Bind1();  
  }Top

3 楼midren(你好好)回复于 2003-06-02 23:16:54 得分 0

我的代码是和acess连接,而不是sqlsever.  
  Top

4 楼gj2008(堕落天使)回复于 2003-06-02 23:45:51 得分 5

将其中的sqlcommand改为olecommandTop

5 楼midren(你好好)回复于 2003-06-03 12:43:48 得分 0

出错误地方是  
  for(int   i=0;i<e.Item.Cells.Count;i++)  
  {  
  TextBox   EditText=null;  
  EditText=(TextBox)e.Item.Cells[i].Controls[0];  
  objDataSet.Tables["12"].Rows[row][i]=EditText.Text;  
  }  
  指定转换无效.  
  不明白Top

6 楼youxia001(游侠001)回复于 2003-06-03 12:51:25 得分 0

upTop

相关问题

  • 靠ASP。NET C# XML SQLSERVER有饭吃吗?
  • 高分求 asp。net+c# 代码
  • ASP NET(C#)一些概念问题
  • c#.net 中的session如何传给asp
  • Excel打印与ASP。NET的C#
  • C++builder6要更新包么??
  • C# 更新listView问题???
  • 菜鸟问题,ASP的写法转ASP。NET(C#)
  • 关于学习c#该不该学asp。net或asp那?
  • 很简单的ASP。NET提交更新的问题,我郁闷,我急

关键词

  • asp.net
  • asp
  • objdataset
  • row
  • dataset

得分解答快速导航

  • 帖主:midren
  • caoit
  • gj2008

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo