I am creating one application visual basic and sqlce
I have two forms name Form1 and Form2.

I used Form1 to add data to database using OLeDb command and used Form2 to show data in datagridview by using dataset

I used following code:

FFORM1 CODE is as following

Private Sub Add_data()
        
	Try
           
            Dim DBConn As OleDbConnection
            Dim DBInsert As New OleDbCommand


            DBConn = New OleDbConnection(Myconnection_String)
            DBInsert.Connection = DBConn
            DBInsert.Connection.Open()
         
            DBInsert.CommandText = "INSERT INTO user(name, designation, age) VALUES (" + textbox1.text + "," + textbox2.text + "," + textbox3.text + ")" 
            
            DBInsert.ExecuteNonQuery()

            MsgBox("Data added successfully.", MsgBoxStyle.OkOnly)

            DBConn.Close()
            DBConn.Dispose()
        Catch ex As System.Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub

FORM 2 : In visual studio 2008 designer I added a dataset named 'mydataset' and tableadapter 'usertableadapter' and succesfully created Fill query. I added one datagridview to form2 and in design mode bind it to the mydataset and its binding source


ON FORM2 I CODE IS AS FOLLOWING.

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        
        Me.userTableAdapter.Fill(Me.myDataSet.user)
        Me.DataGridView1.Refresh()
End Sub

My Problem is :

Form2 Datagridview show the old data only, it do not show the recently added data by form1. I mean
When I build my application datagridview show the data which is already in the database, but it does not show the data which is added by the form1 during run time.
Some one suggested me to fill the datagridview at run time by using OLeDB command.
Then I created Form3 to fill the datagridview2 by using OLedb command and datareader and found that Form3 shows the recently(added by Form1 at run time) added data.

Use of Designer dataset is required for me , because I am using the similar dataset to create report.


How my Form2 dataset,datagridview can show the latest data from the database

Recommended Answers

All 3 Replies

Your form2 code should be like this:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        
        Me.userTableAdapter.Fill(Me.myDataSet.user)
        Me.DataGridView1.DataSource = Me.myDataSet.user  ' Bind grid with table
        Me.DataGridView1.Refresh()
End Sub

youtube video for reference
I was having same problem in my case it was report viewer in your case it is datagrid
what i did was i again fill the dataset with the database values using sqldataadapter

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\typroject\TYPROJECT\TYPROJECT\logindb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
         billDataSet b1 = new billDataSet();
            SqlDataAdapter s = new SqlDataAdapter("select * from TblOrder",con);
            s.Fill(b1,b1.Tables[0].TableName);
            ReportDataSource rds = new ReportDataSource("orders",b1.Tables[0]);
            this.reportViewer1.LocalReport.DataSources.Clear();
            his.reportViewer1.LocalReport.DataSources.Add(rds);
           this.reportViewer1.LocalReport.Refresh();
            this.TblOrderTableAdapter.Fill(this.billDataSet.TblOrder, d1.ToString(), d2.ToString(), companyid);
            this.reportViewer1.RefreshReport();

to update datagridvies at run time
'UI is the form name
'modify ur datasetname and table adaptor names run

UI.ProductTableAdapter5.Fill(UI.Database_psmsDataSet18.product)
UI.DataGridView2.DataSource = UI.Database_psmsDataSet18.product ' Bind grid with table
UI.DataGridView2.Refresh()

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.