473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C# - FORM: DataAdapter.Update() doesn't work :(

Hello,

I wrote a little program to understand the ADO.NET's mechanism. It's a common program with a datagrid, textboxes and a toolstripbar with usually buttons. The problem is that meanwhile i add rows to dataset the save button works when i remove some of them the update method doesn't work.

Please see the following code:

public Popola()
{
InitializeComponent();
string connstring = "Data Source = .\\SQLEXPRESS; Initial Catalog = scuola; Integrated Security = True";
conn = new SqlConnection(connstring);

myset = new DataSet("Classe");

myadapter = new SqlDataAdapter("SELECT * FROM Classe ORDER BY IDClasse ASC", conn);
myadapter.TableMappings.Add("Table", "Classe");
myadapter.Fill(myset);

da2 = new SqlDataAdapter("SELECT * FROM Studente ORDER BY Cognome ASC", conn);
da2.TableMappings.Add("Table", "Studente");
da2.Fill(myset);

DataColumn colMaster1;
DataColumn colDetail1;
colMaster1 = myset.Tables["Classe"].Columns["IDClasse"];
colDetail1 = myset.Tables["Studente"].Columns["IDClasse"];
relClasStud = new DataRelation("RelClasStud", colMaster1, colDetail1);
myset.Relations.Add(relClasStud);
myset.EndInit();

dsView = myset.DefaultViewManager;
.......
cm_clas = (CurrencyManager)this.BindingContext[dsView, "Classe"];
cm = (CurrencyManager)this.BindingContext[dsView, "Classe.RelClasStud"];
}

//Remove and Save buttons code:
private void Remove_Click(object sender, EventArgs e)
{

DataRowView rw_ = (DataRowView)cm.Current;
DataRow rw = rw_.Row;
myset.Tables["Studente"].Rows.Remove(rw);
cm.EndCurrentEdit();
//myset.AcceptChanges();
cm.Position = 0;
textBoxPosition.Text = String.Format("Record {0} of {1}", (cm.Position + 1), cm.Count);
}

private void btnSalva_Click(object sender, EventArgs e)
{
conn.Open();

SqlCommandBuilder sqlcmd = new SqlCommandBuilder(da2);
SqlCommandBuilder sqlcmd1 = new SqlCommandBuilder(myadapter);
da2.Update(myset.Tables["Studente"]);
myadapter.Update(myset.Tables["Classe"]);
conn.Close();

}

Can you help me? T.T
Regards.
Pietro.
Sep 19 '07 #1
1 2106
Ok.. I resolve it!

Just replace the lines:
Expand|Select|Wrap|Line Numbers
  1.     myset.Tables["Studente"].Rows.Remove(rw);
  2.    cm.EndCurrentEdit(); 
  3.  
with:
Expand|Select|Wrap|Line Numbers
  1.       rw.Delete();
  2.  
Thanks anyway.
Pietro.
Sep 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: hch | last post by:
dataAdapter.Update(data, "TableName") won’t work! I was about to deploy my first website on the Internet only to discover that the dataAdapter.Update() throws the Server Error in the third...
6
by: Geoff Pennington | last post by:
I have a class method that returns a DataAdapter. I want to access the table(s) contained in the DataAdapter. Of course, accessing the DataSets would be good enough, because I could get the tables...
16
by: Adda | last post by:
If I cycle through the MdiChildActivate event of the parent form I can read text in a textbox on the child mdiform -- console.writeline(Me.ActiveMdiChild.Controls(1).Text) But if I have a sub...
13
by: Doug Bell | last post by:
Hi, I thought I had this sorted this morning but it is still a problem. My application has a DataAccess Class. When it starts, it: Connects to a DB (OLE DB) If it connects it uses an...
9
by: cherishman | last post by:
When I use self-defined database, sometimes the dataadapter can't auto create insertcommand, updatecommand and deletecommand, it just only create selectcommand? But when I use these database such...
3
by: RSH | last post by:
Hi, I have a situation in where i have two instances of SQL server, the first is our Production Environment, the second is our Development environment. Both servers contain the same databases...
1
by: Franklin M. Gauer III | last post by:
In VS2003 I used to be able to drag a SQL Server table from Server Explorer onto my webservice (in design mode) and it would automatically create a DataAdapter for me. This doesn't work in VS2005...
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.