Unable to update gridview

G

Guest

Hi,

I have a web user control which includes a gridview and a sqldatasource. It
also has a property called TableName which is used to populate the gridview.
However, i'm running into a problem where i can select and delete row but i
cannot update a row.

Here is the code in the usercontrol

protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
if (Roles.IsUserInRole("Management"))
{
GridView1.AutoGenerateEditButton = true;
GridView1.AutoGenerateDeleteButton = true;
}
}
SqlDataSource1.ConnectionString = Session["ConnStr"].ToString();
//SqlDataSource1.SelectCommand = "SELECT [Studio],
Code:
, [Name]
FROM [Studio]";
//SqlDataSource1.UpdateCommand = "Update Studio set [Code] = @Code,
[Name] = @Name where [Studio] = @Studio";
//SqlDataSource1.DeleteCommand = "Delete from Studio where [Studio]
= @Studio";
SqlDataSource1.DeleteCommand = "Delete from " + tablename + " where
[" + tablename + "] = @" + tablename;
SqlDataSource1.SelectCommand = "SELECT [" + tablename + "], [Code],
[Name] FROM [" + tablename + "]";
SqlDataSource1.UpdateCommand = "Update [" + tablename + "] set
[Code] = @Code, [Name] = @Name where [" + tablename + "] = @" + tablename;
SqlDataSource1.DataBind();

string[] arListType = new string[1];
arListType[0] = tablename;
GridView1.DataKeyNames = arListType;
BoundField bf = new BoundField();
bf.DataField = tablename;
bf.HeaderText = tablename;
//bf.Visible = false;
bf.ReadOnly = true;

BoundField bf2 = new BoundField();
bf2.DataField = "Code";
bf2.HeaderText = "Code";

BoundField bf3 = new BoundField();
bf3.DataField = "Name";
bf3.HeaderText = "Name";

GridView1.Columns.Clear();
GridView1.Columns.Add(bf);
GridView1.Columns.Add(bf2);
GridView1.Columns.Add(bf3);

GridView1.DataBind();

}

Does anyone has any idea why and how to fix it?

Thanks!
 
W

W.G. Ryan - MVP

Check teh dataset for HasChanges right before calling Update and make sure
Changes are present. If not, update won't work. Verify you have a key on the
table (although if the code generated than I'm guessing it does). Check the
parameter mappings and make sure they are set and mapped to the right fields
too. One of these is probably the problem.
 

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

Top