CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2011
    Posts
    1

    Update data into database

    My Code:
    Code:
    Imports System.Data
    Public Class Form4
        Dim dt As New DataTable()
        Dim rowIndex As Integer = 0
        Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=Library_System.mdb"
        Dim sqlStr As String = "SELECT * FROM Book_Issue_Details"
        Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connstr)
        Dim issueID As Integer
    
    
        Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    
            txtDateIssue.Clear()
            txtISBN.Clear()
            txtSID.Clear()
    
        End Sub
    
        Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
    
            Form2.Show()
    
            Me.Close()
    
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            End
        End Sub
    
        Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
    
            Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connstr)
            Dim commandBuilder As New OleDb.OleDbCommandBuilder(dataAdapter)
            Dim dsNewRow As DataRow
            dsNewRow = dt.NewRow()
    
            dsNewRow.Item("Book_ISBN") = txtISBN.Text
            dsNewRow.Item("Date_Issued") = DateString
            dsNewRow.Item("Book_Issued_To") = txtSID.Text
            dt.Rows.Add(dsNewRow)
            dataAdapter.Update(dt)
            rowIndex = dt.Rows.Count - 0
            issueID = dt.Rows(rowIndex)("Issue_ID") + 1
            txtISBN.Text = issueID
    
            MsgBox("Book Issued Successfully")
            Form2.Show()
            Me.Hide()
    
        End Sub
    
        Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connstr)
            dataAdapter.Fill(dt)
            dataAdapter.Dispose()
    
        End Sub
    End Class
    My problem is the form is loading and when I type in the data and want to update my database. It says 'System.IndexOutOfRangeException was unhandled'. Anyone help me with this? Where do I have problem in the code?
    Attached Images Attached Images  
    Last edited by WizBang; January 20th, 2011 at 11:49 AM. Reason: Added [code] tags

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Update data into database

    Your Database has 5 colums, however you only handing it 3 colums of data.. If the other two are not set to accept nulls then that may throw an error...

    However if you could show us exactly what line of code it highlights when you get this error , it'll be allot easier to work out the problem
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Dec 2007
    Posts
    234

    Re: Update data into database

    It's also in the wrong place... that's .NET code... not VB6.... I'll alert the authorities...

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Update data into database

    [ moved ]
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured