Results 1 to 9 of 9

Thread: VB 10 Save Button Click

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    4

    VB 10 Save Button Click

    Hi I am new to VB,

    When I click Save button my error goes to below line saying nullvalue.

    Is it belolw line is right code to update value to MS access tables from datagrid.


    Myadpt.Update(Dataset.Tables(0))

    Regards

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: VB 10 Save Button Click

    That means that either 'Myadpt' is Nothing or 'Dataset' is Nothing. Please show us the entire method that contains that line of code and also the code where you think those two objects were created.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    4

    Re: VB 10 Save Button Click

    Below is the entire code.
    Code:
    Imports System.Data.OleDb
    Public Class Form1
    
        Dim Myconnection As OleDbConnection
        Dim Myadpt As OleDbDataAdapter
        Dim CMDbuilder As OleDbCommandBuilder
        Dim Dataset As New DataSet
        Dim mytable As New DataTable
        Dim fname As String
        Dim lname As String
        Dim phonenumber As String
        Dim mobilenumber As String
        Dim emailaddress As String
        Dim Maxrows As Integer
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            btnsave.Visible = (False)
            Try
                Myconnection = New OleDbConnection
                Myconnection.ConnectionString = "Provider=Microsoft.Ace.Oledb.12.0;Data source = C:\Contact\Contact\contact.accdb"
    
                Myconnection.Open()
                Dim Myadpt As OleDbDataAdapter = New OleDbDataAdapter(String.Format("select * from contact"), Myconnection)
                CMDbuilder = New OleDbCommandBuilder(Myadpt)
                Myadpt.Fill(Dataset, "Contact")
                '--------
                Maxrows = Dataset.Tables("Contact").Rows.Count
                '---------------
    
                Contactview.DataSource = Dataset.Tables("contact")
                Contactview.Refresh()
                Contactview.AllowUserToAddRows = False
                Contactview.ReadOnly = True
    
            Catch ex As Exception
    
                MsgBox("Error")
    
            End Try
    
        End Sub
    
        Private Sub Btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
    
            For Each txt In gbox.Controls
                If TypeOf txt Is TextBox Then txt.enabled = True
    
            Next
    
            btnsave.Visible = (True)
    
        End Sub
    
        Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
            Me.Close()
        End Sub
    
        Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
            Dim newrow As DataRow
            For Each datarow In Contactview.Rows
                datarow.selected = False
    
            Next
            Maxrows = Dataset.Tables(0).Rows.Count
    
            fname = txtfname.Text
            lname = txtlname.Text
            phonenumber = txtphonenumber.Text
            mobilenumber = txtmobilenumber.Text
            emailaddress = txtemailaddress.Text
    
            newrow = Dataset.Tables(0).NewRow()
    
            newrow.Item(0) = Maxrows
            newrow.Item(1) = fname
            newrow.Item(2) = lname
            newrow.Item(3) = phonenumber
            newrow.Item(4) = mobilenumber
            newrow.Item(5) = emailaddress
            Dataset.Tables("contact").Rows.Add(newrow)
    
    
            Myadpt.Update(Dataset.Tables("contact"))
            Contactview.DataSource = Dataset.Tables("contact")
            Contactview.Refresh()
            Contactview.Rows(Maxrows).Selected = True
    
            For Each txt In gbox.Controls
                If TypeOf txt Is TextBox Then txt.enabled = False
    
    
            Next
    
            btnsave.Visible = (False)
    
            txtfname.Text = ""
            txtlname.Text = ""
            txtphonenumber.Text = ""
            txtmobilenumber.Text = ""
            txtemailaddress.Text = ""
    
            
            Maxrows += 1
        End Sub
    
    End Class
    Last edited by Shaggy Hiker; Jan 30th, 2018 at 10:25 AM. Reason: Added CODE tags.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: VB 10 Save Button Click

    I didn't ask for the entire code. I asked for the code that was relevant to the problem. If you expect us to wade through code to focus on what's relevant so you don't have to, you're likely to find people becoming less enthused about helping.

    Also, please use formatting tags when posting code for readability.
    vb.net Code:
    1. Imports System.Data.OleDb
    2. Public Class Form1
    3.  
    4.     Dim Myconnection As OleDbConnection
    5.     Dim Myadpt As OleDbDataAdapter
    6.     Dim CMDbuilder As OleDbCommandBuilder
    7.     Dim Dataset As New DataSet
    8.     Dim mytable As New DataTable
    9.     Dim fname As String
    10.     Dim lname As String
    11.     Dim phonenumber As String
    12.     Dim mobilenumber As String
    13.     Dim emailaddress As String
    14.     Dim Maxrows As Integer
    15.  
    16.  
    17.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    18.  
    19.         btnsave.Visible = (False)
    20.         Try
    21.             Myconnection = New OleDbConnection
    22.             Myconnection.ConnectionString = "Provider=Microsoft.Ace.Oledb.12.0;Data source = C:\Contact\Contact\contact.accdb"
    23.  
    24.             Myconnection.Open()
    25.             Dim Myadpt As OleDbDataAdapter = New OleDbDataAdapter(String.Format("select * from contact"), Myconnection)
    26.             CMDbuilder = New OleDbCommandBuilder(Myadpt)
    27.             Myadpt.Fill(Dataset, "Contact")
    28.             '--------
    29.             Maxrows = Dataset.Tables("Contact").Rows.Count
    30.             '---------------
    31.  
    32.             Contactview.DataSource = Dataset.Tables("contact")
    33.             Contactview.Refresh()
    34.             Contactview.AllowUserToAddRows = False
    35.             Contactview.ReadOnly = True
    36.  
    37.         Catch ex As Exception
    38.  
    39.             MsgBox("Error")
    40.  
    41.         End Try
    42.  
    43.     End Sub
    44.  
    45.     Private Sub Btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
    46.  
    47.         For Each txt In gbox.Controls
    48.             If TypeOf txt Is TextBox Then txt.enabled = True
    49.  
    50.         Next
    51.  
    52.         btnsave.Visible = (True)
    53.  
    54.     End Sub
    55.  
    56.     Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
    57.         Me.Close()
    58.     End Sub
    59.  
    60.     Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
    61.         Dim newrow As DataRow
    62.         For Each datarow In Contactview.Rows
    63.             datarow.selected = False
    64.  
    65.         Next
    66.         Maxrows = Dataset.Tables(0).Rows.Count
    67.  
    68.         fname = txtfname.Text
    69.         lname = txtlname.Text
    70.         phonenumber = txtphonenumber.Text
    71.         mobilenumber = txtmobilenumber.Text
    72.         emailaddress = txtemailaddress.Text
    73.  
    74.         newrow = Dataset.Tables(0).NewRow()
    75.  
    76.         newrow.Item(0) = Maxrows
    77.         newrow.Item(1) = fname
    78.         newrow.Item(2) = lname
    79.         newrow.Item(3) = phonenumber
    80.         newrow.Item(4) = mobilenumber
    81.         newrow.Item(5) = emailaddress
    82.         Dataset.Tables("contact").Rows.Add(newrow)
    83.  
    84.  
    85.         Myadpt.Update(Dataset.Tables("contact"))
    86.         Contactview.DataSource = Dataset.Tables("contact")
    87.         Contactview.Refresh()
    88.         Contactview.Rows(Maxrows).Selected = True
    89.  
    90.         For Each txt In gbox.Controls
    91.             If TypeOf txt Is TextBox Then txt.enabled = False
    92.  
    93.  
    94.         Next
    95.  
    96.         btnsave.Visible = (False)
    97.  
    98.         txtfname.Text = ""
    99.         txtlname.Text = ""
    100.         txtphonenumber.Text = ""
    101.         txtmobilenumber.Text = ""
    102.         txtemailaddress.Text = ""
    103.  
    104.        
    105.         Maxrows += 1
    106.     End Sub
    107.  
    108. End Class

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: VB 10 Save Button Click

    The issue is that you have two variables named 'Myadpt'. You declare one at the class level:
    vb.net Code:
    1. Dim Myadpt As OleDbDataAdapter
    You then declare another at the method level in the Load event handler:
    vb.net Code:
    1. Dim Myadpt As OleDbDataAdapter = New OleDbDataAdapter(String.Format("select * from contact"), Myconnection)
    It's that local variable that you assign an object to and use to retrieve the data, but that variable ceases to exist when the Load event handler completes and so that object becomes inaccessible. Later on, in the Click event handler of your save Button, you try to use the class-level variable to save the changes but you never assigned an object to that variable. As a result, that variable is Nothing, just as I said earlier.

    The solution is to not declare that second variable at the method level. You don't do that for 'Myconnection' or 'Dataset' so there's no reason to do it for 'Myadpt' either.

    On an unrelated note, I would suggest that you be a bit more consistent with your naming. If you have a variable named 'Myconnection' then why use 'Myadpt' rather than 'Myadapter' and why use 'Dataset' instead of 'Mydataset'? Personally, I would do away with the "My" prefix altogether. For those variables, I would tend to use 'connection', 'adapter' and 'data' but whatever you choose to go with, try to be consistent. Don't use abbreviations in some places and not others for no apparent reason and don't use prefixes in some places and not others either.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    4

    Re: VB 10 Save Button Click

    Sorry for sending entire code which made others to consume time. Now I will study your second reply. Thank you.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: VB 10 Save Button Click

    The problem with too much code is that it can end up hiding the issue. It's not always easy to determine what is relevant but I did specify what to post on this occasion. The easier you can make it for us to help, the more likely we will be inclined and able to help, so the better for you.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    4

    Re: VB 10 Save Button Click

    Thank you so much I changed code in method level and working fine.

    adapter = New OleDbDataAdapter(String.Format("select * from contact"), connection)

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: VB 10 Save Button Click

    I edited your post to add [CODE][/CODE] tags. You can do this by pressing the # button and pasting the code between the tags. To use the code formatting that JMC used in post #4, use the VB button (which looks like VE on many browsers).
    My usual boring signature: Nothing

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