开发者

How I can save the data from GridView to the database?

I am developing an ASP.Net C# Web Application that contains a GridView to display the records of a certain table from my database which I use ODBC Connection to connect to it and a DataSet to save data in it and edit it then I should save data to the database using the changes made in the DataSet.

I could access the database succefully using the fill() method of the OdbcDataAdapter and I could do databinding so that the data is viewed in the GridView.

My question is how I can save the gridview to the dataset then to the database when any updates or changes done [the vice versa of the operation done before]?

My sample code that is used inside a web form class is as follow:-

 private void SelectFromDatabase()
        { 

            string OdbcConnectionString1     = getConnectionString();

            OdbcConnection OdbcConnection1   = new OdbcConnection(OdbcConnectionString1);

            string OdbcSelectText1 = "SELECT * FROM table";

            OdbcCommand OdbcSelectCommand1   = new OdbcCommand(OdbcSelectText1, OdbcConnection1);

            OdbcDataAdapter OdbcDataAdapter1 = new OdbcDataAdapter();

            try
            { 

                OdbcConnection1.Open();                

                OdbcDataAdapter1.SelectCommand           = OdbcSelectCommand1;

                OdbcDataAdapter1.AcceptChangesDuringFill = true;

                int FillResult                           = OdbcDataAdapter1.Fill(myDataSet, TableName);

                myDataSet.AcceptChanges();

                fillGridViewbyDataset(myGridView, myDataSet, TableName);

                Response.Write("<br/>SelectFromDatabase() Fill Result: " + FillResult);

            } 

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

            finally
            { 
                OdbcConnection1.Close();
            } 

        } 

private void fillGridViewbyDataset(GridView gv, Data开发者_开发百科Set ds, string dt)
{ 
    gv.DataSource = ds;
    gv.DataMember = dt;

    gv.DataBind();
}

what I need is something like:-

how to save Gridview to the DataSet then save the DataSet to the database as i got the gridview updates but the database still without any updates !!

if I have a DataSet called myDs and I edit a field in it by direct access in a loop like the following:-

for (int i = 0; i < myDS.Tables[TableName].Rows.Count; i++)
{
//some function or web method to get the id value of the record being updated
int n = getNewNumber();

//updating the dataset record according to some condition
if (n == 0)
{
myDS.Tables[TableName].Rows[i]["id"] = n;
myDS.Tables[TableName].Rows[i]["description"] = "some data";
}
else
{
myDS.Tables[TableName].Rows[i]["id"] = n;
myDS.Tables[TableName].Rows[i]["description"] = "new data";
}

}

How I make these changes done in the database as I could see it in the GridView when I do databind() but the database is not affected and I try using the fill & update methods of OdbcDataAdapter and OdbcCommandBuilder ??

Please this is urgent as I need it in developing an important application..

Thanks in advance for your replies and answers .....


Everything you need to know about saving from the GridView to the DataSet and to the DB is explained is this article.

Hope this helps!


If it's "an important application", I'd recommend using Stored Procedures and grant only the EXECUTE privilege to the database user on the package. If the user has full DML privileges, your data might be more vulnerable.

Here's a basic tutorial on calling stored procedures

If you have time, I'd also look at the Microsoft Enterprise Library.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜