先简单介绍一下该网页要实现的功能:
此页我是作为一名管理员登陆:
这个页面上有一个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
不知说明白否,急盼!!谢谢!!!!!!!!!!!!
此页我是作为一名管理员登陆:
这个页面上有一个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
不知说明白否,急盼!!谢谢!!!!!!!!!!!!
解决方案 »
- 怎样更改VS2010的CSS级联样式表验证
- asp.net打印问题
- 关于roles的问题
- 急在线等!JS判断问题!请大伙来帮这看看!顶者有分!
- asp.net环境安装的问题
- 源代码管理器(VSS)怎么用呀?
- 一个关于发布的asp.net系统修改的问题,该系统有一个bin/z_10000.dll,n个.cs文件,已经编译过了,我要修改list.aspx他的后台文件list.asp
- 我的天拉,从来没有做过权限管理,居然找我做,根本没法动手,谁可以指导一下,不然丢饭碗了 5555 ~~
- 如何用dataset来读写xml
- ajax问题
- 有关DataGrid itemdatabound事件问题??
- DataList模版列里有个button,把它的click事件注册到一个方法,我要问的是在该方法里面如何引用DataList中的数据,详细的请来看看,高分
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 ~
更新数据的问题倒是解决了,但又出现一个新的:现在我的提交按钮没有起到作用,只要一点击DateGrid中的“编辑”,DropDownList1默认的是什么内容,自动就提交到DB了,这样势必就有些混乱,不如等我自己做完选择后,再按提交按钮更新DB来的清楚!有什么办法呢?:)
thanks again!!!!
你把修改数据的代码不能放在那里,放在另一个你想要他在那里执行的地方
如 新加一个 [更新数据] 的button :Btn_EditData
在这个Btton 的点击事件里:
sub Btn_EditData_Click(sender As Object, e As DataGridCommandEventArgs)
{
// 你的 数据库修改代码!!
}