Deleting row in dataset

J

janetb

Gave up on earlier post of using imageButtons and command arguements, but
can't get the following to work either. When trying to delete a row, and
using the SELECT rows, I get an error of the selected index out of range even
though I'm asking it to select the record in the dataset based on a primary
key field (the rows commented out). When using the FIND rows, it stops on
the row.delete() line with an error of "Object reference not set to an
instance of an object." Really frustrated and would appreciate some help.

<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Data" %>
<%@Page Language="VB" debug="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<script language="vb" runat="server">
Public strConn As String = "user id=myUser;data source=serverName;persist
security info=True;initial catalog=myDatabase;password=pwd;"
Public cn As New SqlClient.SqlConnection(strConn)
Public ds as New Dataset
Public da as New SQLDataAdapter
Public varID as integer, varUPD as integer, varPG as integer

Sub Page_load(sender as Object, e as EventArgs)
if request("upd")="" or request("upd")="0" then varUPD=0 else
varUPD=int(request("upd"))
if request("eventID")="" or request("eventID")="0" then varID=0 else
varID=int(request("eventID"))
if request("pageID")="" or request("pageID")="0" then varPG=0 else
varPG=int(request("pageID"))
eventID.text=varID

da = New SqlDataAdapter("sp_eCoursePage", cn)
da.SelectCommand.CommandType = CommandType.StoredProcedure
Dim p_ID as New SqlParameter("@ID", varID)
da.SelectCommand.Parameters.Add(p_ID)
da.Fill(ds, "pages")
Dim autogen As New SqlCommandBuilder(da)
Dim dt As DataTable = ds.Tables("pages")

select case varUPD
case 1 ' Delete a record.
dt.PrimaryKey = New DataColumn() {dt.Columns("pageID_pk")} 'used find
Dim row As DataRow = dt.Rows.Find(varPG)
'used find
row.Delete()
'used find
'dim mySQL as string = "pageID_pk=" & varPG 'used select
'Dim row As DataRow = dt.Select(mySQL)(0) 'used select
'row.Delete() 'usedselect
da.Update(ds, "pages")' Update the database.
lblMsg.text=mySQL
case 2
dim myURL="adminEcourseUpdate.aspx?pageID=" & varPG
response.redirect(myURL)
end select

If ds.Tables("pages").Rows.Count = 0 Then
dgCourseList.visible=False
else
dgCourseList.DataSource = ds.tables("pages")
dgCourseList.dataBind()
dgCourseList.visible=True
end if
if Not Page.IsPostback then
pageAdd.visible=False
else
pageAdd.visible=True
end if
cn.close
'OnItemCommand="dgCourseListItemCommand" - for imageButtons that couldn't
get to work
end sub
</script>

<asp:DataGrid id="dgCourseList" autogenerateColumns="False" runat="server" >
<Columns>
<asp:BoundColumn DataField="pageID_pk" Visible="False"></asp:BoundColumn>
<asp:hyperlinkcolumn dataNavigateurlField="pageID_pk"
dataNavigateUrlFormatString="adminECourse.aspx?upd=1&pageID={0}"
text="Del"></asp:hyperlinkcolumn>
<asp:hyperlinkcolumn dataNavigateurlField="pageID_pk"
dataNavigateUrlFormatString="adminECourse.aspx?upd=2&pageID={0}"
text="Upd"></asp:hyperlinkcolumn>
</Columns>
blah, blah.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top