-
Jan 29th, 2018, 10:42 PM
#1
Thread Starter
New Member
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
-
Jan 30th, 2018, 12:15 AM
#2
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.
-
Jan 30th, 2018, 01:06 AM
#3
Thread Starter
New Member
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.
-
Jan 30th, 2018, 06:17 AM
#4
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:
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
-
Jan 30th, 2018, 06:28 AM
#5
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:
Dim Myadpt As OleDbDataAdapter
You then declare another at the method level in the Load event handler:
vb.net Code:
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.
-
Jan 30th, 2018, 07:27 AM
#6
Thread Starter
New Member
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.
-
Jan 30th, 2018, 07:49 AM
#7
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.
-
Jan 30th, 2018, 08:00 AM
#8
Thread Starter
New Member
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)
-
Jan 30th, 2018, 10:27 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|