Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
//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

C#
//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();
 
Share this answer
 
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 ???
use !IsPostBack property in page_load
 
Share this answer
 
Comments
DoingWork 5-Nov-13 6:47am    
Its not a Web application. I need guidance for Desktop application.
Visual studio: 2010
C# (Not WPF)
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?
 
Share this answer
 
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)

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