开发者

how to Save and Update data from GridView to database in ASP.Net C#?

I developed a web application using ASP.Net C#.

In this application there is a GridView that I fill it with data using ODBCDataAdapter as in the following code:-

protected void Page_Load(object sender, EventArgs e)
{

ConnectionString1 = "DSN=DataSourceName;SRVR=Server;DB=Database;UID=User;PWD=Password;";

OdbcConnection1 = new OdbcConnection(ConnectionString1);

try
{
                OdbcConnection1.Open();

                CommandText1 = "SELECT * FROM TableName";

                DataSet1 = new DataSet();

                OdbcDataAdapter1 = new OdbcDataAdapter(CommandText1, OdbcConnection1);

                OdbcCommandBuilder1 = new OdbcCommandBuilder(OdbcDataAdapter1);

                OdbcDataAdapter1.Fill(DataSet1, "TableName");

                DataSet1.AcceptChanges();

                myGridView.DataSource = DataSet1;
                myGridView.DataMember = "TableName";

                myGridView.DataBind();
}

catch (Exception Exception1)
{
Response.Write("<br/>Exception1 Message: " + Exception1.Message);
}

OdbcConnection1.Close();

}

This code works fine and loads the data from dataset to the GridView.

My problem is that I do some changes in that GridView and I would like to save these changes in the real database using the DataSet that should change according to an event of a button click or a specific condition.

I try using the following but it did not work althogh it gives 0 as the result.

OdbcDataAdapter1.UpdateCommand = new OdbcCommand("UPDATE TableName", OdbcConnection1);

            OdbcDataAdapter1.Fill(DataSet1,"TableName");

            int g = OdbcDataAdapter1.Update(DataSet1,"TableName");

            Response.Write("g: " + g);

I tried also the following:-

            OdbcCommandBuilder1 = new OdbcCommandBuilder(OdbcDataAdapter1);

            try
            {
                int k = OdbcDataAdapter1.Update(DataSet1, "TableName");
                DataSet1.AcceptChanges();
                Response.Write(开发者_如何学编程"k: " + k);
            }
            catch (Exception Except)
            {
                Response.Write("Except: " + Except.Message);
            }

as every time I check the database I find no changes although the changes appears in the GridView..


I need to read the Microsoft and MSDN for more examples and code samples about doing the basic database SQL operations (SELECT - INSERT - UPDATE - DELETE) using C#, ASP.NET, and ADO.Net. The ODBCDataAdapter should be the good way to handle that in case of using Disconnected mode or the ODBCDataReader will be the good way in case of using Online mode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜