By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

update database from datatable using commandbuilder

P: 1
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
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 487
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

Post your reply

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