我的思路是,查询自己写,然后显示在gridview里,然后利用gridview里的事件对查询后的结果进行编辑和删除,现在贴上代码,大家帮看看有什么问题.
查询:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myConnectionString As String
myConnectionString = "server=localhost;database=****;uid=****;pwd=123;"
Dim myConnection As New SqlConnection(myConnectionString)
Dim mySelect As String = "SELECT * FROM Repository WHERE Name LIKE '%" + TextBox3.Text + "%' AND Operator LIKE '%" + DropDownList1.Text + "%' AND Type LIKE '%" + DropDownList2.Text + "%' AND Server LIKE '%" + DropDownList3.Text + "%' AND Detail LIKE '%" + TextBox4.Text + "%' AND Keyword LIKE '%" + TextBox6.Text + "%'"
Dim myCommand As New SqlCommand(mySelect)
Dim adapter As SqlDataAdapter = New SqlDataAdapter(mySelect, myConnection)
myCommand.Connection = myConnection
myConnection.Open()
Dim ds As New DataSet
adapter.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
myCommand.Connection.Close()
End Sub删除:
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim myConnectionString As String
myConnectionString = "server=localhost;database=****;uid=****;pwd=123;"
Dim myConnection As New SqlConnection(myConnectionString)
Dim myDelete As String
myDelete = "DELETE FROM Repository WHERE RepositoryID='" & Me.GridView1.DataKeyNames(e.RowIndex()) & "'"
Dim myCommand As New SqlCommand(myDelete)
myCommand.Connection = myConnection
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
End Sub运行结果:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------索引超出了数组界限。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 索引超出了数组界限。源错误:
行 35:
行 36:
行 37: myDelete = "DELETE FROM Repository WHERE RepositoryID='" & Me.GridView1.DataKeyNames(e.RowIndex()) & "'"
行 38: ''myDelete = "DELETE FROM Repository WHERE RepositoryID='210011'"
行 39: Response.Write(myDelete)
源文件: D:\knowledge\advancedsearch.aspx.vb 行: 37
我现在的疑问是:
1.我在GRIDVIEW里面添加删除按钮,设计界面双击"删除"产生事件,产生的都是 Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging End Sub这个.
2.我这样的代码产生这样的错误是什么原因,我的目的就是查询后的结果也可以编辑删除,大家帮我看看我删除的代码错在哪里,编辑也给点思路.编辑我的理解是用他自己的数据源控件的时候,GRIDVIEW这样的编辑就可以,或者自己写个详细编辑的页面.
大家帮我看看啊!
查询:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myConnectionString As String
myConnectionString = "server=localhost;database=****;uid=****;pwd=123;"
Dim myConnection As New SqlConnection(myConnectionString)
Dim mySelect As String = "SELECT * FROM Repository WHERE Name LIKE '%" + TextBox3.Text + "%' AND Operator LIKE '%" + DropDownList1.Text + "%' AND Type LIKE '%" + DropDownList2.Text + "%' AND Server LIKE '%" + DropDownList3.Text + "%' AND Detail LIKE '%" + TextBox4.Text + "%' AND Keyword LIKE '%" + TextBox6.Text + "%'"
Dim myCommand As New SqlCommand(mySelect)
Dim adapter As SqlDataAdapter = New SqlDataAdapter(mySelect, myConnection)
myCommand.Connection = myConnection
myConnection.Open()
Dim ds As New DataSet
adapter.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
myCommand.Connection.Close()
End Sub删除:
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim myConnectionString As String
myConnectionString = "server=localhost;database=****;uid=****;pwd=123;"
Dim myConnection As New SqlConnection(myConnectionString)
Dim myDelete As String
myDelete = "DELETE FROM Repository WHERE RepositoryID='" & Me.GridView1.DataKeyNames(e.RowIndex()) & "'"
Dim myCommand As New SqlCommand(myDelete)
myCommand.Connection = myConnection
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
End Sub运行结果:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------索引超出了数组界限。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 索引超出了数组界限。源错误:
行 35:
行 36:
行 37: myDelete = "DELETE FROM Repository WHERE RepositoryID='" & Me.GridView1.DataKeyNames(e.RowIndex()) & "'"
行 38: ''myDelete = "DELETE FROM Repository WHERE RepositoryID='210011'"
行 39: Response.Write(myDelete)
源文件: D:\knowledge\advancedsearch.aspx.vb 行: 37
我现在的疑问是:
1.我在GRIDVIEW里面添加删除按钮,设计界面双击"删除"产生事件,产生的都是 Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging End Sub这个.
2.我这样的代码产生这样的错误是什么原因,我的目的就是查询后的结果也可以编辑删除,大家帮我看看我删除的代码错在哪里,编辑也给点思路.编辑我的理解是用他自己的数据源控件的时候,GRIDVIEW这样的编辑就可以,或者自己写个详细编辑的页面.
大家帮我看看啊!
不过C#的是这样的:
Convert.ToInt32(GridView1.DataKeyNames[e.RowIndex][0])
其中[0]是指第几个元素,也就是ID在第几列,参考一下
我现在这样写了,好象还是没效果么.大家来看看.