PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Unable to update gridview

Reply

Unable to update gridview

 
Thread Tools Rate Thread
Old 16-06-2006, 07:39 PM   #1
=?Utf-8?B?VGVyZW5jZQ==?=
Guest
 
Posts: n/a
Default Unable to update gridview


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!

  Reply With Quote
Old 19-06-2006, 02:41 PM   #2
W.G. Ryan - MVP
Guest
 
Posts: n/a
Default Re: Unable to update gridview

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.
"Terence" <Terence@discussions.microsoft.com> wrote in message
news:7F2FCCD2-00C6-4136-8DA2-7E1EFF43912E@microsoft.com...
> 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!
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off