Problem in adding record to mdf file

guo84

Member
Joined
Feb 7, 2009
Messages
6
Programming Experience
1-3
Hi

I am using vb.net 2008 trying to add record to my mdf file.

Mdf file was created using sql server 2008.

Code:
' Dim builder As DataRowBuilder
Dim dsnewrow As New DataRow

dsnewrow = dscontact.Tables("Contact").NewRow

If mblnadd Then
Try
dsnewrow("ContactID") = cbocontact.Text
dsnewrow("Name") = txtcontactname.Text
dsnewrow("Mobile") = txtmobile.Text
dsnewrow("Fax") = txtfax.Text
dsnewrow("Email") = txtemail.Text
dscontact.Tables("Contact").Rows.Add(dsnewrow)
Catch ex As Exception
MessageBox.Show("Unable to add record", "Contact")
End Try
mblnadd = False
MessageBox.Show("Record Added Sucessfully", "Contact")
End If

Error Message:
'System.Data.Datarow.Protected Friend Sub New(builder as
System.data.datarowbuilder;) is not accessible in this context
becuase it is 'Protected Friend'.

May I know how do I solve this issue.
Thanks a million.
 

Attachments

  • addrecord.JPG
    addrecord.JPG
    127 KB · Views: 58
You can't create a new DataRow directly. The only way to create a new DataRow is to call NewRow on the DataTable you're going to add it to. You're already doing that in the very next line anyway so why are you even trying to create a new DataRow that way? You probably don't understand exactly what that line is trying to do. This:
VB.NET:
Dim dsnewrow As New DataRow
is shorthand for this:
VB.NET:
Dim dsnewrow As DataRow = New DataRow
You don't want to create a new DataRow and assign it to the variable though, because you're getting a DataRow from the DataTable the very next line. Just declare the variable. Do NOT create a new DataRow.
 
You seem to be using a typed dataset. Why are you using the generic accesses to it?

Here is how your code should look:

VB.NET:
' Dim builder As DataRowBuilder
Dim dsnewrow As dscontact.ContactRow = dscontact.Contact.NewContactRow()

If mblnadd Then
Try
dsnewrow.ContactID = cbocontact.Text
dsnewrow.Name = txtcontactname.Text
dsnewrow.Mobile = txtmobile.Text
dsnewrow.Fax = txtfax.Text
dsnewrow.Email = txtemail.Text
dscontact.Contact.AddContactRow(dsnewrow)

Catch ex As Exception
MessageBox.Show("Unable to add record", "Contact")
End Try
mblnadd = False
MessageBox.Show("Record Added Sucessfully", "Contact")
End If


Note that none of this goes any way toward adding the record to the database (MDF), only to the local dataset. TO get it to the database you have to use the tableadapter.Update function. If you havent already read it, read the DNU link in my signature. It might save you many hours of frustration
 
Type 'dscontact.ConactRow' is not defined.

Hi

Thanks for ur reply.

Understand I am asking a simple question,
But I am learning from my mistakes to move on as I am running out of time.:(

Like to check I try ur amended solution however it is stating
"Type 'dscontact.ConactRow' is not defined".

Attached is the screenshot.

Do I need to declare something?

Please advise. Thanks.
 

Attachments

  • addrecord.JPG
    addrecord.JPG
    121.4 KB · Views: 54
Oh.. You have dsapplication.xsd, dscompany.xsd etc.. it seems I assumed you had a dscontact too. If you don't then you can either create it, or revert your code. I'd create it, because working with strongly typed datasets is Better
 
make another dataset for dscontact, just like your other datasets, application, company.. etc
 
Able to add record, but did not capture data for name and email

Thanks finally able to add record.
However after adding i reliase data for name and email is not there.
How can i solve these bugs?

My code is to do validation first before trying to add a new datarow into the dataset.

Please advise. Thanks.:confused::(


VB.NET:
Private Sub AddCustomerToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddCustomerToolStripMenuItem.Click
        If txtcontactname.Text <> "" Then
            If IsNumeric(txtcontactname.Text) Then
                MessageBox.Show("Numeric not allowed for contact name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                If txtmobile.Text <> "" Then
                    If Not IsNumeric(txtmobile.Text) Then
                        MessageBox.Show("Alphabet not allowed for mobile no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        txtmobile.Focus()
                    End If

                    If txtfax.Text <> "" Then
                        If Not IsNumeric(txtfax.Text) Then
                            MessageBox.Show("Alphabet not allowed for fax no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            txtmobile.Focus()
                        End If
                        'If txtemail.Text <> "" Then
                        '    If Not IsNumeric(txtemail.Text) Then
                        '        MessageBox.Show("Alphabet not allowed for email.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        '        txtemail.Focus()
                        '    End If
                        'Else
                        '    MessageBox.Show("Please enter data for email.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                        'End If
                    Else
                        MessageBox.Show("Please enter data for fax no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    End If
                Else
                    MessageBox.Show("Please enter data for mobile no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If

        Else
            MessageBox.Show("Please enter data for contact name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If



        'Dim mblnadd As Boolean

        'If mblnadd Then
        Try
            Dim dsnewrow As DataRow = dscontact.Contact.NewRow
            dsnewrow("ContactID") = cbocontact.Text
            dsnewrow("Name") = CStr(txtcontactname.Text)
            dsnewrow("Mobile") = txtmobile.Text
            dsnewrow("Fax") = txtfax.Text
            dsnewrow("Email") = CStr(txtemail.Text)
            cbocontact.Focus()
            txtfax.Focus()
            dscontact.Contact.Rows.Add(dsnewrow)
            ContactTableAdapter.Update(dscontact)
        Catch ex As Exception
            MessageBox.Show("Unable to add record" & ControlChars.NewLine & ex.Message, "Contact", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        'mblnadd = False
        MessageBox.Show("Record Added Sucessfully", "Contact", MessageBoxButtons.OK, MessageBoxIcon.None)
        'End If


    End Sub

 Private Sub frmcontact_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        closeallforms()
        Dim temp As Integer
        Me.ContactTableAdapter.Fill(Me.dscontact.Contact)
        cbocontact.SelectedIndex = cbocontact.Items.Count - 1
        txtcontactname.Clear()
        txtmobile.Clear()
        txtfax.Clear()
        txtemail.Clear()
End sub
 
Last edited by a moderator:
Back
Top