472,352 Members | 1,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 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 1824

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...
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...
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...
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...
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...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.