473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Databinding editable GridView with SqlDataSource on button click

This seems like it should be simple to do but for some reason I have
been unable to make it work.

I would like to databind a SqlDataSource to a GridView during the
click event of a button. This sounds easy but the next requirement is
that the GridView is editable. So I have included the SelectCommand
and the UpdateCommand on the SqlDataSource to allow the GridView to be
editable. I have now been able to get the GridView to display data on
the button click using the following code. As you can see I've tried
three different ways of doing this and each causes a different
problem. Now I am stuck and could use some help.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class EditableGridView2 : System.Web.UI.Page
{
// 3. Displays data but the row edit button causes the data to
dissapear.
protected SqlDataSource dataSource = new SqlDataSource();

protected void Page_Load(object sender, EventArgs e)
{
this.gridView.RowEditing += new
GridViewEditEventHandler(gridView_RowEditing);
this.gridView.RowUpdating += new
GridViewUpdateEventHandler(gridView_RowUpdating);
this.gridView.RowUpdated += new
GridViewUpdatedEventHandler(gridView_RowUpdated);
this.getData.Click += new EventHandler(getData_Click);

// 3. Displays data but the row edit button causes the data to
dissapear.
dataSource.ID = "SqlDataSource1";
dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();
this.Controls.Add(dataSource);
this.gridView.DataSourceID = dataSource.ID;
}

void getData_Click(object sender, EventArgs e)
{
// 1. and 2.
//SqlDataSource dataSource = new SqlDataSource();
//dataSource.ID = "SqlDataSource1";
//dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();

dataSource.SelectCommand = "SELECT [appID], [status], [clientName]
FROM [Applications] WHERE ([status] = @status)";
dataSource.UpdateCommand = "UPDATE [MemberApplications] SET [status]
= @status, [clientName] = @clientName WHERE [appID] = @appID";
dataSource.SelectParameters.Add("status", "Pending");
dataSource.UpdateParameters.Add(new Parameter("status"));
dataSource.UpdateParameters.Add(new Parameter("clientName"));
dataSource.UpdateParameters.Add(new Parameter("appID"));

// 1. Displays data but clicking the row edit causes the exception
"The DataSourceID of 'gridView' must be the ID of a control of type
IDataSource. A control with ID 'SqlDataSource1' could not be found."
//this.Controls.Add(dataSource);
//this.gridView.DataSourceID = dataSource.ID;

// 2. Displays data but the row edit button does not work.
//this.gridView.DataSource = dataSource;
//this.gridView.DataBind();
}

void gridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
this.message.Text = e.AffectedRows.ToString();
}

void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//
}

void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//
}
}
Dec 18 '07 #1
4 9746
On Dec 18, 11:13 am, "mohaa...@gmail.com" <mohaa...@gmail.comwrote:
This seems like it should be simple to do but for some reason I have
been unable to make it work.

I would like to databind a SqlDataSource to a GridView during the
click event of a button. This sounds easy but the next requirement is
that the GridView is editable. So I have included the SelectCommand
and the UpdateCommand on the SqlDataSource to allow the GridView to be
editable. I have now been able to get the GridView to display data on
the button click using the following code. As you can see I've tried
three different ways of doing this and each causes a different
problem. Now I am stuck and could use some help.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class EditableGridView2 : System.Web.UI.Page
{
// 3. Displays data but the row edit button causes the data to
dissapear.
protected SqlDataSource dataSource = new SqlDataSource();

protected void Page_Load(object sender, EventArgs e)
{
this.gridView.RowEditing += new
GridViewEditEventHandler(gridView_RowEditing);
this.gridView.RowUpdating += new
GridViewUpdateEventHandler(gridView_RowUpdating);
this.gridView.RowUpdated += new
GridViewUpdatedEventHandler(gridView_RowUpdated);
this.getData.Click += new EventHandler(getData_Click);

// 3. Displays data but the row edit button causes the data to
dissapear.
dataSource.ID = "SqlDataSource1";
dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();
this.Controls.Add(dataSource);
this.gridView.DataSourceID = dataSource.ID;
}

void getData_Click(object sender, EventArgs e)
{
// 1. and 2.
//SqlDataSource dataSource = new SqlDataSource();
//dataSource.ID = "SqlDataSource1";
//dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();

dataSource.SelectCommand = "SELECT [appID], [status], [clientName]
FROM [Applications] WHERE ([status] = @status)";
dataSource.UpdateCommand = "UPDATE [MemberApplications] SET [status]
= @status, [clientName] = @clientName WHERE [appID] = @appID";
dataSource.SelectParameters.Add("status", "Pending");
dataSource.UpdateParameters.Add(new Parameter("status"));
dataSource.UpdateParameters.Add(new Parameter("clientName"));
dataSource.UpdateParameters.Add(new Parameter("appID"));

// 1. Displays data but clicking the row edit causes the exception
"The DataSourceID of 'gridView' must be the ID of a control of type
IDataSource. A control with ID 'SqlDataSource1' could not be found."
//this.Controls.Add(dataSource);
//this.gridView.DataSourceID = dataSource.ID;

// 2. Displays data but the row edit button does not work.
//this.gridView.DataSource = dataSource;
//this.gridView.DataBind();
}

void gridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
this.message.Text = e.AffectedRows.ToString();
}

void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//
}

void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//
}

}
Shouldn't the tablename be same in both select and update commands?

dataSource.SelectCommand = "SELECT [appID], [status], [clientName]
FROM
[Applications]
WHERE ([status] = @status)";
dataSource.UpdateCommand = "UPDATE
[MemberApplications]
SET [status]
= @status, [clientName] = @clientName WHERE [appID] = @appID";
Dec 18 '07 #2
Yes, the table name should be the same in both commands. Sorry about
the bad pasting job. The code really is the same in both places. So,
just assume that the table name is the same. The code still doesn't
work.

Thanks for the heads up.

On Dec 18, 2:22 pm, Latish Sehgal <latish.seh...@gmail.comwrote:
On Dec 18, 11:13 am, "mohaa...@gmail.com" <mohaa...@gmail.comwrote:


This seems like it should be simple to do but for some reason I have
been unable to make it work.
I would like to databind a SqlDataSource to a GridView during the
click event of a button. This sounds easy but the next requirement is
that the GridView is editable. So I have included the SelectCommand
and the UpdateCommand on the SqlDataSource to allow the GridView to be
editable. I have now been able to get the GridView to display data on
the button click using the following code. As you can see I've tried
three different ways of doing this and each causes a different
problem. Now I am stuck and could use some help.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class EditableGridView2 : System.Web.UI.Page
{
// 3. Displays data but the row edit button causes the data to
dissapear.
protected SqlDataSource dataSource = new SqlDataSource();
protected void Page_Load(object sender, EventArgs e)
{
this.gridView.RowEditing += new
GridViewEditEventHandler(gridView_RowEditing);
this.gridView.RowUpdating += new
GridViewUpdateEventHandler(gridView_RowUpdating);
this.gridView.RowUpdated += new
GridViewUpdatedEventHandler(gridView_RowUpdated);
this.getData.Click += new EventHandler(getData_Click);
// 3. Displays data but the row edit button causes the data to
dissapear.
dataSource.ID = "SqlDataSource1";
dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToStri-ng();
this.Controls.Add(dataSource);
this.gridView.DataSourceID = dataSource.ID;
}
void getData_Click(object sender, EventArgs e)
{
// 1. and 2.
//SqlDataSource dataSource = new SqlDataSource();
//dataSource.ID = "SqlDataSource1";
//dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToStri-ng();
dataSource.SelectCommand = "SELECT [appID], [status], [clientName]
FROM [Applications] WHERE ([status] = @status)";
dataSource.UpdateCommand = "UPDATE [MemberApplications] SET [status]
= @status, [clientName] = @clientName WHERE [appID] = @appID";
dataSource.SelectParameters.Add("status", "Pending");
dataSource.UpdateParameters.Add(new Parameter("status"));
dataSource.UpdateParameters.Add(new Parameter("clientName"));
dataSource.UpdateParameters.Add(new Parameter("appID"));
// 1. Displays data but clicking the row edit causes the exception
"The DataSourceID of 'gridView' must be the ID of a control of type
IDataSource. A control with ID 'SqlDataSource1' could not be found."
//this.Controls.Add(dataSource);
//this.gridView.DataSourceID = dataSource.ID;
// 2. Displays data but the row edit button does not work.
//this.gridView.DataSource = dataSource;
//this.gridView.DataBind();
}
void gridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
this.message.Text = e.AffectedRows.ToString();
}
void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//
}
void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//
}
}

Shouldn't the tablename be same in both select and update commands?

dataSource.SelectCommand = "SELECT [appID], [status], [clientName]
FROM
[Applications]
WHERE ([status] = @status)";
dataSource.UpdateCommand = "UPDATE
[MemberApplications]
SET [status]
= @status, [clientName] = @clientName WHERE [appID] = @appID";- Hide quoted text -

- Show quoted text -
Dec 18 '07 #3
Ok. I am able to get the Edit thing to work, the issue here is that
when you click Edit, the page reloads and since you have defined your
select and update commands in the button handler (which does not
execute on page reload), the datasource has incomplete information for
the edit to work. Debug and watch the datasource to check this out.
Also, since you do not want to include this information in the page
load event, i have added this to the Row_Edit event. It looks
something like

void gridView_RowEditing(object sender, GridViewEditEventArgs
e)
{
//
dataSource.SelectCommand = "SELECT [CustomerID],
[CompanyName], [Phone] FROM [Customers]";
dataSource.UpdateCommand = "UPDATE [Customers] SET
[CompanyName] = @CompanyName, [Phone] = @Phone WHERE [CustomerID] =
@CustomerID";

dataSource.UpdateParameters.Add(new
Parameter("CompanyName"));
dataSource.UpdateParameters.Add(new Parameter("Phone"));
dataSource.UpdateParameters.Add(new
Parameter("CustomerID"));

gridView.DataSource = dataSource;

gridView.EditIndex = e.NewEditIndex;
gridView.DataBind();

}

I think you might need to define some implementation in your other
events also, but hopefully this should give you a start.

-Latish Sehgal
http://www.dotnetsurfers.com/
Dec 19 '07 #4
Hello Latish,

I'm back at it again. Trying to solve this problem and implement what
you have given me. What I have now is the following code which almost
works but doesn't. In the end this worked up to the point where the
update is being made. The code excutes as far as the
gridView_RowUpdating event but then the gridView_RowUpdated event is
never called. So the update is never done. Can you or anyone else who
wants to look at this and see what I might be able to change to make
this work.

<code>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class EditableGridView : System.Web.UI.Page
{
void gridView_RowEditing(object sender, GridViewEditEventArgs e)
{
// Re-create the SqlDataSource here for row edits.

// The SqlDataSource already exists so there isn't a reason to set
the following values again.
//this.dataSource.ID = "SqlDataSource1";
//this.dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();
//this.Controls.Add(this.dataSource);
this.gridView.DataSource = this.dataSource;

this.dataSource.SelectCommand = "SELECT [appID], [status],
[clientName] FROM [MemberApplications] WHERE ([status] = @status) AND
dssgDelete = 'no'";
this.dataSource.UpdateCommand = "UPDATE [MemberApplications] SET
[status] = @status, [clientName] = @clientName WHERE [appID] =
@appID";
// Adding this parameter here causes a "Parameter already added"
exception.
//this.dataSource.SelectParameters.Add("status", "Pending");
this.dataSource.UpdateParameters.Add(new Parameter("status"));
this.dataSource.UpdateParameters.Add(new Parameter("clientName"));
this.dataSource.UpdateParameters.Add(new Parameter("appID"));

this.gridView.EditIndex = e.NewEditIndex;
this.gridView.DataBind();
}

void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowIndex = e.RowIndex;
}

void gridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
this.message.Text = e.AffectedRows.ToString();
}

protected SqlDataSource dataSource = new SqlDataSource();

protected void Page_Load(object sender, EventArgs e)
{
this.gridView.RowEditing += new
GridViewEditEventHandler(gridView_RowEditing);
this.gridView.RowUpdating += new
GridViewUpdateEventHandler(gridView_RowUpdating);
this.gridView.RowUpdated += new
GridViewUpdatedEventHandler(gridView_RowUpdated);
this.getData.Click += new EventHandler(getData_Click);

this.dataSource.ID = "SqlDataSource1";
this.dataSource.ConnectionString =
ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ToString();
this.Controls.Add(this.dataSource);
}

void getData_Click(object sender, EventArgs e)
{
this.dataSource.SelectCommand = "SELECT [appID], [status],
[clientName] FROM [MemberApplications] WHERE ([status] = @status) AND
dssgDelete = 'no'";
this.dataSource.UpdateCommand = "UPDATE [MemberApplications] SET
[status] = @status, [clientName] = @clientName WHERE [appID] =
@appID";
this.dataSource.SelectParameters.Add("status", "Pending");
this.dataSource.UpdateParameters.Add(new Parameter("status"));
this.dataSource.UpdateParameters.Add(new Parameter("clientName"));
this.dataSource.UpdateParameters.Add(new Parameter("appID"));

this.gridView.DataSource = this.dataSource;
this.gridView.DataBind();
}
}
</code>

Regards,

Aaron

On Dec 19 2007, 10:47 am, Latish Sehgal <latish.seh...@gmail.com>
wrote:
Ok. I am able to get the Edit thing to work, the issue here is that
when you click Edit, the page reloads and since you have defined your
select and update commands in the button handler (which does not
execute on page reload), the datasource has incomplete information for
the edit to work. Debug and watch the datasource to check this out.
Also, since you do not want to include this information in the page
load event, i have added this to the Row_Edit event. It looks
something like

void gridView_RowEditing(object sender, GridViewEditEventArgs
e)
{
//
dataSource.SelectCommand = "SELECT [CustomerID],
[CompanyName], [Phone] FROM [Customers]";
dataSource.UpdateCommand = "UPDATE [Customers] SET
[CompanyName] = @CompanyName, [Phone] = @Phone WHERE [CustomerID] =
@CustomerID";

dataSource.UpdateParameters.Add(new
Parameter("CompanyName"));
dataSource.UpdateParameters.Add(new Parameter("Phone"));
dataSource.UpdateParameters.Add(new
Parameter("CustomerID"));

gridView.DataSource = dataSource;

gridView.EditIndex = e.NewEditIndex;
gridView.DataBind();

}

I think you might need to define some implementation in your other
events also, but hopefully this should give you a start.

-Latish Sehgalhttp://www.dotnetsurfers.com/
On Dec 19 2007, 10:47*am, Latish Sehgal <latish.seh...@gmail.com>
wrote:
Ok. I am able to get the Edit thing to work, the issue here is that
when you click Edit, the page reloads and since you have defined your
select and update commands in the button handler (which does not
execute on page reload), the datasource has incomplete information for
the edit to work. Debug and watch the datasource to check this out.
*Also, since you do not want to include this information in the page
load event, i have added this to the Row_Edit event. It looks
something like

* * * * void gridView_RowEditing(object sender, GridViewEditEventArgs
e)
* * * * {
* * * * * * * * //
* * * * * * dataSource.SelectCommand = "SELECT [CustomerID],
[CompanyName], [Phone] FROM [Customers]";
* * * * * * dataSource.UpdateCommand = "UPDATE [Customers] SET
[CompanyName] = @CompanyName, [Phone] = @Phone WHERE [CustomerID] =
@CustomerID";

* * * * * * dataSource.UpdateParameters.Add(new
Parameter("CompanyName"));
* * * * * * dataSource.UpdateParameters.Add(new Parameter("Phone"));
* * * * * * dataSource.UpdateParameters.Add(new
Parameter("CustomerID"));

* * * * * * gridView.DataSource = dataSource;

* * * * * * gridView.EditIndex = e.NewEditIndex;
* * * * * * gridView.DataBind();

* * * * }

I think you might need to define some implementation in your other
events also, but hopefully this should give you a start.

-Latish Sehgalhttp://www.dotnetsurfers.com/
Jan 10 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: P | last post by:
Hello, I am having a difficult time updating a record via a stored procedure using the gridview and sqldatasource. I cannot seem to be able to find a way to set everything up so that I can pass...
8
by: Mike Kelly | last post by:
I've chosen to implement the "optimistic concurrency" model in my application. To assist in that, I've added a ROWVERSION (TIMESTAMP) column to my main tables. I read the value of the column in my...
2
by: dba56 | last post by:
In an ASP.Net 2.0 page using SQL Server 2K, I have a editable gridview that is bound to a SQLDataSource using stored procedures for its select and update queries. The grid works fine but is slow...
0
by: Frank | last post by:
Hello All, I am working with VS.NET 2005 and I have an editable GridView whose HTML markup is shown below. In short, when the user clicks on the Edit button, one of the textboxes is replaced...
0
by: Frank | last post by:
Hello All, I am working with VS.NET 2005 and I have an editable GridView whose HTML markup is shown below. In short, when the user clicks on the Edit button, one of the textboxes is replaced...
2
by: beish1 | last post by:
Hi I have a page which displays a Gridview containing on each row a checkbox and 3 fields/cells (title, longitude, latitude). On the same page I have two textboxes containing the latitude and...
2
by: Hrvoje Vrbanc | last post by:
Hello all! As I have only recently started to use native ASP.NET 2.0 data access controls (and found them to be very powerful), I have one question that I was unable to find the answer to: -...
6
by: dipalichavan82 | last post by:
i want editable gridview in C# vs2005.i want a column get added to gridview with edit button. once i click on edit button at runtime,all cells shud become textbox and edit should be replaced by...
0
by: ADN | last post by:
Hi, I am currently extending the GridView control and would like to add a button to the GridView so that it will automatically render one button at the top of the grid. I have a click event for...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.