473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

update database from datatable using commandbuilder

1 New Member
hai,

am only beginer in c#...
i am trying to connect database with webform. using a technique that the fields of the table and the controls in a form are named same...then i try get the controls and check their name with
database fields names if they equal then i plan to do addition or update etc...

MY PROBLEM is, i am succeed in addition, but know i try to edit the same field. i assign the datatable and made the changes in that datatable..now the datatable is edited..i check that...now i try to update it into database using commandbuilder. ..nothing will updated ...my codes are below
PLZ HELP ME......

for add a new row in a table i use below comand it will work....

Expand|Select|Wrap|Line Numbers
  1. create a datatable and assign the required table using select command
  2.  DataRow dr = dt.NewRow();
  3.          foreach (Control c in this.form1.Controls)
  4.          {
  5.              for (int i = 0; i <= dt.Columns.Count-1; i++)
  6.              {
  7.                  if (dt.Columns[i].ColumnName == c.ID)
  8.                  {
  9.                      switch (c.GetType().ToString())
  10.                      {
  11.                          case "System.Web.UI.WebControls.TextBox":
  12.                              TextBox tx = (TextBox)c;
  13.                                 dr[c.ID]  =tx.Text; 
  14.                                break;
  15.                            case "System.Web.UI.WebControls.DropDownList":
  16.                                DropDownList  drop = (DropDownList)c;
  17.                                dr[c.ID] = drop.Text;
  18.                                break;
  19.                      }
  20.                  }
  21.              }
  22.        }
  23.        dt.Rows.Add(dr);
  24.        string constr1;
  25.        SqlConnection sqlcon1;
  26.        constr1 = "server=dotnet6;database=staff;user id=sa;password=sa;";
  27.        sqlcon1 = new SqlConnection(constr1);
  28.        string sqlquery = "select * from staffdetails";
  29.        SqlDataAdapter da = new SqlDataAdapter(sqlquery,sqlcon1);
  30.        SqlCommandBuilder blr = new SqlCommandBuilder(da);
  31.        da.Update(dt);
  32.        da.Dispose();
  33.  
  34. BUT FOR EDIT A ALREADY ONE I WROTE THE FOLLOW..IT WON'T WORK..
  35.  
  36. DataRow dr = dt.Rows[dt.Rows.Count - 1];
  37.        dr.BeginEdit();
  38.        foreach (Control c in this.form1.Controls)
  39.        {
  40.            for (int i = 0; i <= dt.Columns.Count - 1; i++)
  41.            {
  42.                if (dt.Columns[i].ColumnName == c.ID)
  43.                {
  44.                    switch (c.GetType().ToString())
  45.                    {
  46.                        case "System.Web.UI.WebControls.TextBox":
  47.                            TextBox tx = (TextBox)c;
  48.                            dr[c.ID] =tx.Text;
  49.                            break;
  50.                        case "System.Web.UI.WebControls.DropDownList":
  51.                            DropDownList drop = (DropDownList)c;
  52.                            dr[c.ID] = drop.Text;
  53.                            break;
  54.                    }
  55.  
  56.                }
  57.            }
  58.        }
  59.        dt.AcceptChanges();
  60.        dr.EndEdit();
  61.        string constr1;
  62.        SqlConnection sqlcon1;
  63.        constr1 = "server=dotnet6;database=staff;user id=sa;password=sa;";
  64.        sqlcon1 = new SqlConnection(constr1);
  65.        string sqlquery = "select * from staffdetails";
  66.        SqlDataAdapter da = new SqlDataAdapter(sqlquery, sqlcon1);
  67.        SqlCommandBuilder blr = new SqlCommandBuilder(da);
  68.        da.Update(dt);
  69.       da.Dispose();
why... plz help me.. i'm only a beginer so if i made some major mistakes don't blame me...
plzz help me
Nov 23 '07 #1
1 3271
CyberSoftHari
487 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. dt.AcceptChanges();
  2. dr.EndEdit();
Chech here, dt.AcceptChanges() ; and dr.EndEdit();
I suggest to use sql update query.
(BTW, why don’t you use code tags for your code? and i am unable to point the line number.)
Nov 23 '07 #2

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

Similar topics

2
4266
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 underline. It was working fine before. ConnectionString in Web.config: ----------------------------------------- <?xml version="1.0" encoding="utf-8" ?> <configuration>
3
2269
by: LP | last post by:
Hello, In the past I used SqlCommandBuilder and SqlDataAdapter .Update method to apply changes in a DataTable back to its table source in SQL Server. It worked fine when DataSet had only 1 table, this time I have 2 tables in a dataset and I want to update them all at once. However it only updates the first one and fails when attempts to update 2nd table.. Has anyone done anything like that? It looks like SqlCommandBuilder generates...
0
4919
by: Vijay Balki | last post by:
I am fetching data in DataSet - myDataSet, from a remote database using a Web Service in my VB.NET client..Once I fetch it I store the data in XML file (myXMLFile) using the WriteXML method of the myDataSet. The connection to the Web Service is closed at this point. I update this XML file in my application, and when I am ready to send the data back, I load the XML file back to a DataSet - mySendDataSet, using ReadXML method. Now I...
8
1903
by: rriness | last post by:
I'm getting an inconsistent failure when trying to save data in ADO.Net. I'm using an Access database with a simple query - SELECT StudentID, FirstName, LastName FROM Students - and have no other users in the database. I keep getting the message 'Operation must use an updateable query.' Process seems to work on some PC's, not others. Is there a difference in .Net framework versions? Or some other
5
3696
by: Al | last post by:
Hi, I need to update tables in access 97. The table names have spaces (not my choice). My update fails even though I use the OleDbCommandBuilder. Here is a code I am using myDataAdapter = New OleDbDataAdapter myDataAdapter.SelectCommand = New OleDbCommand("Select * from ", MyConnection) Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(PatientsBillingDataAdapter)
11
2248
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to reflect that the 3 items have been deleted only to discover that the 3 items appear, however when I click on them to display their information which runs a datareader over the same database it appears that the data has now gone. I wondered whether...
8
2696
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. Example: I have a dataadapter that contains one table with one row. I change the value of the 'FisrtName' column in that row from Jack to John. I call ..update on the dataadapter it goes through fine. Now I change that same column in that same row...
3
2612
by: John Cosmas | last post by:
I have a DATATABLE which I have populated in my application, and I need it written out to a particular table I specify in my ACCESS database. My code works to the point of the MERGE and UPDATE, but it creates exactly the number of BLANK records per the populated DATATABLE. Here is my code... pstrDestinationTable = "tws_tbl_Case_Scanner_" & GetDateTimeStamp() pstrSQL = "SELECT * INTO " & pstrDestinationTable & " FROM...
1
1320
OuTCasT
by: OuTCasT | last post by:
Can someone please tell me how does a person use a commandbuilder to update a dataset so that i can update the table in the database. sqlEarningsCommand = New SqlCommand("Select * from earnings order by EmployeeID", sqlConnection) sqlEarningsAdapter = New SqlDataAdapter(sqlEarningsCommand) sqlEarningsDatatable = New DataTable sqlEarningsAdapter.Fill(sqlEarningsDatatable) ...
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.