数据集中的数据库更新在asp.net中不起作用

时间:2014-05-13 10:25:45

标签: c# asp.net ado.net dataset

您好我正在从GridView控件更新DataSet并希望使用此更改的DataSet更新底层数据库。我的代码没有更新底层数据库。请查看我的代码。我的数据库表也有主键列。 “ddlManager”是GridView控件中的DropDownList。每当我从下拉列表中更改选择时,它都会调用“SelectedIndexChanged()”方法

protected void ddlManager_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.NamingContainer;

        string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from tblUsersTable", con);
            SqlCommandBuilder builder = new SqlCommandBuilder(da);
            DataSet ds = new DataSet();
            da.Fill(ds);

            //*********************************
            // code for updating dataset
            DataRow dr = ds.Tables[0].Rows[row.RowIndex];
            if (ddl.SelectedIndex != 0) // zero index contains "Please Select" value
            {
                dr["isManager"] = ddl.SelectedValue;
            }
            ds.AcceptChanges();
            //**********************************

            int rowsUpdated = da.Update(ds);
            if (rowsUpdated > 0)
            {
                lblMessage.Text = rowsUpdated.ToString() + " rows updated ";
            }
            else
            {
                lblMessage.Text = "No row updated ";
            }
        }
    }

0 个答案:

没有答案