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

How to update the Database by using SqlDataAdapter object in asp.net?

Here it is my code ,but it can't update the database.How
to do it ?
In _UpdateUnit event, I can not get the original value
to @Original_UnitID,so I set a hidden column named
LabelKey.But It don't update the database.My server is
SqlServer 2000.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace sitemanager
{
/// <summary>
/// adminUnit 的摘要说明。
/// </summary>
public class adminUnit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button
addUnit;
protected System.Web.UI.WebControls.Label
Message;
protected sitemanager.ds ds1;
protected
System.Data.SqlClient.SqlDataAdapter mySqlDataAdapter;
protected
System.Web.UI.WebControls.DataGrid DataGridAdminUnit;
protected System.Data.SqlClient.SqlCommand
sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand
sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand
sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand
sqlDeleteCommand1;
protected
System.Data.SqlClient.SqlConnection sqlConnection1;

private void Page_Load(object sender,
System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
_Bind();
}
}

private void _Bind()
{
mySqlDataAdapter.Fill
(ds1,"department");
DataGridAdminUnit.DataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗
体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修

/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{

System.Configuration.AppSettingsReader
configurationAppSettings = new
System.Configuration.AppSettingsReader();
this.ds1 = new sitemanager.ds();
this.mySqlDataAdapter = new
System.Data.SqlClient.SqlDataAdapter();
this.sqlDeleteCommand1 = new
System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new
System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new
System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new
System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new
System.Data.SqlClient.SqlCommand();

((System.ComponentModel.ISupportInitialize)
(this.ds1)).BeginInit();

this.DataGridAdminUnit.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler
(this._CancelUnit);
this.DataGridAdminUnit.EditCommand
+= new
System.Web.UI.WebControls.DataGridCommandEventHand ler
(this._EditUnit);

this.DataGridAdminUnit.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler
(this._UpdateUnit);

this.DataGridAdminUnit.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexCh anged);
//
// ds1
//
this.ds1.DataSetName = "ds";
this.ds1.Locale = new
System.Globalization.CultureInfo("zh-CN");
//
// mySqlDataAdapter
//

this.mySqlDataAdapter.DeleteCommand =
this.sqlDeleteCommand1;

this.mySqlDataAdapter.InsertCommand =
this.sqlInsertCommand1;

this.mySqlDataAdapter.SelectCommand =
this.sqlSelectCommand1;

this.mySqlDataAdapter.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {



new
System.Data.Common.DataTableMapping("Table", "department",
new System.Data.Common.DataColumnMapping[] {







new
System.Data.Common.DataColumnMapping("UnitID", "UnitID"),







new
System.Data.Common.DataColumnMapping
("UnitName", "UnitName")})});

this.mySqlDataAdapter.UpdateCommand =
this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText
= "DELETE FROM department WHERE (UnitID =
@Original_UnitID)";
this.sqlDeleteCommand1.Connection
= this.sqlConnection1;

this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Unit ID",
System.Data.SqlDbType.VarChar, 2,
System.Data.ParameterDirection.Input, false, ((System.Byte)
(0)), ((System.Byte)(0)), "UnitID",
System.Data.DataRowVersion.Original, null));
//
// sqlConnection1
//

this.sqlConnection1.ConnectionString = ((string)
(configurationAppSettings.GetValue
("myConnection.ConnectionString", typeof(string))));
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText
= "INSERT INTO department(UnitID, UnitName) VALUES
(@UnitID, @UnitName)";
this.sqlInsertCommand1.Connection
= this.sqlConnection1;

this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@UnitID",
System.Data.SqlDbType.VarChar, 2, "UnitID"));

this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@UnitName",
System.Data.SqlDbType.VarChar, 50, "UnitName"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText
= "SELECT UnitID, UnitName FROM department ORDER BY
UnitID";
this.sqlSelectCommand1.Connection
= this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText
= "UPDATE department SET UnitID = @UnitID, UnitName =
@UnitName WHERE (UnitID = @Ori" +
"ginal_UnitID)";
this.sqlUpdateCommand1.Connection
= this.sqlConnection1;

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@UnitID",
System.Data.SqlDbType.VarChar, 2, "UnitID"));

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@UnitName",
System.Data.SqlDbType.VarChar, 50, "UnitName"));

this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Unit ID",
System.Data.SqlDbType.VarChar, 2,
System.Data.ParameterDirection.Input, false, ((System.Byte)
(0)), ((System.Byte)(0)), "UnitID",
System.Data.DataRowVersion.Original, null));
this.Load += new
System.EventHandler(this.Page_Load);

((System.ComponentModel.ISupportInitialize)
(this.ds1)).EndInit();

}
#endregion

private void DataGrid1_SelectedIndexChanged
(object sender, System.EventArgs e)
{

}

private void _EditUnit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int i = e.Item.ItemIndex;

this.DataGridAdminUnit.EditItemIndex =
e.Item.ItemIndex;
_Bind();
}

private void _CancelUnit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

this.DataGridAdminUnit.EditItemIndex = -1;
_Bind();
}

private void _UpdateUnit(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

this.sqlUpdateCommand1.Parameters
["@Original_UnitID"].Value = ((Label)e.Item.FindControl
("LabelKey")).Text;
this.sqlInsertCommand1.Parameters
["@UnitID"].Value = ((TextBox)e.Item.FindControl
("TextBoxUnitID")).Text;
this.sqlInsertCommand1.Parameters
["@UnitName"].Value = ((TextBox)e.Item.FindControl
("TextBoxUnitName")).Text;

this.sqlUpdateCommand1.Parameters
["@UnitID"].Value = ((TextBox)e.Item.FindControl
("TextBoxUnitID")).Text;
this.sqlUpdateCommand1.Parameters
["@UnitName"].Value = ((TextBox)e.Item.FindControl
("TextBoxUnitName")).Text;

int j =
this.mySqlDataAdapter.Update(ds1,"department");


this.DataGridAdminUnit.EditItemIndex = -1;
_Bind();
}
}
}

Nov 17 '05 #1
0 1880

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ALI-R | last post by:
I am using the following code to update my database but it dosn't do the update and it dosen't show any error: SqlDataAdapter myDataAdapter=new SqlDataAdapter(); ...
1
by: Wing | last post by:
Hi all, I have created 2 tables in sql database and join these 2 tables before assign the result to the dataset, and display the result in datagrid. Everything is fine up to this point. The...
4
by: Oscar Thornell | last post by:
Hi, I have a relativley large/complex typed dataset that contains 7-8 tables and some supporting relational tables (lookups) for many-to-many relations. A good exampel would be a dataset that...
4
by: CaptRR | last post by:
I think this is the right group to post to, so here goes. My problem is this, I cannot update the datarow to save my life. Been on this for 2 days now, and still am no closer to figuring it out...
2
by: Nu2ASP.NET | last post by:
What I am trying to do is essentially 'flip' the bits, when the user clicks in the checkbox. For example, if the CheckBox appears checked, and the user un-checks it, I want the underlying data...
4
by: George | last post by:
Hi all, I am having trouble with updating my data in an Access database. here is my code: Imports System.Data.OleDb Dim AppPath As String = Mid(Application.ExecutablePath, 1,...
0
by: Jim in Arizona | last post by:
I'm experimenting by following instructions at this address: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/datalist.aspx on how to use the DataList. I was trying out the...
1
by: gomathinayagam | last post by:
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...
3
by: sampalmer21 | last post by:
Hi, If I update a cell in the DataGridView control, I use the DataAdapter.Update(DataTable) method to update the database, but when I restart the application my changes are not in the database....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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,...
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...

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.