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
- create a datatable and assign the required table using select command
- DataRow dr = dt.NewRow();
- foreach (Control c in this.form1.Controls)
- {
- for (int i = 0; i <= dt.Columns.Count-1; i++)
- {
- if (dt.Columns[i].ColumnName == c.ID)
- {
- switch (c.GetType().ToString())
- {
- case "System.Web.UI.WebControls.TextBox":
- TextBox tx = (TextBox)c;
- dr[c.ID] =tx.Text;
- break;
- case "System.Web.UI.WebControls.DropDownList":
- DropDownList drop = (DropDownList)c;
- dr[c.ID] = drop.Text;
- break;
- }
- }
- }
- }
- dt.Rows.Add(dr);
- string constr1;
- SqlConnection sqlcon1;
- constr1 = "server=dotnet6;database=staff;user id=sa;password=sa;";
- sqlcon1 = new SqlConnection(constr1);
- string sqlquery = "select * from staffdetails";
- SqlDataAdapter da = new SqlDataAdapter(sqlquery,sqlcon1);
- SqlCommandBuilder blr = new SqlCommandBuilder(da);
- da.Update(dt);
- da.Dispose();
- BUT FOR EDIT A ALREADY ONE I WROTE THE FOLLOW..IT WON'T WORK..
- DataRow dr = dt.Rows[dt.Rows.Count - 1];
- dr.BeginEdit();
- foreach (Control c in this.form1.Controls)
- {
- for (int i = 0; i <= dt.Columns.Count - 1; i++)
- {
- if (dt.Columns[i].ColumnName == c.ID)
- {
- switch (c.GetType().ToString())
- {
- case "System.Web.UI.WebControls.TextBox":
- TextBox tx = (TextBox)c;
- dr[c.ID] =tx.Text;
- break;
- case "System.Web.UI.WebControls.DropDownList":
- DropDownList drop = (DropDownList)c;
- dr[c.ID] = drop.Text;
- break;
- }
- }
- }
- }
- dt.AcceptChanges();
- dr.EndEdit();
- string constr1;
- SqlConnection sqlcon1;
- constr1 = "server=dotnet6;database=staff;user id=sa;password=sa;";
- sqlcon1 = new SqlConnection(constr1);
- string sqlquery = "select * from staffdetails";
- SqlDataAdapter da = new SqlDataAdapter(sqlquery, sqlcon1);
- SqlCommandBuilder blr = new SqlCommandBuilder(da);
- da.Update(dt);
- da.Dispose();
plzz help me