Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have used binding navigator and binding source to insert data and delete data from database but, its not inserting and deleting.... if i delete the data its deleting but there is no change in database and if add its adding but no update in database ...


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace datanavi
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlCommand cmd;

        // my table name is dn
        // It has two fields name,age
        // two textbox i m having in form 

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'textileDataSet.dn' table. You can move, or remove it, as needed.
            this.dnTableAdapter.Fill(this.textileDataSet.dn);

        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            //this code is not working while inserting
           con.Open();
            cmd = new SqlCommand("insert into table dn values '" + textBox1.Text + "','" + textBox2.Text + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

//i used bindingsource and bindingnavigator control to interact with Database table
Posted
Updated 25-Dec-12 18:18pm
v11
Comments
[no name] 16-Dec-12 3:48am    
Elaborate your question... Provide some more info..
-Krunal R.
selva_1990 16-Dec-12 3:56am    
there is a button available in binding navigator ("add new" with plus symbol)
i double clicked that and wrote command to insert data into database but it showing exception like i mentioned...
[no name] 16-Dec-12 8:15am    
provide your code..
selva_1990 16-Dec-12 8:25am    
i updated
[no name] 16-Dec-12 8:28am    
please read it :
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingnavigator.addnewitem(v=vs.80).aspx

1 solution

Since you are pulling data directly from the controls I assume you are not familiar with the BindingNavigator.BindingSource[^] property.

Have a look at Depends4Net - Part 2[^] for an example on how to bind to "custom" elements - which is what you usually have to operate on when you are using SqlCommand and SqlDataReader to access the database.

You can also switch to the Entity Framework, and if you do then you can have a look at:Entity Framework in WinForms[^]

Best regards
Espen Harlinn
 
Share this answer
 
v2

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