Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cannot figure out what I am doing wrong. I am trying to insert a new question into the access database and the member asking the question gets 1 point added to their points in the Points table while the question is inserted into the questionList table.

There is no row at position 0.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IndexOutOfRangeException: There is no row at position 0.

Source Error: 


Line 25:                 Dim pointCnt As New DataTable
Line 26:                 Dim QID As String = ran.Next(0, TextBox1.Text.Length)
Line 27:                 Dim pnt As Integer = pointCnt.Rows(0)(0) + 1
Line 28:                 Dim Points As Integer
Line 29: 

Source File: E:\hosting\blueeyeweb\postNewQuestion.aspx.vb    Line: 27 

Stack Trace: 


[IndexOutOfRangeException: There is no row at position 0.]
   System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex) +1547970
   System.Data.RBTree`1.get_Item(Int32 index) +19
   System.Data.DataRowCollection.get_Item(Int32 index) +11
   postNewQuestion.Button1_Click(Object sender, EventArgs e) in E:\hosting\blueeyeweb\postNewQuestion.aspx.vb:27
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


What I have tried:

Imports System.Data.OleDb
Imports System.Data

Partial Class postNewQuestion
    Inherits System.Web.UI.Page

    Dim dap, pointDs As OleDbDataAdapter
    Dim con As OleDbConnection
    Dim com As New OleDbCommand
    Dim invalid() As String = {"<h1>", "<script>", "<h2>", "<input", "<h3>", "<a", "<h4>"}
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Ask Question" Then
            If TextBox1.Text <> "" Then
                For Each s As String In invalid
                    If TextBox1.Text.ToLower.Contains(s) = True Then
                        errMessage.Text = Server.HtmlEncode("Cannot Contain <h1>,<Script>,<input>,<a> etc tags")
                        Exit Sub
                    End If
                Next

                con = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0 ;data source=" & Server.MapPath("app_data/SimpleQSet.mdb") & ";")

                com.Connection = con
                Session("member") = "420Man"

                Dim ran As New Random
                Dim pointCnt As New DataTable
                Dim QID As String = ran.Next(0, TextBox1.Text.Length)
                Dim pnt As Integer = pointCnt.Rows(0)(0) + 1
                Dim Points As Integer

                Try

                    com.CommandText = "insert into QuestionList values('" + QID + "','" + TextBox1.Text + "','" + DropDownList1.SelectedValue + "','" + DateTime.Now.ToShortDateString() + "','" + Session("member") + "')"
                    con.Open()
                    com.ExecuteNonQuery()


                Catch ex As Exception
                    pointDs = New OleDbDataAdapter("select Points from Points where Member='" + Session("member") + "'", con)
                    pointDs.Fill(pointCnt)

                    Using cmd = New OleDbCommand()
                        com.CommandText = "update [Points]=@Points set Points='" & pnt & "' where Member='" & Session("member") & "'"

                        cmd.Parameters.AddWithValue("@Points", Points)
                        com.ExecuteNonQuery()
                    End Using
                Finally

                End Try

                con.Dispose()
                con.Close()
            Else
                errMessage.Text = "Question Cannot Be Blank"
                TextBox1.Focus()
            End If
        Else
            Response.Redirect("postNewQuestion-Confirmed.aspx")
        End If
        con.Dispose()
        con.Close()

    End Sub
Posted
Updated 24-Sep-17 22:40pm

1 solution

For starters, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And then, look at your code:
VB
Dim pointCnt As New DataTable
                Dim QID As String = ran.Next(0, TextBox1.Text.Length)
                Dim pnt As Integer = pointCnt.Rows(0)(0) + 1

You create the DataTable, but you don't give it any rows or columns: so when you try to access them, there is nothing there.
Probably what you want to do is read the value from the database, but frankly I'm not sure - that code doesn't look like it was thought about too much before it was written, and it's a bit ... random ... in what it's doing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900