Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!

i'm using Ying Bai - Practical Database Programming with Visual C#.Net book to built my application(at least some parts of it).

about inserting data to db it says that you can use TableAdapter.Insert method to add data directly to the db. well I did it all like the example said and when I run my application I can see the added data but when I close the application and run it again then the data is not there.

One thing that makes me confused is that it says in the theory part that you need to run executeNonQuery command for this method to work, but in the example code the command is not there and it's suppose to work!

please Help! :)

Am I missing something or missunderstanding?

here's my code:

this is my insert statement built in the tableAdapter via dataset designer, the query is called InsertNewKombiQuery:
SQL
INSERT INTO KombiStateTbl(NumberPlate, LicenseExpiryDate, ControlDate, Documentos, FaróisLanternas, EstepeMacacoChaveDeRoda, Triângulo, Extintor, Acendedor, RodasCalota, Retrovisores, Párabrisa, BancosTapetes, Pneus, ParachoqueDianteiro, ParachoqueTraseiro, PortaDianteiraDireitaM, PortaDianteiraEsquerdaP, PortaLateral, LateralEsquerda, LateralEsquerdaTraseira, LateralDireitaTraseira, ParalamaTraseiroDireito, ParalamaTraseiroEsquerdo, Teto, PortaDoPortaMalas, PortaTraseiraDoMotor, Traseira, Dianteira, FaróisDianteiros, FaroisTraseiros, CurrentPic, CurrentPicPath) VALUES (@Param1,@Param2,@Param3, 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', NULL, NULL);


and code in my form:

C#
private void okButton_Click(object sender, EventArgs e)
int intInsert = 0;
            
intInsert = kombiStateTblTableAdapter.InsertNewKombiQuery(noPlateTextBox.Text,
                licenseDateTimePicker.Value.Date, controlDateTimePicker.Value.Date);
            
            if (intInsert != 0)
            {
                MessageBox.Show("The data insertion is successful", "Success", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                noPlateTextBox.Text = "";                
                okButton.Enabled = false;
                this.Close();
            }
            else
            {
                MessageBox.Show("The data insertion is failed", "Error", MessageBoxButtons.RetryCancel,
                    MessageBoxIcon.Error);
            }            
        }
        {
Posted

You havent mentioned about what is written inside the method InsertNewKombiQuery but you said the Author explained about calling ExecuteNonQuery method to dataAdapter class.

Yes inorder insert into the database table you need to perform the action on the database itself having the data's inside your local adapter class doesnt mean that you have inserted the data successfully to your database.
using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }

check your code/class for above piece of code which performs the actual database insert.
you may modify your code to pass the insert statement as the query string.
 
Share this answer
 
Try calling the table adpaters .Update(dataset) method after the insert call. This should commit the changes to the database.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900