Click here to Skip to main content
11,415,301 members (79,792 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 ...


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 15-Dec-12 22:45pm
Edited 25-Dec-12 19:18pm
v11
Comments
Krunal Rohit at 16-Dec-12 3:48am
   
Elaborate your question... Provide some more info..
-Krunal R.
selva_1990 at 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...
Krunal Rohit at 16-Dec-12 8:15am
   
provide your code..
selva_1990 at 16-Dec-12 8:25am
   
i updated
Krunal Rohit at 16-Dec-12 8:28am
   
please read it :
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingnavigator.addnewitem(v=vs.80).aspx
selva_1990 at 16-Dec-12 8:34am
   
i have seen but could you give one example to insert..,
Krunal Rohit at 16-Dec-12 8:36am
   
void customersBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = DemoCustomer.CreateNewCustomer();
}
Krunal Rohit at 16-Dec-12 8:40am
   
got something ??
selva_1990 at 16-Dec-12 8:43am
   
still i am not clear sir
Krunal Rohit at 16-Dec-12 8:47am
   
First, Don't call me Sir, I'm just 20 years old student... :)
Second, here CreateNewCustome is a method of Customer class.. So define one method and call it as mentioned..
selva_1990 at 16-Dec-12 8:56am
   
k k bro cool :)
Krunal Rohit at 16-Dec-12 8:58am
   
Yes, I am..
selva_1990 at 16-Dec-12 9:04am
   
but few month back i inserted directly without any coding..., just gave data source and data member for binding source and data binding source to textbox..it inserted but now not insertin
Krunal Rohit at 16-Dec-12 9:08am
   
For that I need to understand your scenario...
selva_1990 at 16-Dec-12 9:18am
   
i ll provide my entire program
Krunal Rohit at 16-Dec-12 9:20am
   
Proceed...
selva_1990 at 16-Dec-12 10:15am
   
i gave full scenario..sry for late network prob...
selva_1990 at 16-Dec-12 10:27am
   
waiting for ur reply bro dont forgot...
selva_1990 at 25-Dec-12 8:01am
   
are u there
Krunal Rohit at 25-Dec-12 8:07am
   
Yep..
selva_1990 at 25-Dec-12 8:52am
   
i have explained all waiting for ur reply
selva_1990 at 25-Dec-12 8:54am
   
by default bindingnavigator control contain toolstrip menu like movelast,movefirst,next,previous,add,delete...
in every tutorial no one explaining add & delete so i need how to add data and delete
Krunal Rohit at 25-Dec-12 21:17pm
   
Add new button..
see : http://stackoverflow.com/questions/1262984/where-is-the-code-behind-add-button-for-winform-bindingnavigator-control
selva_1990 at 25-Dec-12 21:34pm
   
I don't want to add button i need to add data into database using binding navigator its urgent you have to help me
Krunal Rohit at 25-Dec-12 21:36pm
   
Have you explore that link ?
selva_1990 at 25-Dec-12 21:42pm
   
i have seen but its about adding ... i just neeed to insert data into DB using bindingnavigator
Krunal Rohit at 25-Dec-12 21:46pm
   
Ya its adding, please see at MSDN : http://msdn.microsoft.com/en-in/library/system.windows.forms.bindingnavigator.addnewitem(v=vs.90).aspx

I've given you answer.. you can write code behind this button :)
selva_1990 at 25-Dec-12 22:04pm
   
this.customersBindingSource.AddNew();

tis not working .. i just add this code into my click event
Krunal Rohit at 25-Dec-12 22:34pm
   
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
// write query for inserting data in DB

}
selva_1990 at 25-Dec-12 22:42pm
   
i have wrote this code there "this.bindingSource1.AddNew();" its right ly know but its not inserting
Krunal Rohit at 25-Dec-12 22:44pm
   
You're not getting me..
I'm telling about : "INSERT INTO TABLE_NAME VALUES VAL1, VAL2, ..."

like this query.. Hope now you understand what I mean.. :)
selva_1990 at 25-Dec-12 23:06pm
   
con.Open();
cmd = new SqlCommand("insert into table dn values '" + textBox1.Text + "','" + textBox2.Text + "'", con);
cmd.ExecuteNonQuery();
con.Close();


u mean this query
Krunal Rohit at 25-Dec-12 23:19pm
   
ya..
selva_1990 at 26-Dec-12 0:04am
   
dude its not working...
Krunal Rohit at 26-Dec-12 0:12am
   
error???
selva_1990 at 26-Dec-12 0:18am
   
its not showing any error but its not adding in DB if u dont mind can u do one favor for me
Krunal Rohit at 26-Dec-12 0:19am
   
what ?
selva_1990 at 26-Dec-12 0:32am
   
can u leave video tutorial or attach full project with one simple example... its my project work
Krunal Rohit at 26-Dec-12 0:35am
   
I can give you after 2 days.. Because my exams are going on.. only 2 papers are left... but for sure, I'll mail you code for that after 2 days.. If you're good with this, provide your mail..
selva_1990 at 26-Dec-12 0:42am
   
ya fine...
selvadominates@gmail.com i need insert,delete,update using binding navigator..
i m waiting for ur mail, it'll be more helpful for me.., dont forgot.. ALL BEST FOR Exam :) :)
Krunal Rohit at 26-Dec-12 0:51am
   
Thanks :)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,200
1 OriginalGriff 7,512
2 Maciej Los 3,710
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web03 | 2.8.150427.4 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100