CODE

Public Sub InstituteCreateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstituteCreateButton.Click

Dim InstituteName As String
InstituteName = InstituteNameBox.Text
Dim InstituteCountry As String
InstituteCountry = InstituteCountryBox.Text

Try
Dim InstituteTableAdp As New InstituteBaseDataSetTableAdapters.InstituteTableTableAdapter
InstituteTableAdp.Insert(InstituteName, InstituteCountry)
MsgBox("Success")

Catch ex As Exception
MsgBox("Failed!")

End Try
InstituteCreateButton.Text = InstituteCountry
End Sub

The idea is to insert a new row when the user clicks the button. While testing, the output is success, yet when I preview the data table there is no record there.

Please Guide!

Recommended Answers

All 6 Replies

Still awaiting a reply.

try

InstituteBaseDataSet.acceptchanges after your inserting

Thanks for the suggestion, get this reply:

Error 1 Reference to a non-shared member requires an object reference.

The record is either added/inserted to the dataset/datatable or it is not. AcceptChanges will only change the rowstate (if it already exists in the dataset) of the record from New to Unchanged and will then prevent the record from then being saved to the database since the datatable sees it as unchanged.

Iammirko can you show an example of how your checking if this record is inserted? Also I dont see in this example where your creating an instance of this dataset and pointing the table adapter to it.

The record is either added/inserted to the dataset/datatable or it is not. AcceptChanges will only change the rowstate (if it already exists in the dataset) of the record from New to Unchanged and will then prevent the record from then being saved to the database since the datatable sees it as unchanged.

Iammirko can you show an example of how your checking if this record is inserted? Also I dont see in this example where your creating an instance of this dataset and pointing the table adapter to it.

I use VS 2008 (Express), I have used the wizard to create the data set.
I use the right side panel of data sources and right click on data set and click on preview data and get no result, moreover acceptchanges is not a (or at least does not appear as) member function of the InstituteBaseDataSet.

Hi! Got another way to do it. Working successfully now.

Public Sub InstituteCreateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstituteCreateButton.Click

        Dim InstituteName As String
        InstituteName = InstituteNameBox.Text
        Dim InstituteCountry As String
        InstituteCountry = InstituteCountryBox.Text

        Dim myconn As New SqlConnection()
        Dim mycomm As New SqlCommand()
        myconn.ConnectionString = ("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\InvestTrends\InvestTrends\InvestTrends\InstituteBase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

        Try
            myconn.Open()
        Catch ex As Exception
            MsgBox("Connection Opening Failed!")
        End Try

        mycomm.Connection = myconn

        Try
            mycomm.CommandText = "INSERT into InstituteTable(Name, Country) VALUES('" + InstituteName + " ',' " + InstituteCountry + "')"
            mycomm.ExecuteNonQuery()
            MsgBox("Success!")
        Catch ex As Exception
            MsgBox("Insert Failed!")
        End Try
        myconn.Close()
    End Sub

Thanks everyone for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.