Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > [OleDB, DataSet] How to insert new data into database
 

[OleDB, DataSet] How to insert new data into database

Hello,

I try to insert data into the .mdb file . I use DataSet and DataAdapter object and OleDB library. I print the data from a file without any problem using DataGrid class.
In the Form1_Load i have line of code:
this
.tabela1TableAdapter.Fill(this
.zadaniaDataSet.Tabela1);
Then I add a button, after clicking on I want to save data into my database. For example for columns 'Tytul' and 'Opis' to my database called 'Tabela1' :
private void button1_Click(object sender, EventArgs e)
        {
            ZadaniaDataSet.Tabela1Row newTabela1Row;
            newTabela1Row = ZadaniaDataSet.Tabela1.NewTabela1Row();
            newTabela1Row.Tytul = "Cos";
            newTabela1Row.Opis = "cos cos cos";

            // Add the row to the Region table
            this.zadaniaDataSet.Tabela1.Rows.Add(newTabela1Row);

            // Save the new row to the database
            this.tabela1TableAdapter.Update(this.zadaniaDataSet.Tabela1);

        }
I have followed this specyfication : http://msdn.microsoft.com/en-us/library/ms233812(VS.80 ).aspx

During the compilation debuger shows following mistake:

for : newTabela1Row = ZadaniaDataSet.Tabela1.NewTabela1Row();

Error1An object reference is required for the non-static field, method, or property 'WindowsFormsApplication5.ZadaniaDataSet.Tabela1.get'C:\Users\ir3\Documents\Visual Studio 2008\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Form1.cs3629WindowsFormsApplication5

I will be gratefull for any help. What's wrong with my code?



ir3nicus  Tuesday, April 14, 2009 2:07 PM
Hi,

From the error you got, it seems that the DataSet type name is ZadaniaDataSet. You should use the variable name of the DataSet instanceon the form rather than the DataSet typename. For example, if the variable name of the DataSet instance is zadaniaDataSet1, modify your code as follows:

newTabela1Row = zadaniaDataSet1.Tabela1.NewTabela1Row();

In addition, you should call the AcceptChanges method on the dataset after you save the changes back to DB successfully.

Hope this helps.

Sincerely,
Linda Liu
Linda Liu  Thursday, April 16, 2009 10:01 AM
Please see:

How to: Insert New Records into a Database

TableAdapter Select and Insert Queries with Parameters
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Wednesday, April 15, 2009 3:22 AM
Hi,

From the error you got, it seems that the DataSet type name is ZadaniaDataSet. You should use the variable name of the DataSet instanceon the form rather than the DataSet typename. For example, if the variable name of the DataSet instance is zadaniaDataSet1, modify your code as follows:

newTabela1Row = zadaniaDataSet1.Tabela1.NewTabela1Row();

In addition, you should call the AcceptChanges method on the dataset after you save the changes back to DB successfully.

Hope this helps.

Sincerely,
Linda Liu
Linda Liu  Thursday, April 16, 2009 10:01 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView / DataBoundItem Question
• Problem with setting the selected color for row to Transparent
• Why does the GotFocus event fire twice?
• Updating a databinding
• DataGridView Cell click problem
• date format problem
• How to put new row at the top of the datagrid.
• Custom Control that's databound, reseting itself on leave
• Help removing indicator/pointer in row header of datagridview
• Bind two data column's value to a single control?