Results 1 to 5 of 5

Thread: Add Record - MS Access

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    86

    Add Record - MS Access

    Hi,

    Ok i'm trying to add a new record to my database, below is the code i have but it doesnt seem to be working. Does anyone know why? I dont even get the message box saying new record added. Any help would be great.

    Code:
    ''BEGIN DATABASE CONNECTION
    
                con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = W:\Textvertising\database.mdb"
    
                da = New OleDb.OleDbDataAdapter(sql, con)
    
                logto = TextBox1.Text
                logfrom = TextBox2.Text
                logmessage = TextBox3.Text
    
    
    
                Dim cb As New OleDb.OleDbCommandBuilder(da)
                Dim dsNewRow As DataRow
    
                dsNewRow = ds.Tables("SMS").NewRow()
    
                dsNewRow.Item("ID") = logid
                dsNewRow.Item("To") = logto
                dsNewRow.Item("From") = logfrom
                dsNewRow.Item("Message") = logmessage
    
    
    
                ds.Tables("SMS").Rows.Add(dsNewRow)
    
                da.Update(ds, "SMS")
    
                MsgBox("New Record added to the Database")
    
                ''END DATABASE CONNECTION
    Thanks in advance

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Add Record - MS Access

    That's strange because your message box shoud be displayed regardles of how the Update will end (are you sure you don't get any error messages?)

    Or maybe your Update takes too long and it's not finished when you click stop. Try to run through your code step by step using F8 key instead of simply running it.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    86

    Re: Add Record - MS Access

    Hi,

    Noticed i get this error:

    A first chance exception of type 'System.NullReferenceException' occurred in SMS Messaging Program.exe

    What is it and how do i fix it?

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Add Record - MS Access

    Which line of code generates this exception?
    Do you develop with 'devenv' (standard Visual Studio IDE) or something else?
    If you put a breakpoint at the beginning of this procedure and then execute your code line by line (press F8 when breakpoint is hit) you'll find out what line uses a null reference.

    Any of these lines can generate a null reference, I suppose:
    vb.net Code:
    1. dsNewRow.Item("ID") = logid
    2. dsNewRow.Item("To") = logto
    3. dsNewRow.Item("From") = logfrom
    4. dsNewRow.Item("Message") = logmessage
    5.  
    6. ds.Tables("SMS").Rows.Add(dsNewRow)
    Check the spelling of your fields.

    Also check if the da variable is nothing or con variable is nothing.

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Add Record - MS Access

    dsNewRow.Item("ID") = logid
    I hope ID is the primary key. Is the ID column in your table an AutoNumber field? If yes, then comment out that line.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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