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

Z

zhaoJian

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.DataGridCommandEventHandler
(this._CancelUnit);
this.DataGridAdminUnit.EditCommand
+= new
System.Web.UI.WebControls.DataGridCommandEventHandler
(this._EditUnit);

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

this.DataGridAdminUnit.SelectedIndexChanged += new
System.EventHandler(this.DataGrid1_SelectedIndexChanged);
//
// 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_UnitID",
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_UnitID",
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();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top