CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

菜鸟发问,高手请进(关于datagrid和数据库)

楼主leostone()2006-05-04 15:53:58 在 .NET技术 / C# 提问

小弟要做毕业论文.从网上拷贝回来这一段代码.  
  可是delete和update功能都不能实现.说是脚本发生错误,各位达人能帮我看看吗?  
   
   
  ......  
  private   void   dgShow_DeleteCommand(object   source,   System.Web.UI.WebControls.DataGridCommandEventArgs   e)//不能正常使用  
  {  
      if(dgShow.Items.Count==1)//如果该页只有一行,往前删除后往前翻一页.  
  {  
  if(dgShow.CurrentPageIndex!=0)  
      dgShow.CurrentPageIndex   =   dgShow.CurrentPageIndex-1;  
  }  
          string   strSql   =   "delete   from   BaseInfo   where   name="+e.Item.Cells[0].Text+"";  
          ExecuteSql(strSql);  
          BindData();  
   
  }  
   
  private   void   dgShow_UpdateCommand(object   source,   System.Web.UI.WebControls.DataGridCommandEventArgs   e)//不能正常使用  
  {  
    string   strname   =   e.Item.Cells[0].Text;//处于非编辑状态  
    string   strteam   =   ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态  
    string   strposition   =((TextBox)(e.Item.Cells[2].Controls[0])).Text;  
    string   strborn   =   ((TextBox)(e.Item.Cells[3].Controls[0])).Text;  
    string   strheight   =((TextBox)(e.Item.Cells[4].Controls[0])).Text;  
    string   strweight   =((TextBox)(e.Item.Cells[5].Controls[0])).Text;  
    string   strfrom   =((TextBox)(e.Item.Cells[6].Controls[0])).Text;  
    string   strSql   =   "update   BaseInfo   set   team='"+strteam+"'";  
  strSql+=",positon="+strposition+",born='"+strborn+"',height='"+strheight+"'   ,weight='"+strweight+"',[from]='"+strfrom+"'where   name="+strname+"";  
    ExecuteSql(strSql);  
    dgShow.EditItemIndex   =   -1;  
    BindData();  
   
  }  
  ...... 问题点数:20、回复次数:4Top

1 楼leostone()回复于 2006-05-04 15:59:55 得分 0

private   void   ExecuteSql(string   strSql)  
  {  
  try  
                    {  
    string   strconn   =   System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取  
    SqlConnection   conn   =new   SqlConnection(strconn);  
    SqlCommand   com   =   new   SqlCommand(strSql,conn);  
    conn.Open();  
    com.ExecuteNonQuery();  
    conn.Close();  
    }    
  catch(Exception   e)  
    {  
    Response.Write("<script   language   =   'javascript'>alert('"+e.Message+"');</script>")   ;  
   
    }  
  }Top

2 楼califord(远方)回复于 2006-05-04 16:00:47 得分 20

string   strSql   =   "delete   from   BaseInfo   where   name="+e.Item.Cells[0].Text+"";????  
  改成  
  string   strSql   =   "delete   from   BaseInfo   where   name='"+e.Item.Cells[0].Text+"'";  
  试试  
   
  下面一样的道理Top

3 楼leostone()回复于 2006-05-04 16:16:28 得分 0

谢谢califord(远方)   大哥....delete功能可以了.  
  我改成了  
  string   strSql   =   "update   BaseInfo   set   team='"+strteam+"'";  
  strSql   +=",positon='"+strposition+"',born='"+strborn+"',  
  height='"+strheight+"'   ,weight='"+strweight+"',[from]='"+strfrom+"'where   name='"+strname+"'";  
   
   
  update还是不行哦...Top

4 楼leostone()回复于 2006-05-04 17:27:55 得分 0

搞掂了......  
  string     strname     =     e.Item.Cells[0].Text;//处于非编辑状态      
  和strSql     +=     ",positon='     "+strposition+     "',born='     "+strborn+     "',      
  height='     "+strheight+     "'     ,weight='     "+strweight+     "',[from]='     "+strfrom+     "'where     name='     "+strname+     "'     ";      
  冲突了  
   
  哈哈...Top

相关问题

关键词

得分解答快速导航

  • 帖主:leostone
  • califord

相关链接

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

广告也精彩

反馈

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