Click here to Skip to main content
14,098,650 members
Rate this:
 
Please Sign up or sign in to vote.
Dear All,

Problem Case:
When adapter.Update(dataSet) is called then Data is not updated in DataBase.

Here is my some piece of code. Complete code can be shared if demanded.
//Declared at Class level
SqlConnection conn = null;
SqlDataAdapter adptr = null;
DataSet dataSet = new DataSet();

// for Update button click event
private void btn_Update_Click(object sender, EventArgs e)
{
    adptr.UpdateCommand = new SqlCommandBuilder(adptr).GetUpdateCommand();
    adptr.Update(dataSet);
}

//For Fetch button click event //Select command
private void btn_FetchName_Click(object sender, EventArgs e)
{
    textBox_EmpName.DataBindings.Clear();
    dataSet.Clear();
    adptr = new SqlDataAdapter("SELECT EmpId, EmpName FROM EmpBasicInfo WHERE EmpId='" + textBox_EmpId.Text + "'", conn);
    adptr.Fill(dataSet);

    textBox_EmpName.DataBindings.Add(new Binding("Text", dataSet.Tables[0], "EmpName"));
    //MessageBox.Show("Name:" + dataSet.Tables[0].Rows[0][0]);

}
Posted
Updated 4-Nov-13 23:18pm
v5
Comments
Richard C Bishop 30-Oct-13 13:09pm
   
You are not updating anything. You are merely selecting from it.
DoingWork 31-Oct-13 0:48am
   
Plz read my code again. I'm trying to update whole dataSet in database by this piece of code.

// for Update button click event
private void btn_Update_Click(object sender, EventArgs e)
{
adptr.UpdateCommand = new SqlCommandBuilder(adptr).GetUpdateCommand();
adptr.Update(dataSet);
}
Foothill 31-Oct-13 17:46pm
   
First question, do you have necessary permissions to update the database as your login?
DoingWork 1-Nov-13 0:48am
   
Yes, I've.
SQL-08 was installed with windows authentication mode. 2ndly when I write code to update (value in Database) without binding then it works fine.
But when I try to update (value in database) with binding, it fails. Even there is no error/Exception occurrence.
Salman622 4-Nov-13 3:29am
   
can you provide the complete .cs page
DoingWork 4-Nov-13 4:44am
   
Yes....
Here is my complete code .cs page

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.ComponentModel;

namespace Binding_and_ReprotingTest
{
public partial class Form1 : Form
{
SqlConnection conn = null;
SqlDataAdapter adptr = null;
DataSet dataSet1 = new DataSet();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection(@"Data Source=.\MSSQLSERVER1;Initial Catalog=DB_Test;Integrated Security=True");
conn.Open();
}

private void btn_Update_Click(object sender, EventArgs e)
{
MessageBox.Show("Name:" + dataSet1.Tables[0].Rows[0][1]);
adptr.UpdateCommand = new SqlCommandBuilder(adptr).GetUpdateCommand();
adptr.Update(dataSet1.Tables[0]);
}

private void btn_FetchName_Click(object sender, EventArgs e)
{
textBox_EmpName.DataBindings.Clear();
dataSet1.Clear();
adptr = new SqlDataAdapter("SELECT EmpId, EmpName FROM EmpBasicInfo WHERE EmpId='" + textBox_EmpId.Text + "'", conn);
adptr.Fill(dataSet1);

textBox_EmpName.DataBindings.Add(new Binding("Text", dataSet1.Tables[0], "EmpName"));
}
}
}
Amitava Bag (Kolkata) 5-Nov-13 5:52am
   
Place a debugger break-point at the line "adptr.UpdateCommand = .....". Please check what it is showing. Place another break point at "adptr.Update(dataSet1.Tables[0]);". Is the updated value displaying in datatable withing this dataset?
DoingWork 5-Nov-13 6:28am
   
At first break point it is showing this update command: "UPDATE [EmpBasicInfo] SET [EmpId] = @p1, [EmpName] = @p2 WHERE (([EmpId] = @p3) AND ((@p4 = 1 AND [EmpName] IS NULL) OR ([EmpName] = @p5)))"

At 2nd break point, I've verified that updated value displaying in datatable within this dataset.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

//Alter "btn_FetchName_Click" Event

//Alter Line
textBox_EmpName.DataBindings.Add(new Binding("Text", dataSet1.Tables[0], "EmpName", false, DataSourceUpdateMode.OnPropertyChanged));

//New Line
dataSet1.Tables[0].RowChanged += Form1_RowChanged;


//Add New Event
void Form1_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change)
            {
                adptr.Update(dataSet1.Tables[0]);
            }
        }




//Alter "btn_Update_Click" Event

//Delete Line
//adptr.Update(dataSet1.Tables[0]);

//Add Line after -- adptr.UpdateCommand = new SqlCommandBuilder(adptr).GetUpdateCommand();
dataSet1.Tables[0].AcceptChanges();
   
Comments
DoingWork 2-Dec-13 4:28am
   
I inserted a break point at
if (e.Action == DataRowAction.Change)
in
void Form1_RowChanged(object sender, DataRowChangeEventArgs e)

Problem is that when I click on fetch button, every time number of RowChange event calls incremented by 2.
means on 1st click, no call.
2nd click: 2 calls
3rd click: 4 calls
4th click: 6 calls
and so on.....

There should be only one rowChanged event call. Why more than one ???
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

use !IsPostBack property in page_load
   
Comments
DoingWork 5-Nov-13 6:47am
   
Its not a Web application. I need guidance for Desktop application.
Visual studio: 2010
C# (Not WPF)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Please bind EmpID in textBox_EmpId with the specific datatable within dataset and don't alter the value of this textbox. Let me know whether the problem persist?
   
v2
Comments
DoingWork 5-Nov-13 6:55am
   
I did and test it as you advice me but still problem persist.
Amitava Bag (Kolkata) 5-Nov-13 7:13am
   
Can you check the datatable modified row's datarowstate value?
DoingWork 5-Nov-13 13:40pm
   
Tomorrow, I will check solution3 which is provided by you.
Please guide me that how I can check datatable modified row's datarowstate value ?? I'm new in C# learning.
Amitava Bag (Kolkata) 6-Nov-13 0:36am
   
dataSet1.Tables[0].Rows[0].RowState.
This rowstate basically shows which operation will occure in database (or made in datatable).
For more detail please visit http://msdn.microsoft.com/en-us/library/system.data.datarow.rowstate%28v=vs.110%29.aspx
DoingWork 7-Nov-13 2:06am
   
Without making changes as you suggested in solution 3, I simply checked "RowState" before update calling and after update calling. Both times, row state is unchanged.

Now I'm going to check solution (posted by you)
Amitava Bag (Kolkata) 8-Nov-13 3:11am
   
What happen? It works?
DoingWork 11-Nov-13 5:49am
   
Thanks............
Problem resolved.....
Please elaborate shortly that what was problem in my solution?????

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190518.1 | Last Updated 5 Nov 2013
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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