Load Gridview from Click event of button using AJAX

  • Thread starter Thread starter Alex. O. Koranteng
  • Start date Start date
A

Alex. O. Koranteng

I am attempting to load gridview using a class module. The module is called
by the OnClick_Event of button. All my controls are within the script manager
and Update panels. I have clientside code to show the status of completion of
load, but I am getting Error load message. Please find below my
class module; I could send the rest of the sample code. Any suggestions

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

/// <summary>
/// Summary description for Class1
/// </summary>
public class GetEmployeesDB
{
private static readonly string _conString;

public static SqlDataReader GetEmployees(string sortExpression, int
startRowIndex, int maximumRows)
{
// Initialize connection
SqlConnection con = new SqlConnection(_conString);
SqlCommand cmd = new SqlCommand("dbo.products_sel", con);


SqlDataAdapter adapter = new SqlDataAdapter(cmd);

// Initialize command
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;

DataSet ds = new DataSet();

con.Open();
adapter.Fill(ds, "Employees");

// Create parameters
cmd.Parameters.AddWithValue("@SortExpression", sortExpression);
cmd.Parameters.AddWithValue("@StartRowIndex", startRowIndex);
cmd.Parameters.AddWithValue("@MaximumRows", maximumRows);

// Execute command
//con.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);

}

static GetEmployeesDB()
{
_conString =
WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
}


}
 
Hi Alex,
I am attempting to load gridview using a class module. The module is called
by the OnClick_Event of button. All my controls are within the script manager
and Update panels. I have clientside code to show the status of completion of
load, but I am getting Error load message. Please find below my
class module; I could send the rest of the sample code. Any suggestions

Could you tell me the error description and how to reproduce it? To narrow
down the problem please bind the GridView to a hard coded collection first.
Do you still get the same error?

If it still doesn't work please send me a demo that can reproduce this
issue. My email is (e-mail address removed). Please update here after sending
the project in case I missed that email.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Allen,

Thanks for your help. I have attached the code sample as requested and have
mailed it to your email account. Thanks for all your help.
 
Hi Alex,
Thanks for your help. I have attached the code sample as requested and have
mailed it to your email account. Thanks for all your help.

I cannot find your email in my inbox. Could you resend it? My email is
(e-mail address removed).

Regards,
Allen Chen
Microsoft Online Support
 
Allen,

I just resend the sample file you requested to your new email address and
thanks for all your help soo far.
 
Hi Alex,
I just resend the sample file you requested to your new email address and
thanks for all your help soo far.

I've tested your code. In the Default2.aspx page the error is caused by
throwing exception in this event handler:

public DataSet GetEmployees()
{
throw new Exception("The method or operation is not implemented.");
}

In the PartialPageRendering1.aspx page the error is caused by key mismatch
in the code below:
string cn =
WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;

The following code is the correct one:
string cn =
WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].Conne
ctionString;

My stored procedure is:
ALTER PROCEDURE dbo.products_sel
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
SELECT TOP 10

Productid
,productname
FROM
Products

RETURN

Another issue is in the JavaScript function. The Label1 is not found in
your JavaScript functions. Below is the correct code:

function invokingWebRequest(){
document.getElementById("<%=Label1.ClientID %>").innerHTML = "Getting
Data...";
}

function completeWebRequest(){
document.getElementById("<%=Label1.ClientID %>").innerHTML = "Data
Retrieved";
}

It then works fine on my side. Could you let me know does it work for you?
If not how to reproduce the error (and, what's the error description?)?


Regards,
Allen Chen
Microsoft Online Support
 
Allen,

I have implemented your code suggestions and PartialPageRendering1.aspx is
working just fine. However, I am getting error shown below on Default2.aspx.
Please check your inbox for detail explanation and any suggestions will be
appreciated. I have found your technical support very invaluable.


Server Error in '/Ajaxload1' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: CS0118: 'GetEmployeesDB' is a 'type' but is used
like a 'variable'

Source Error:



Line 24: DataSet ds = null;
Line 25:
Line 26: ds = GetEmployeesDB();
Line 27:
Line 28: //ds = EmployeesDB();


Source File:
c:\Users\test\Desktop\techSupport\Outgoing\Ajaxload1\Default2.aspx.cs
Line: 26






Allen Chen said:
Hi Alex,
I just resend the sample file you requested to your new email address and
thanks for all your help soo far.

I've tested your code. In the Default2.aspx page the error is caused by
throwing exception in this event handler:

public DataSet GetEmployees()
{
throw new Exception("The method or operation is not implemented.");
}

In the PartialPageRendering1.aspx page the error is caused by key mismatch
in the code below:
string cn =
WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;

The following code is the correct one:
string cn =
WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].Conne
ctionString;

My stored procedure is:
ALTER PROCEDURE dbo.products_sel
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
SELECT TOP 10

Productid
,productname
FROM
Products

RETURN

Another issue is in the JavaScript function. The Label1 is not found in
your JavaScript functions. Below is the correct code:

function invokingWebRequest(){
document.getElementById("<%=Label1.ClientID %>").innerHTML = "Getting
Data...";
}

function completeWebRequest(){
document.getElementById("<%=Label1.ClientID %>").innerHTML = "Data
Retrieved";
}

It then works fine on my side. Could you let me know does it work for you?
If not how to reproduce the error (and, what's the error description?)?


Regards,
Allen Chen
Microsoft Online Support
 
Hi Alex,

Thanks for your update.
Compiler Error Message: CS0118: 'GetEmployeesDB' is a 'type' but is used
like a 'variable'

GetEmployeesDB is a class. It cannot be used in this way:

DataSet ds = null;
ds = GetEmployeesDB();

Do you want to call the static method GetEmployees method in the
GetEmployeesDB class? If so you have two options:

1. Return DataSet from the GetEmployees method.

To fill a DataSet please refer to:
http://msdn.microsoft.com/en-us/library/bh8kx08z.aspx

2. Return SqlDataReader (as what you've done in your code):

using (SqlDataReader sr = GetEmployeesDB.GetEmployees("sortexpression",
0, 5))
{
gvProducts.DataSource = sr;
gvProducts.DataBind();
}

Please test it and let me know if it works.


Regards,
Allen Chen
Microsoft Online Support
 
Allen,

Thanks a lot for your suggestions and links. I will implement them, do some
testing and update you by Wednesday.
 
Hi Alex,
Thanks a lot for your suggestions and links. I will implement them, do some
testing and update you by Wednesday.

Have you tried my suggestion? Can it work?

Regards,
Allen Chen
Microsoft Online Support
 
Back
Top