473,322 Members | 1,401 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,322 software developers and data experts.

update database from datatable using commandbuilder

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 3234
CyberSoftHari
487 Expert 256MB
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
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...
3
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...
0
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...
8
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...
5
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...
11
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...
8
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. ...
3
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,...
1
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.