先简单介绍一下该网页要实现的功能:  
 
此页我是作为一名管理员登陆:  
这个页面上有一个textbox,输入后作为搜索的关键字,把所有和这个关键字相同的结果全部显示在gataGrid中。还有一个控件就是DropDownList1,通过选择来更新我的数据库,而制约条件就是id号了(针对某一行操作),id号也就是业务的流水号,不能从任何地方获取,作为管理员我并不知道流水号是多少(但它存在于数据库的表里)。我希望从gataGrid里直接获取,这样可以实现么?小妹我非常着急,只要这页完成了就大功告成了~~  谢谢各位老大们!
 
代码如下:  
Dim  startIndex  as  Integer  
Dim  myconnect  as  new  sqlconnection("server=localhost;uid=sa;pwd=;database=service")  
Dim  cname  as  string  
 
Function  objDataView()  As  DataView  
     Dim  sql  as  string  
     cname=TextBox1.text  
     myconnect.open()  
     sql="select  *  from  Course  where  coursename='"+cname+"'"  
     Dim  objDataSet  As  New  DataSet()  
     Dim  objDataAdapter  As  New  SqlDataAdapter(sql,  myconnect)  
     objDataAdapter.Fill(objDataSet,  "Course")  
     objDataView  =  New  DataView(objDataSet.Tables("Course"))  
     myconnect.close()  
End  Function  
 
Sub  Page_Load(sender  As  Object,  e  As  EventArgs)  
     DataGrid1.DataSource  =  objDataView()  
     DataGrid1.DataBind()  
End  Sub  
 
 
Sub  ChangePage(sender  As  Object,  e  As  DataGridPageChangedEventArgs)    //DataGrid的分页  
     startIndex  =  e.NewPageIndex*DataGrid1.PageSize  
     DataGrid1.CurrentPageIndex  =  e.NewPageIndex  
     DataGrid1.DataSource  =  objDataView()  
     DataGrid1.DataBind()  
End  Sub  
 
sub  DataGrid1_EditCommand(sender  As  Object,  e  As  DataGridCommandEventArgs)  //DataGrid的编辑功能,也就是问题出现的地方  
     Dim  sql1  as  string  
     Dim  intid  As  Integer  
     Dim  strvalid  As  string  
     intid  =  DataGrid1.DataKeys(  e.Item.ItemIndex  )  
     strvalid  =  DropDownList1.SelectedItem.value  
     Dim  mycommand  as  sqlcommand  
     mycommand  =  new  sqlcommand(sql1,myconnect)  
 
     sql1="update  Course  set  valid=@valid  where  id=@id"//  起先sql1="update  Course  set  valid='"+DropDownList1.SelectedItem.value+"'"  没有加where条件句,更新数据一点都没问题。但由于希望它针对与某一行操作,就编写为现在这样,出错信息是@valid  @id需要声明    
     mycommand.Parameters.Add("@valid",strvalid)  
     mycommand.Parameters.Add("@id",intid)  
 
     ExecuteSQL(sql1)  
     Span.InnerHtml="修改纪录成功!"  
     DataGrid1.DataSource  =  objDataView()  
     DataGrid1.DataBind()  
end  sub  
 
sub  ExecuteSQL(sql1  as  string)  
     Dim  mycommand  as  sqlcommand  
     mycommand  =  new  sqlcommand(sql1,myconnect)  
     myconnect.Open()  
     mycommand.ExecuteNonQuery()  
     myconnect.Close()  
end  sub  
 
 
不知说明白否,急盼!!谢谢!!!!!!!!!!!!

解决方案 »

  1.   

    sql1="update  Course  set  valid=@valid  where  id=@id"改成sql1="update  Course  set  valid='" + strvalid +"'  where  id=" + intid  试试
      

  2.   

    不好意思,写错了,是DataGrid,not gataGrid  :P
      

  3.   

    sql1="update  Course  set  valid=@valid  where  id=@id"//  起先sql1="update  Course  set  valid='"+DropDownList1.SelectedItem.value+"'"  没有加where条件句,更新数据一点都没问题。但由于希望它针对与某一行操作,就编写为现在这样,出错信息是@valid  @id需要声明    
         mycommand.Parameters.Add("@valid",strvalid)  
         mycommand.Parameters.Add("@id",intid) =========================改为: (id 字段应该为整型吧 ,要是字符型要加 '')
    sql1="update  Course  set  valid= '" + strvalid + "' where id = " + intid.ToString() '''mycommand.Parameters.Add("@valid",strvalid)   //注释掉
    '''mycommand.Parameters.Add("@id",intid)    /////注释掉
    ---------------
    一般习惯用 stored procedure 来处理数据库操作是比较合适的。
    不建议过多的在业务代码中直接使用sql代码!----------
    good luck ~
      

  4.   

    哈哈,太强了!!
    更新数据的问题倒是解决了,但又出现一个新的:现在我的提交按钮没有起到作用,只要一点击DateGrid中的“编辑”,DropDownList1默认的是什么内容,自动就提交到DB了,这样势必就有些混乱,不如等我自己做完选择后,再按提交按钮更新DB来的清楚!有什么办法呢?:) 
    thanks again!!!!
      

  5.   

    因为你把更新数据库的代码写在了sub  DataGrid1_EditCommand(sender  As  Object,  e  As  DataGridCommandEventArgs) 事件里面,当然就每次有编辑动作,这个函数下的代码就执行一次------------------
    你把修改数据的代码不能放在那里,放在另一个你想要他在那里执行的地方
    如 新加一个 [更新数据] 的button  :Btn_EditData 
    在这个Btton 的点击事件里:
    sub  Btn_EditData_Click(sender  As  Object,  e  As  DataGridCommandEventArgs)
    {
     // 你的 数据库修改代码!! 
    }
      

  6.   

    你说的button可以是我的提交按钮么?希望可以利用一下:)